#!/bin/sh -efu
#
# Copyright (C) 2006-2010  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2006-2008  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2006  Sergey Vlasov <vsu@altlinux.org>
#
# git commit wrapper for gear.
#
# 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

rules="${RULES-}"
main_tree_id=${TREE_ID:-HEAD}
commitlog=

show_help()
{
	cat <<EOF
Usage: $PROG [options] [git-commit options] [--] <filepattern>...

Options:

  --spec=FILE         do not look for specfile in repository,
                      use given FILE instead;
  --no-edit           do not edit the message taken from changelog
                      entry or with -m option;
  -m, --message=TEXT  use given TEXT for commit message;
  -V, --version       print program version and exit;
  -h, --help          show this text and exit.

Report bugs to http://bugzilla.altlinux.org/

EOF
	exit
}

workdir=
cleanup_handler()
{
	[ -z "$workdir" ] || rm -rf -- "$workdir"
}

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

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

GETOPT_ALLOW_UNKNOWN=1
TEMP=`getopt -n $PROG -o 'm:,h,V' -l 'message:,no-edit,spec:,help,version' -- "$@"` || show_usage
eval set -- "$TEMP"

do_edit='--edit'
commitlog=
specfile=
while :; do
	case "$1" in
		--spec) shift
			specfile="$1"
			[ -z "$commitlog" ] ||
				show_usage "Options --spec and --message are mutually exclusive."
			;;
		--no-edit) do_edit=
			;;
		-m|--message) shift
			commitlog="$1"
			[ -z "$specfile" ] ||
				show_usage "Options --spec and --message are mutually exclusive."
			;;
		-h|--help) show_help
			;;
		-V|--version) print_version
			;;
		--) shift; break
			;;
	esac
	shift
done

GEAR_COMMIT=1
GEAR_COMMIT_AMEND=

args=
while [ $# -gt 0 ]; do
	[ "$1" != '--amend' ] ||
		GEAR_COMMIT_AMEND=1

	args="$args \"$(quote_shell "$1")\""
	shift
done

# Change to toplevel directory
chdir_to_toplevel

if [ -z "$specfile" ]; then
	if git cat-file -t "$main_tree_id" >/dev/null 2>&1; then
		install_cleanup_handler cleanup_handler
		workdir="$(mktemp -dt "$PROG.XXXXXXXXXX")"

		# Get specfile from archive.
		find_specfile

		rm -rf -- "$workdir"
		uninstall_cleanup_handler
		unset rules main_tree_id workdir
	else
		specfile="$(set +f; find_specfile_in_cwd '' *)" ||:
	fi
fi

[ -s "$specfile" ] ||
	fatal "$specfile: file not available or empty"

if [ -z "$commitlog" ]; then
	vr="$(sed -ne '/^[[:space:]]*%changelog[[:space:]]*$/{n; s/\* .* \([^[:space:]]\+\)$/\1/p}' "$specfile")"
	ch="$(sed -ne '0,/^[[:space:]]*%changelog[[:space:]]*$/d;2,/^* /{/^* /!p}' "$specfile")"
	[ -z "$vr$ch" ] ||
		commitlog="\"$(quote_shell "$vr

$ch" |sed s/%%/%/g)\""
	unset ch vr specfile
fi

export GEAR_COMMIT GEAR_COMMIT_AMEND

eval git commit ${commitlog:+-m "$commitlog" $do_edit} $args
