#!/bin/sh -efu
#
# Copyright (C) 2003  Anton Farygin <rider@altlinux.org>
# Copyright (C) 2004-2008  Sergey Vlasov <vsu@altlinux.org>
#
# Build the kernel-image package from the git repository.
#
# 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.
#

TOP="$(readlink -ev .)"

. kernel-build-sh-functions

print_version()
{
	cat <<EOF
$PROG version $PROG_VERSION

Copyright (C) 2003  Anton Farygin <rider@altlinux.org>
Copyright (C) 2004-2008  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
}

show_help()
{
	cat <<EOF
$PROG - build the kernel-image package.

Usage: $PROG [options] <flavour>
   or: $PROG [options] -t <commit>

Options:
  --hasher                    use hasher to build packages;
  --hsh-options=OPTIONS       pass additional options to hsh;
  --hsh-workdir=DIR           specify another working directory for hsh;
  --no-install                do not install packages into fake root;
  --rpmbuild-options=OPTIONS  pass additional options to rpmbuild;
  --target=ARCH               specify target architecture;
  -t, --tree-ish=ID           COMMIT[:PATH] specifying the tree to process;
  -V, --version               print program version and exit;
  -h, --help                  show this text and exit.

EOF
	exit
}

TEMP=`getopt -n "$PROG" -o t:,V,h -l hasher,hsh-options:,hsh-workdir:,no-install,rpmbuild-options:,target:,tree-ish:,version,help -- "$@"` ||
	show_usage
eval set -- "$TEMP"

opt_hasher=
opt_install=t
main_tree_id=
while :; do
	case "$1" in
		--) shift; break
			;;
		--hasher) opt_hasher=t
			;;
		--hsh-options) shift; hsh_options="$1"
			;;
		--hsh-workdir) shift; hsh_workdir="$1"
			;;
		--no-install) opt_install=
			;;
		--rpmbuild-options) shift; rpmbuild_options="$1"
			;;
		--target) shift; rpm_target="$1"
			;;
		-t|--tree-ish) shift; main_tree_id="$1"
			;;
		-V|--version) print_version
			;;
		-h|--help) show_help
			;;
		*) fatal "Unrecognized option: $1"
			;;
	esac
	shift
done

finish_setup

if [ -n "$main_tree_id" ]; then
	[ $# -eq 0 ] || show_usage "Too many arguments"
else
	[ $# -ge 1 ] || show_usage "Not enough arguments"
	[ $# -le 1 ] || show_usage "Too many arguments"
	main_tree_id="kernel-image-$1"
fi

make_rpm_dirs
[ -n "$opt_hasher" ] || [ -z "$opt_install" ] || install_packages

if [ -n "$opt_hasher" ]; then
	cmd="GIT_DIR=$(shell_quote_args "$TOP/kernel/.git") $(
		shell_quote_args gear --hasher -t "$main_tree_id" --
	) hsh --repackage-source $hsh_options $(
		shell_quote_args --target "$rpm_target" "$hsh_workdir"
	)"
else
	cmd="GIT_DIR=$(shell_quote_args "$TOP/kernel/.git") $(
		shell_quote_args gear --rpmbuild -t "$main_tree_id" --
	) $RPMBUILD $(
		shell_quote_args \
			--dbpath "$rpm_rootdir/var/lib/rpm" \
			--define "_usrsrc $rpm_rootdir/usr/src" \
			-ba
	)"
fi

eval "$cmd"
