#!/bin/sh -efu
#
# Copyright (C) 2008  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2008  Dmitry V. Levin <ldv@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
. shell-getopt

cmd=
show_help()
{
	cat <<-EOF
	Usage: $PROG [options] [$cmd options] [gear options]

	Options:
	  -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://bugzilla.altlinux.org/

	EOF
	exit
}

print_version()
{
	cat <<-EOF
	$PROG version $PROG_VERSION
	Written by Alexey Gladkov <legion@altlinux.org>

	Copyright (C) 2008  Alexey Gladkov <legion@altlinux.org>
	Copyright (C) 2008  Dmitry V. Levin <ldv@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
}

args='--command -- gear-command'
case "$PROG" in
	gear-hsh)
		cmd='hasher'
		args="$args-$cmd hsh"
		;;
	gear-rpm)
		cmd='rpmbuild'
		args="$args-$cmd rpmbuild"
		;;
	gear-buildreq)
		cmd='rpmbuild'
		args="$args-$cmd buildreq"
		;;
	*)
		fatal "Unknown command: $PROG"
		;;
esac

GETOPT_ALLOW_UNKNOWN=1
GETOPT_ALLOW_ABBREV=
TEMP=`getopt -n $PROG -o "$gear_short_options" -l "$gear_long_options" -- "$@"` ||
	show_usage
eval set -- "$TEMP"

gear_args=
while :; do
	case "$1" in
		-h|--help) show_help
			;;
		-V|--version) print_version
			;;
		-v|--verbose) verbose=-v
			;;
		--) shift; break
			;;
	esac
	gear_args="$gear_args \"$(quote_shell "$1")\""
	shift
done

while [ $# -gt 0 ]; do
	args="$args \"$(quote_shell "$1")\""
	shift
done

eval exec gear $gear_args $args
