#!/bin/sh -efu
#
# Copyright (C) 2007-2009  Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2007-2008  Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2008  Alexey I. Froloff <raorn@altlinux.org>
#
# gear-create-tag - create a signed release tag object.
#
# 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

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

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

show_help()
{
	cat <<EOF
$PROG - create a signed release tag object for the given commit object

Usage: $PROG [commit-ish]

Options:
  -f, --force           replace an existing tag (instead of failing);
  -u, --key-id=KEYID    use the given GPG key to create a signed tag;
  -m, --message=TEXT    override custom tag message
                        (default: <NAME> <VERSION>-<RELEASE>);
  -n, --name=TEXT       override custom tag name
                        (default: <VERSION>-<RELEASE>);
  -r, --rules=FILENAME  override rules file name;
  -t, --time=DATE-FMT   set the date used in tag;
  -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
}

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

TEMP=`getopt -n $PROG -o f,m:,n:,r:,t:,u:,v,V,h -l force,key-id:,message:,name:,rules:,time:,verbose,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

force=
keyid=
opt_s='-s'
gear_config_option tag_name name '@version@-@release@'
gear_config_option tag_msg message '@name@ @version@-@release@'
main_tree_id='HEAD'
while :; do
	case "$1" in
	    -f|--force) force=-f
		;;
	    -m|--message) shift; tag_msg="$1"
		;;
	    -n|--name) shift; tag_name="$1"
		;;
	    -r|--rules) shift; rules="$1"
		;;
	    -t|--time) shift
		[ -z "$1" ] ||
			export GIT_COMMITTER_DATE="$1"
		;;
	    -u|--key-id) shift; keyid="$1"; opt_s=
		;;
	    -v|--verbose) verbose=-v
		;;
	    -V|--version) print_version
		;;
	    -h|--help) show_help
		;;
	    --) shift; break
		;;
	    *) fatal "unrecognized option: $1"
		;;
	esac
	shift
done

if [ "$#" -gt 0 ]; then
	main_tree_id="$1"
	shift
fi

[ "$#" -eq 0 ] ||
	show_usage 'Too many arguments.'

chdir_to_toplevel

main_commit_sha1="$(get_commit_sha1 "$main_tree_id")" ||
	fatal "$main_tree_id: Invalid commit object name"

install_cleanup_handler cleanup_handler
workdir="$(mktemp -dt "$PROG.XXXXXXXXXX")"
find_specfile
rm -rf -- "$workdir"
uninstall_cleanup_handler

subst_NVR_from_spec_file '' tag_name tag_msg

verbose "Creating tag named \"$tag_name\" using tag message \"$tag_msg\" for commit $main_commit_sha1"
exec git tag $force $opt_s ${keyid:+-u "$keyid"} -m "$tag_msg" "$tag_name" "$main_commit_sha1"
