#!/bin/bash
#
# Copyright (C) 2008-2014  Etersoft
# Copyright (C) 2008-2014  Vitaly Lipatov <lav@etersoft.ru>
# Copyright (C) 2008        Denis Smirnov <mithraen@altlinux.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/>.
#

# copied from etersoft-build-utils/share/eterbuild/functions/git

get_branch_list()
{
	git branch | grep '^ ' | sed 's/^..\(.*\)/\1/'
}

get_remote_branch_list()
{
	git branch -a | grep remotes | grep -v HEAD | grep -v master
}

get_remote_repo_list()
{
	git remote "$@" 2>/dev/null | sed -e "s|(fetch)$||" | sed -e "s|(push)$||" | sort -u

}

is_exist_branch()
{
	test -n "$1" || return 1
	get_branch_list | grep -q $1
}

is_exist_remote_repo()
{
	test -n "$1" || return 1
	get_remote_repo_list | grep -q $1
}

get_current_branch()
{
	git branch | grep '^\*' | sed 's/^..//' | tr -d "\n"
}

get_remote_git_url()
{
	git config --get "remote.$1.url"
}

# get name from current remote repository name
get_remote_repo_name()
{
    local branch remote url basename
    url="$(get_remote_git_url $GITHOST)"

    if [ -z "$url" ] ; then
        # any git.*** from git remote has girar like path
        local anygirar
        for anygirar in $(get_remote_git_list) ; do
             url="$(get_remote_git_url $anygirar)"
             rhas "$url" "(packages|srpms|gears)/" && break
             url=''
        done
    fi

    # TODO: try get from spec name?

    if [ -z "$url" ] ; then
        # repo dir name
        url="$(get_gear_name)"
    fi

    if [ -z "$url" ] ; then
        # remote url for the current branch
        branch="$(get_current_branch)"
        [ -n "$branch" ] || return
        remote="$(git config --get branch.$branch.remote)"
        url="$(get_remote_git_url $remote)"
    fi

    [ -n "$url" ] || return
    basename=$(basename "$url" .git | filter_gear_name)
    echo "$basename"
    [ -n "$basename" ]
}

get_repo_name()
{
    # try use remote repo name firstly
    get_remote_repo_name && return
    # use repo dir name in other way
    #get_gear_name && return
    # get name from spec
    #build_rpms_name "$LISTNAMES"
    #PROJECTNAME=$(echo $BASENAME | filter_gear_name)
}

# TODO: rewrite with estrlist (does no matter, one or many
get_remote_git_list()
{
        local i
        for i in $(get_remote_repo_list) ; do
                if is_girar_name "$i" ; then
                        [ "$1" = "-v" ] && echo "$i $(get_remote_git_url "$i")" && continue
                        echo "$i"
                fi
        done
}

# TODO: rewrite with estrlist
get_remote_pub_list()
{
        local i
        for i in $(get_remote_repo_list) ; do
                if is_pub_name "$i" ; then
                        [ "$1" = "-v" ] && echo "$i $(get_remote_git_url "$i")" && continue
                        echo "$i"
                fi
        done
}


# Check if $1 like git.alt or git.eter, git.something
is_girar_name()
{
	rhas "$1" "^git\." && return
	rhas "$1" "^gitery" && return
}

is_pub_name()
{
	rhas "$1" "^pub\."
}

is_one_girar_name()
{
	local i
	local RES=""
	for i in $@ ; do
		[ -z "$RES" ] || return
		is_girar_name "$i" || return
		RES="$i"
	done
	[ -n "$RES" ]
}

# Try autodetect GITHOST. Return true, if get it from arg (need for shift args)
set_git_host()
{
	if is_girar_name "$1" ; then
		GITHOST="$1"
		return 0
	fi

	# Try get from remote list, if unique record there
	REMOTELIST="$(get_remote_git_list)"
	if is_one_girar_name "$REMOTELIST" ; then
		# use one target if it one
		GITHOST="$REMOTELIST"
		return 1
	fi

	# use default from config
	GITHOST="$GIRARHOST"

	# if no default, set default GITHOST from ~/.ssh/config
	if [ -z "$GITHOST" ] ; then
		GITHOST=$(get_girar_host_from_ssh)
		#[ -n "$GIRARHOST" ] || fatal "Can't get default girar alias (like git.alt) from ~/.ssh/config"
	fi

	return 1
}

set_gear_host()
{
	#assert_var GITHOST
	GEARHOST="$GITHOST"
	# https://www.altlinux.org/Git.alt/Справочник#SSH-доступ
	[ "$GEARHOST" = "git.alt" ] && GEARHOST="gear.alt"
	[ "$GEARHOST" = "gitery" ] && GEARHOST="gyle"
	return 0
}

# set hosts vars (by the arg if host in git.* style)
set_work_hosts()
{
	local RES
	set_git_host "$1"
	RES=$?
	set_gear_host
	return $RES
}

# set hosts vars by non empty arg
force_work_hosts()
{
	local RES=0
	case "$1" in
		# if the arg is empty or an option, try common way
		-*|--*|"")
			GITHOST=
			#set_git_host ""
			RES=1
			;;
		*)
			GITHOST="$1"
			;;
	esac
	set_gear_host
	return $RES
}

git_commit_ignore_nothing()
{
        git commit "$@" && return
        git commit "$@" 2>&1 | grep "nothing to commit" && return 0
        return 1
}

get_last_tag()
{
        git describe --abbrev=0 --tags 2>/dev/null
}

# check if tag is last commit tag (put on the last commit). if tag is missed, check with the last tag in repo
is_last_commit_tag()
{
        local TAG=$1
        test -n "$TAG" || TAG=$(get_last_tag)
        [ -n "$TAG" ] || return
        # check if the tag put on the last commit
        [ "$(git rev-parse HEAD)" = "$(git rev-parse $TAG^0)" ]
}

set_merge_ff()
{
	# only if work with repo from /projects (shared work)
	rhas "$(get_remote_repo_list -v)" ":/projects" || return
	# if have not merge.ff setting, set only
	[ -n "$(git config --local merge.ff)" ] || docmd git config --local merge.ff only
}

# copied from rooter
show_alias()
{	local ALIAS="$1"
	ssh -G "$ALIAS"
}
