#!/bin/bash

REPO_TYPE=$1
REPO_NAME=$2

function usage {
	local BASECMD=$(basename "$0")
	echo 'usage: <REPO_TYPE> <REPO_NAME> [DESTINATION]'
	echo 'example:'
	echo -e '\t# REPO_TYPE=github'
	echo -e "\t$BASECMD github Dolibarr/dolibarr\n"
	echo -e '\t# REPO_TYPE=artcode'
	echo -e "\t$BASECMD artcode pvincent/debian-gnome\n"
	exit -1
}

function get_github {
	release=$( git ls-remote --tags --sort="v:refname" \
		git://github.com/$REPO_NAME \
		| tail -n1 | cut -f2 | cut -d '/' -f3 )
	release=${release%^\{\}} # remove extra characters ^{} from github
	echo $release
}

function get_artcode {
	release=$( git ls-remote --tags --sort="v:refname" \
		https://git.artcode.re/$REPO_NAME \
		| tail -n1 | cut -f2 | cut -d '/' -f3 )
	echo $release
}


[[ $# -ne 2 ]] && usage

case $REPO_TYPE in
	github )
		get_github
		;;
	artcode )
		get_artcode
		;;
	* )
		echo "repository type <${REPO_TYPE}> not yet supported!" && exit 1
		;;
esac