#!/bin/sh -efu
#
# Copyright (C) 2006  Alexey Gladkov <legion@altlinux.org>
# 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.
#

. gear-sh-functions
. girar-client-sh-functions

show_help()
{
	cat <<EOF
Usage: $PROG [Options] [<repository> [<refspec>...]]

$PROG upload a new git repository to git.alt server.

$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;
  -o,--origin=NAME    use NAME instead of 'origin' to set up git remote;

  -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 Gladkov <legion@altlinux.org>

Copyright (C) 2006  Alexey Gladkov <legion@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:,o:,q,v,V,h \
             -l remote:,origin:,quiet,verbose,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

origin="origin"
while :; do
	case "$1" in
		-R|--remote) shift; girar_remote="$1";;
		-o|--origin) shift; origin="$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

GIT_DIR="$(git rev-parse --git-dir)"
GIT_DIR="$(readlink -ev "$GIT_DIR")"
export GIT_DIR

if [ "$#" -gt 0 ]; then
	remote_path="$1" && shift
else
	# Poor man's basename(dirname())
	remote_path="${GIT_DIR%.git}"
	remote_path="${remote_path%/}"
	remote_path="${remote_path##*/}"
fi

remote_path="$(remote_repo_path "$remote_path")"

run_remote_command init-db "$remote_path"||
	fatal "unable to initialize remote repository \`$remote_path'"

remote="$girar_remote:$remote_path"

args=--all
refspec=
if [ "$#" -gt 0 ]; then
	args=
	refspec=1
fi

if ! git push $verbose $args "$remote" ${refspec+"$@"}; then
	run_remote_command rm-db "$remote_path"
	exit 1
fi

if ! git config "remote.$origin.url" >/dev/null; then
	git remote add "$origin" "$remote"
fi

message "package uploaded to $remote"
