#!/bin/sh
#
# Copyright (C) 2014  Etersoft
# Copyright (C) 2014  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/giter-sh-functions

check_tty

estrlist()
{
        $PROGDIR/giter-estrlist "$@"
}

# Args: LIST1 LIST2
# do_exclude_list print LIST2 exclude fields contains also in LIST1
# Example: do_exclude_list "1 3" "1 2 3 4" -> "2 4"
do_exclude_list()
{
        estrlist exclude "$@"
}

# TODO: move to git (in etersoft-build-utils too)
# TODO: move to separate command
get_root_git_dir()
{
        local DIR=$(git rev-parse --git-dir 2>/dev/null)/../ || return
        readlink -f "$DIR" 2>/dev/null || realpath "$DIR"
        # FIXME: 1) can return / with good status 2) no one check status later
}

# TODO: move to separate gear file
# TODO: rewrite with get_gear_rules_by_spec in mind
get_gear_rules()
{
	local DIR="$1"
	[ -z "$IGNOREGEAR" ] || return 1
	[ -f "$DIR" ] && DIR=$(dirname "$DIR")
	[ -n "$DIR" ] && cd $DIR
	local GITDIR=$(get_root_git_dir)
	[ -n "$DIR" ] && cd - >/dev/null
	local rules="$GITDIR/.gear/rules"
	[ -r "$rules" ] || rules="$GITDIR/.gear-rules"
	[ -r "$rules" ] || return
	echo $rules
}


# Usage: is_gear [path_to_spec|dir_inside_git]
is_gear()
{
	get_gear_rules $@ >/dev/null
}

filter_gear_name()
{
	sed -e "s|\+|plus|g"
}

# FIXME: use in rpmbs, fix for package name from spec
get_gear_name()
{
	local gitdir=$(get_root_git_dir)
	[ -n "$gitdir" ] || return
	local gearname=$(basename "$gitdir" | filter_gear_name)
	[ "$gearname" = "/" ] && gearname=""
	echo "$gearname"
}

get_girar_host_from_ssh()
{
	grep -i "host[\t ]\+" ~/.ssh/config 2>/dev/null | sed "s|.*host[\t ]*||gi" | grep -m1 "^gitery *$" && return
	grep -i "host[\t ]\+" ~/.ssh/config 2>/dev/null | sed "s|.*host[\t ]*||gi" | grep -m1 "^git\."
}

# load user config file
if [ -f ~/.config/giter ] ; then
	. ~/.config/giter
fi

# TODO: merge with sh-functions
echocon()
{
	tty -s && echo "$@"
}

initial_letter()
{
    echo "$1" | head -n1 | cut -c1
}

# copied from repos
get_git_url()
{
	# TODO: move to giter
	# FIXME: cyclic dependence
	# TODO: fast hack for assure in epm
	[ -f /etc/eterbuild/repos/gitalias ] || epm assure /etc/eterbuild/repos/gitalias etersoft-build-utils </dev/null
	grep "^$1 " /etc/eterbuild/repos/gitalias | cut -f2 -d" "
}

assert_var()
{
	local i re
	for i in $@ ; do
		re=$(eval echo \$$i)
		[ -n "$re" ] || fatal "assert: $i nonexist"
	done
}

# CHECKME: the same like estrlist has ?
# Note: used egrep! write '[0-9]+(first|two)', not '[0-9]\+...'
rhas()
{
	echo "$1" | grep -E -q -- "$2"
}
