#!/bin/sh -e
#
# $Id: rebuild,v 1.21 2003/11/02 15:26:54 ldv Exp $
# Copyright (C) 2003  Dmitry V. Levin <ldv@altlinux.org>
# 
# The rebuild 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
rebuild - rebuilds source packages.

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

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

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

Valid options are:
--no-stuff                      do not generate apt hash files;
--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];
--target=ARCH                   target architecture;
--args=ARGS                     extra arguments for rpmbuild;
--nodeps                        ignore package dependencies (dangerous);
--number=NUMBER                 subconfig identifier;
--hasher-priv-dir=DIR           hasher-priv directory;
-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-sisyphus-check::,no-sisyphus-check-in::,no-sisyphus-check-out::,target:,args:,nodeps,number:,hasher-priv-dir:,quiet,verbose,help -- "$@"` || Usage
eval set -- "$TEMP"

nodeps=
rpmargs=
while :; do
	case "$1" in
		--no-stuff) no_stuff=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
			;;
		--target) shift; target="$1"; shift
			;;
		--args) shift; rpmargs="$1"; shift
			;;
		--nodeps) nodeps=--nodeps; shift
			;;
		--number)
			shift
			number="${1##-}"
			[ -z "$(echo -E "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			shift
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"; shift
			;;
		-q|--quiet) quiet=-q; shift
			;;
		-v|--verbose) verbose=-v; shift
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
done

# Exactly two arguments, please.
[ "$#" -ge 2 ] || Usage
set_workdir "$1"
shift

source="$1"
[ -z "${source##/*}" ] || source="$saved_cwd/$source"

sname="${source##*/}"
shift

[ -d "$chroot" ] || Fatal "$chroot: cannot find chroot."

# Find out packager.
packager="$(rpmquery -p --qf '%{PACKAGER}' "$source")"
[ "$packager" != '(none)' ] || packager=
[ -n "$packager" ] || Fatal "$sname: undefined packager"

check_helpers

if [ -z "$nodeps" ]; then
	deps="$(rpmquery -pR -- "$source")"
	deps="$(echo -E "$deps" |grep -v '^rpmlib(' |tr -d [[:blank:]])" ||:
	Verbose "$sname: calculated dependencies: $deps"

	[ -z "$deps" ] || /usr/share/hasher/install \
		${quiet:+$quiet} \
		${verbose:+$verbose} \
		${number:+--number=$number} \
		--hasher-priv-dir="$hasher_priv_dir" \
		"$workdir" $deps
fi

purge_chroot_in
purge_chroot_out
copy_chroot_incoming "$source"

[ -n "$no_sisyphus_check_in" ] ||
	no_sisyphus_check_in="$no_sisyphus_check"
if [ "$no_sisyphus_check_in" != all ]; then
	[ -n "$no_sisyphus_check_in" ] ||
		no_sisyphus_check_in=gpg
	sisyphus_check --no-check="$no_sisyphus_check_in" -- chroot/.in &&
		Verbose "$source: sisyphus_check passed." ||
		Fatal "$source: sisyphus_check failed."
fi

cat >"$entry" <<__EOF__
$entry_header

cat >>/usr/src/.rpmmacros <<\EOF
%packager $packager
EOF

f="$(quote_arg "$sname")"
nice time rpmbuild --rebuild $nodeps --target="$(quote_arg "$target")" $rpmargs "\$f"
find /usr/src/RPM/SRPMS/ -mindepth 1 -maxdepth 1 -type f -name \*.src.rpm -print0 |
	xargs -r0 mv --reply=yes $verbose --target-directory=/.out/ --
find /usr/src/RPM/RPMS/ -mindepth 2 -maxdepth 2 -type f -name \*.rpm -print0 |
	xargs -r0 mv --reply=yes $verbose --target-directory=/.out/ --
__EOF__
chmod 755 "$entry"

"$chrootuid2" $number "$chroot" /.host/entry &&
	Verbose "rebuild of \`$sname' complete." ||
	Fatal "rebuild of \`$sname' failed."

make_repo

[ -n "$no_sisyphus_check_out" ] ||
	no_sisyphus_check_out="$no_sisyphus_check"
if [ "$no_sisyphus_check_out" != all ]; then
	[ -n "$no_sisyphus_check_out" ] &&
		no_sisyphus_check_out="$no_sisyphus_check_out,gpg" ||
		no_sisyphus_check_out=gpg
	sisyphus_check --no-check="$no_sisyphus_check_out" -- chroot/.out &&
		Verbose "$source: sisyphus_check passed." ||
		Fatal "$source: sisyphus_check failed."
fi

case `find chroot/.out/ -mindepth 1 -maxdepth 1 -type f -name \*.src.rpm |grep -c '\.src\.rpm$'` in
	0)
		find chroot/.in/ -mindepth 1 -maxdepth 1 -type f -name \*.src.rpm -print0 |
			xargs -r0 mv --reply=yes $verbose --target-directory=repo/SRPMS.hasher/ --
		;;
	1)
		find chroot/.out/ -mindepth 1 -maxdepth 1 -type f -name \*.src.rpm -print0 |
			xargs -r0 mv --reply=yes $verbose --target-directory=repo/SRPMS.hasher/ --
		;;
	*)
		Fatal "too many src.rpm files written."
		;;
esac
find chroot/.out/ -mindepth 1 -maxdepth 1 -type f -name \*.rpm -print0 |
	xargs -r0 mv --reply=yes $verbose --target-directory=repo/"$target"/RPMS.hasher/ --

purge_chroot_in
purge_chroot_out

update_repo
