#!/bin/sh -e
#
# $Id: hsh,v 1.29 2003/11/26 09:55:06 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The hsh utility for the hasher project
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

. /usr/share/hasher/functions

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
hsh - builds package using hasher utilities.

This program is free software, covered by the GNU General Public License.
hsh comes with ABSOLUTELY NO WARRANTY, see license for details.

Usage: $PROG [options] <path-to-workdir> <package>...

<path-to-workdir> must be valid writable directory.

Valid options are:
--no-stuff                      do not install built packages;
--no-cache                      do not use initroot cache;
--no-contents-indices           do not use content indices;
--no-sisyphus-check[=LIST]      do not run sisyphus_check tests [specified in LIST];
--no-sisyphus-check-in[=LIST]   do not run sisyphus_check input tests [specified in LIST];
--no-sisyphus-check-out[=LIST]  do not run sisyphus_check output tests [specified in LIST];
--repackage-source              package source as well as binaries;
--target=ARCH                   target architecture;
--number=NUMBER                 subconfig identifier;
--hasher-priv-dir=DIR           hasher-priv directory;
--nprocs=NUMBER                 number of CPUs to use;
--install-langs=LANGLIST        list of languages to install;
--build-args=ARGS               extra arguments for rpmbuild;
--contents-index-bin=INDEX      pkg contents binary index;
--contents-index-all=INDEX      pkg contents complete index;
--apt-config=FILE               path to custom apt.conf file;
-q, --quiet                     try to be more quiet;
-v, --verbose                   print a message for each action;
-h, --help                      show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o q,v,h -l no-stuff,no-cache,no-contents-indices,no-sisyphus-check::,no-sisyphus-check-in::,no-sisyphus-check-out::,repackage-source,target:,number:,hasher-priv-dir:,nprocs:,install-langs:,build-args:,contents-index-all:,contents-index-bin:,apt-config:,quiet,verbose,help -- "$@"` || Usage
eval set -- "$TEMP"

no_cache=
no_contents_indices=
nprocs=
rpmargs=
install_langs=
contents_index_all=
contents_index_bin=
repackage_source=
apt_config=
while :; do
	case "$1" in
		--no-stuff) no_stuff=1; shift
			;;
		--no-cache) no_cache=1; shift
			;;
		--no-contents-indices) no_contents_indices=1; shift
			;;
		--no-sisyphus-check)
			shift
			[ -n "$1" ] && no_sisyphus_check="$1" || no_sisyphus_check=all
			shift
			;;
		--no-sisyphus-check-in)
			shift
			[ -n "$1" ] && no_sisyphus_check_in="$1" || no_sisyphus_check_in=all
			shift
			;;
		--no-sisyphus-check-out)
			shift
			[ -n "$1" ] && no_sisyphus_check_out="$1" || no_sisyphus_check_out=all
			shift
			;;
		--repackage-source) repackage_source=1; shift
			;;
		--target) shift; target="$1"; shift
			;;
		--build-args) shift; rpmargs="$1"; shift
			;;
		--number)
			shift
			number="${1##-}"
			[ -z "$(echo -E "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			shift
			;;
		--nprocs)
			shift
			nprocs="$1"
			[ -z "$(echo -E "$nprocs" |tr -d [0-9])" ] ||
				Fatal "--nprocs: $nprocs: invalid number."
			shift
			;;
		--install-langs) shift; install_langs="$1"; shift
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"; shift
			;;
		--contents-index-all) shift; contents_index_all="$1"; shift
			;;
		--contents-index-bin) shift; contents_index_bin="$1"; shift
			;;
		--apt-config) shift; apt_config="$1"; shift
			[ -r "$apt_config" ] || Fatal "$apt_config: cannot access."
			;;
		-q|--quiet) quiet=-q; shift
			;;
		-v|--verbose) verbose=-v; shift
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
done

# At least two arguments, please.
[ "$#" -ge 2 ] || Usage

set_workdir "$1"
shift

for f in "$@"; do
	[ -z "${f##/*}" ] || f="$saved_cwd/$f"
	[ -r "$f" ] ||
		Fatal "${f##*/}: cannot access source"
	[ -s "$f" ] ||
		Fatal "${f##*/}: broken source"
done

exit_handler()
{
	local rc=$?
	trap - EXIT
	rm -rf -- lockdir
	exit $rc
}

pidfile="lockdir/pid"
if ! mkdir -m700 $verbose -- lockdir; then
	[ ! -s "$pidfile" ] ||
		Info "working directory \`$workdir' is locked, pid=`cat "$pidfile"`"
	Fatal "unable to create lock directory"
fi

trap exit_handler SIGHUP SIGPIPE SIGINT SIGQUIT SIGTERM EXIT

echo $$ >"$pidfile"


for f in "$@"; do
	[ -z "${f##/*}" ] || f="$saved_cwd/$f"

	rm -rf aptbox
	$hasher_dir/mkaptbox "$workdir" \
			--update \
			--target="$target" \
			${apt_config:+--apt-config="$apt_config"} \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${no_stuff:+--no-stuff} \
			#
	[ ! -d chroot ] ||
		$hasher_dir/rmchroot "$workdir" \
			--hasher-priv-dir="$hasher_priv_dir" \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${number:+--number=$number} \
			#
	$hasher_dir/mkchroot "$workdir" \
			--hasher-priv-dir="$hasher_priv_dir" \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${number:+--number=$number} \
			#
	$hasher_dir/initroot "$workdir" \
			--hasher-priv-dir="$hasher_priv_dir" \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${no_cache:+--no-cache} \
			${no_contents_indices:+--no-contents-indices} \
			${repackage_source:+--repackage-source} \
			${number:+--number=$number} \
			${nprocs:+--nprocs=$nprocs} \
			${install_langs:+--install-langs="$install_langs"} \
			${contents_index_all:+--contents-index-all=$contents_index_all} \
			${contents_index_bin:+--contents-index-all=$contents_index_bin} \
			#
	$hasher_dir/rebuild "$workdir" \
			--target="$target" \
			--hasher-priv-dir="$hasher_priv_dir" \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${no_stuff:+--no-stuff} \
			${no_sisyphus_check:+--no-sisyphus-check="$no_sisyphus_check"} \
			${no_sisyphus_check_in:+--no-sisyphus-check-in="$no_sisyphus_check_in"} \
			${no_sisyphus_check_out:+--no-sisyphus-check-out="$no_sisyphus_check_out"} \
			${number:+--number=$number} \
			${rpmargs:+--args="$rpmargs"} \
			"$f"
done
