#!/bin/sh
#
# Copyright (C) 2012-2016  Etersoft
# Copyright (C) 2012-2016  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

PROGDIR=$(dirname $0)
[ "$PROGDIR" = "." ] && PROGDIR=$(pwd)

. $PROGDIR/giter-common-functions
. $PROGDIR/giter-git-functions
. $PROGDIR/giter-girar-functions


if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
	echo "gremote - git remote -v analogue"
	echo "Use: gremote [GIRAR] [options]"
	echo "Options:"
	echo "  -u USER  add path to USER package repo"
	echo "  -o       add path to the origin repo (/srpms or /gears)"
	echo "  -p       convert paths to public URL if possible"
	exit 0
fi

set_work_hosts $1 && shift

if [ "$1" = "-u" ] ; then
	shift
	RU="$1"
	#[ -n "$RU" ] || fatal "User missed. Please, run as gremote -u USER"
	#SPEC=$(get_gear_spec)
	#build_rpms_name "$SPEC"
	# TODO: rewrite
	PKGNAME=$(get_repo_name)
	[ -n "$PKGNAME" ] || fatal "Can't detect current project name"

	[ -n "$RU" ] || RU=$(get_girar_user)

	ALIAS=$RU
	# for our remote we use name git.alt or git.eter
	[ "$RU" = $(get_girar_user) ] && ALIAS="$GITHOST"

	docmd git remote add $ALIAS $GITHOST:$(get_user_repo "$RU" $PKGNAME)
	exit
fi

if [ "$1" = "-o" ] ; then
	# http://git.altlinux.org/gears/N/NAME.git
	PKGNAME=$(get_repo_name)
	echo "Get correct repo path for $PKGNAME from $GITHOST..."
	RREPO=$(get_girar_repo $PKGNAME) || fatal "Can't detect origin repo for $PKGNAME"
	# TODO: add http access support when have no ssh access
	docmd git remote add gear $GITHOST:$RREPO
	exit
fi

if [ "$1" = "-p" ] ; then
	get_remote_repo_list -v | \
	while read alias url; do
		if rhas "$url" "^git@.*:" ; then
		        # git@gitlab.eterfund.ru:jenkins/test.git
			# git@github.com:vitlav/libnatspec.git
			# https://github.com/Etersoft/uniset2.git
			domain=$(echo "$url" | sed -e "s/git@\(.*\):.*/\1/g") #"
			path=$(echo "$url" | sed -e "s/git@.*://g")
			url="https://$domain/$path"
			url=$(echo "$url" | sed -e "s|\.git$||")
		elif ! rhas "$url" "://" && rhas "$url" ":" ; then
			# git.eter:/projects/korinf/giter.git
			# http://git.etersoft.ru/projects/korinf/giter.git
			# FIXME: the same in gclone (use function!)
			sshalias=$(echo "$url" | sed -e "s/:.*//g")
			path=$(echo "$url" | sed -e "s/$sshalias://g")
			# add missed .git suffix
			rhas "$path" "\.git$" || path="$path.git"

			# hack for related paths in git.*
			if rhas "$path" "^(packages|public)" && is_girar_name $sshalias ; then
				giraruser=$(GITHOST=$sshalias get_girar_user)
				if [ -n "$giraruser" ] ; then
					path="/people/$giraruser/$path"
					url="$sshalias:$path"
				fi
			fi
			# try print with public Url if we have one
			if [ -n "$sshalias" ] && rhas "$path" "^/" ; then
				purl=$(get_git_url "$sshalias")
				if [ -n "$purl" ] ; then
					url="$purl$path"
				fi
			fi
		fi
		echo -e "$alias\t$url"
	done
	exit
fi

test -r "$1" && fatal "Do not need params anymore"

showcmd git remote -v
get_remote_repo_list -v
