#!/bin/sh
#
# Copyright (C) 2008-2017  Etersoft
# Copyright (C) 2008-2017  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

test -r "$1" && fatal "Do not need any files in params"

PUSHFORCE=
PUSHALLBRANCHES=
PUSHTOALL=
TAGSALL=

#############################
name=${0##*/}
Descr="$name - publish to remote git repository"
Usage="Usage: $name [options] [GIRAR/remote alias] [branch/tag]"

phelp()
{
	echo "$Descr"
	echo "$Usage"
	echo "Options:"
	echo " -f - force push"
	echo " -r - push to all remotes"
	echo " -a - push all branches"
	echo " -t - push all tags"
	echo
	echo "Examples:"
	echo " $ gpush git.alt"
}

LISTARGS=
for opt in "$@" ; do
    case $opt in
        -h) phelp; exit 0;;
        -f) PUSHFORCE="--force" ;;
        -r) PUSHTOALL="--toall" ;;
        -a) PUSHALLBRANCHES="--all" ;;
        -t) TAGSALL="--tags" ;;
        *)
            LISTARGS="$LISTARGS $opt"
    esac
done

forcegirar()
{
FORCEGIRAR=
if force_work_hosts "$1" ; then
    # We have set host via first program arg
    FORCEGIRAR="$1"
    shift
fi
LISTARGS="$@"
}

forcegirar $LISTARGS

push_to_remote()
{
	local GHOST=$1
	local TEXTTAG=
	local PROJECTDIR=$(get_root_git_dir)

	[ -n "$TAGSALL" ] && TEXTTAG="(with all tags)"

	echo
	echover "** Push $TEXTBRANCH from $PROJECTDIR to $GHOST $TEXTTAG"

	docmd git push $PUSHFORCE $GHOST $CURRENTBRANCH || return

	if [ -n "$TAGSALL" ] ; then
		docmd git push --tags $PUSHFORCE $GHOST $CURRENTBRANCH
		return
	fi

	if is_last_commit_tag ; then
		local LASTTAG=$(get_last_tag)
		if [ -n "$LASTTAG" ] ; then
			echo "*** Push last tag $LASTTAG"
			docmd git push $PUSHFORCE $GHOST $LASTTAG
			return
		fi
	fi
	
	return 0
}


tune_girarlist()
{

REMOTELIST="$(estrlist union $(get_remote_pub_list) $(get_remote_git_list) | estrlist list -)"

# If run with gear as param
if [ -n "$FORCEGIRAR" ] ; then
	if [ -z "$REMOTELIST" ] && is_girar_name "$FORCEGIRAR" ; then
		docmd ginit $FORCEGIRAR
	fi
	LISTGITHOST="$GITHOST"
	# hack to allow gitery -> git.alt compatibility
	if [ "$GITHOST" = "gitery" ] && ! is_exist_remote_repo "$GITHOST" && is_exist_remote_repo "git.alt" ; then
		LISTGITHOST="git.alt"
	fi
else
	LISTGITHOST="$REMOTELIST"
	if [ -z "$LISTGITHOST" ] ; then
		# origin by default if exists and alone
		if get_remote_repo_list | grep -q origin ; then
			LISTGITHOST="origin"
		fi
	fi
fi

if [ -n "$PUSHTOALL" ] ; then
       LISTGITHOST="$(get_remote_repo_list)"
fi

# if set it can be tag or branch name
case "$1" in
        -*)
            fatal "Unknown option $1"
        ;;
        "")
            CURRENTBRANCH=$(get_current_branch)
        ;;
        *)
            CURRENTBRANCH="$1"
            shift
        ;;
esac

[ -n "$CURRENTBRANCH" ] || fatal "Can't detect current branch"
}


tune_girarlist $LISTARGS

if [ -n "$PUSHALLBRANCHES" ] ; then
	TEXTBRANCH="all branches"
	CURRENTBRANCH="--all"
else
	TEXTBRANCH="branch $CURRENTBRANCH"
	if git tag | grep -q "^$CURRENTBRANCH\$" ; then
		TEXTBRANCH="tag $CURRENTBRANCH"
	fi
fi

[ -n "$LISTGITHOST" ] || fatal "There are no known git aliases (you can use git.* or pub.*, or origin if alone). Try $ gremote and $ gpush ALIAS"

set_merge_ff

for i in $LISTGITHOST ; do
	push_to_remote $i || fatal "Failed to push. Fix it and run the command again."
done
