#!/bin/sh -efu
#
# Copyright (C) 2009  Alexey I. Froloff <raorn@altlinux.org>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

. girar-client-sh-functions

show_help()
{
	cat <<EOF
Usage: $PROG [Options] <repository> <package> [<target branch>]

$PROG imports package from girar server archive.

$PROG uses the git configuration file. The following variables are read:

 * girar.remote, corresponding to --remote;

Options:
  -R,--remote=NAME    girar server alias, defaults to git.alt;
  -p,--people[=DIR]   import to your remote git.alt:DIR, too
                      (and set it up as origin);
  -G,--gears          only try /gears/ hierarchy;
  -S,--srpms          only try /srpms/ hierarchy;
  -u,--update         update into current repository;

  -q,--quiet          try to be more quiet;
  -v,--verbose        print a message for each action;
  -V,--version        print program version and exit;
  -h,--help           show this text and exit.

Report bugs to http://bugs.altlinux.ru/

EOF
	exit
}

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION
Written by Alexey I. Froloff <raorn@altlinux.org>

Copyright (C) 2009  Alexey I. Froloff <raorn@altlinux.org>
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
	exit
}

TEMP=`getopt -n $PROG -o R:,p::,G,S,u,q,v,V,h \
             -l remote:,people::,gears,srpms,update,quiet,verbose,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

people=
# Among other uses, the optional $people_dir is useful when the
# package name contains bad symbols (e.g., gtk+)
people_dir=
update=
gears=1
srpms=1
while :; do
	case "$1" in
		-R|--remote) shift; girar_remote="$1";;
	        -p|--people) people=1; shift; people_dir="$1";;
		-G|--gears) srpms=;;
		-S|--srpms) gears=;;
		-u|--update) update=1;;
		-q|--quiet) quiet=-q;;
		-v|--verbose) verbose=-v;;
		-V|--version) print_version;;
		-h|--help) show_help;;
		--) shift; break;;
		*) fatal "unrecognized option: $1";;
	esac
	shift
done

[ $# -eq 2 -o $# -eq 3 ] || show_usage
[ -n "$gears$srpms" ] ||
	fatal "--gears and --srpms are mutually exclusive"

repo="$1" && shift
package="$1" && shift
branch="${1-master}"

ackage="${package#?}"
p="${package%$ackage}"

if [ -z "$update" ]; then
	if git rev-parse --git-dir >/dev/null 2>&1; then
		message "You are already inside git repository, maybe you wish to use --update?"
		sleep 3
	fi

	mkdir "$package" ||
		fatal "Unable to create directory \`$package'"

	cd "$package" ||
		fatal "Unable to change directory to \`$package'"

	git init ||
		fatal "Unable to initialize git repository"
fi

rc=1
if [ -n "$gears" ]; then
	girar_repo="/gears/$p/$package.git"
	rc=0
	git fetch $verbose $quiet -u "$girar_remote:$girar_repo" "$repo:$branch" ||
		rc=1
fi

if [ $rc != 0 -a -n "$srpms" ]; then
	girar_repo="/srpms/$p/$package.git"
	rc=0
	git fetch $verbose $quiet -u "$girar_remote:$girar_repo" "$repo:$branch" ||
		rc=1
fi

[ $rc = 0 ] ||
	fatal "Unable to fetch branch \`$repo' from archive"

if [ -n "$people" ]; then
    if ! run_remote_command clone "$girar_repo" ${people_dir:+"$people_dir"}; then
        message 'Remote clone command failed.'
    else
	people_validpath="$people_dir"
	if [ -z "$people_validpath" ]; then
	    people_validpath="$package"
	fi
	# Unwinding the usual git.alt's default shortening:
	case "$people_validpath" in
	    *.git) ;;
	    *) people_validpath="$people_validpath".git ;;
	esac
	case "$people_validpath" in
	    */*) ;;
	    *) people_validpath=packages/"$people_validpath" ;;
	esac
	git remote add origin "$girar_remote:$people_validpath" &&
	{
	    git fetch origin "$repo"
	    git push -u origin "$branch" &&
	    ssh "$girar_remote" default-branch "${people_dir:-$package}" "$branch"
	} ||
	message 'Syncing refs in your new local and new remote people repos failed.'
    fi
fi

git checkout $quiet "$branch" ||
	fatal "Unable to checkout branch \`$repo'"
