#!/bin/sh -e
#
# $Id: rebuild,v 1.44 2004/08/26 09:09:23 ldv Exp $
# Copyright (C) 2003, 2004  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.
#

if [ "$0" = ./rebuild ]; then
	. ./functions
	hasher_dir="$(/bin/pwd)"
else
	. /usr/share/hasher/functions
fi

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
rebuild - rebuilds source packages.

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];
  --rebuild-prog=PROG               program to run for rebuild instead of autogenerated script;
  --target=ARCH                     target architecture;
  --args=ARGS                       extra arguments for rpmbuild;
  --mountpoints=LIST                list of known mount points;
  --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;
  -V, --version                     print program version and exit;
  -h, --help                        show this text.
EOF
	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o q,v,V,h -l no-stuff,no-sisyphus-check::,no-sisyphus-check-in::,no-sisyphus-check-out::,rebuild-prog:,target:,args:,mountpoints:,nodeps,number:,hasher-priv-dir:,quiet,verbose,help,version -- "$@"` || Usage
eval set -- "$TEMP"

nodeps=
rpmargs=
prog_rebuild=
known_mountpoints=
required_mountpoints=
while :; do
	case "$1" in
		--no-stuff) no_stuff=1
			;;
		--no-sisyphus-check) shift
			[ -n "$1" ] && no_sisyphus_check="$1" || no_sisyphus_check=all
			;;
		--no-sisyphus-check-in) shift
			[ -n "$1" ] && no_sisyphus_check_in="$1" || no_sisyphus_check_in=all
			;;
		--no-sisyphus-check-out) shift
			[ -n "$1" ] && no_sisyphus_check_out="$1" || no_sisyphus_check_out=all
			;;
		--rebuild-prog) shift; prog_rebuild="$1"
			;;
		--target) shift; target="$1"
			[ -z "${target##[A-Za-z]*}" ] ||
				Fatal "--target: $target: invalid architecture."
			;;
		--args) shift; rpmargs="$1"
			;;
		--mountpoints) shift; known_mountpoints="$1"
			;;
		--nodeps) nodeps=--nodeps
			;;
		--number) shift; number="${1##-}"
			[ -z "$(printf %s "$number" |tr -d [0-9])" ] ||
				Fatal "--number: $number: invalid number."
			number="-$number"
			;;
		--hasher-priv-dir) shift; hasher_priv_dir="$1"
			;;
		-q|--quiet) quiet=-q
			;;
		-v|--verbose) verbose=-v
			;;
		-V|--version) print_version rebuild
			;;
		-h|--help) Usage 0
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
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."

check_helpers

# Create rebuild script.
if [ -n "$prog_rebuild" ]; then
	install -p -m755 $verbose "$prog_rebuild" chroot/.host/rebuild
else
	cat >"chroot/.host/rebuild" <<__EOF__
#!/bin/sh -le
exec nice time rpmbuild --rebuild $nodeps --target="$(quote_arg "${target:-$def_target}")" $rpmargs "\$@"
__EOF__
	chmod 755 "chroot/.host/rebuild"
	Verbose "created rebuild script."
fi

# Copy source to chroot.
copy_chroot_incoming "$source"

# Execute sisyphus_check.
[ -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

	cat >"$entry" <<__EOF__
$entry_header

mkdir -p $verbose -m700 -- "\$TMPDIR"
sisyphus_check --no-check="$(quote_arg "$no_sisyphus_check_in")" /.in
__EOF__
	chmod 755 "$entry"

	wlimit_time_elapsed=300 wlimit_time_idle=300 wlimit_bytes_written=65536 \
	"$chrootuid2" $number "$chroot" /.host/entry &&
		Verbose "$sname: sisyphus_check passed." ||
		Fatal "$sname: sisyphus_check failed."
fi

# Calculate, check and install build dependencies.
if [ -z "$nodeps" ]; then
	cat >"$entry" <<__EOF__
$entry_header

rpmquery -pR -- "$(quote_arg "$sname")"
__EOF__
	chmod 755 "$entry"

	deps="$(wlimit_time_elapsed=60 wlimit_time_idle=60 wlimit_bytes_written=65536 \
		"$chrootuid2" $number "$chroot" /.host/entry)" &&
		Verbose "$sname: fetched build dependencies." ||
		Fatal "$sname: failed to fetch build dependencies."
	deps="$(printf %s "$deps" |grep -v '^rpmlib(' |LC_ALL=C tr -d [[:blank:]])"
	if bad_deps="$(printf %s "$deps" |LC_ALL=C grep -v '^[[:alnum:]_/]')"; then
		Fatal "$sname: invalid build dependencies: $bad_deps"
	fi
	Verbose "$sname: calculated build dependencies: $deps"

	if [ -n "$known_mountpoints" ]; then
		for file_deps in `printf %s "$deps" |LC_ALL=C grep '^/' |LC_ALL=C sort -u`; do
			for mpoint in $known_mountpoints; do
				if [ "$mpoint" = "$file_deps" ]; then
					[ -z "$required_mountpoints" ] &&
						required_mountpoints="$mpoint" ||
						required_mountpoints="$required_mountpoints,$mpoint"
					break
				fi
			done
		done
		Verbose "$sname: calculated mount points: $required_mountpoints"
	fi

	if [ -n "$deps" ]; then
		"$hasher_dir/install" \
			${quiet:+$quiet} \
			${verbose:+$verbose} \
			${number:+--number=$number} \
			${hasher_priv_dir:+--hasher-priv-dir="$hasher_priv_dir"} \
			-- \
			"$workdir" $deps

		copy_chroot_incoming "$source"
	fi
fi

# Calculate required mountpoints from installed file dependencies.
if [ -z "$nodeps" -a -n "$known_mountpoints" ]; then
	cat >"$entry" <<__EOF__
$entry_header

rpmquery -aR |grep ^/ |sort -u
__EOF__
	chmod 755 "$entry"

	deps="$(wlimit_time_elapsed=60 wlimit_time_idle=60 wlimit_bytes_written=65536 \
		"$chrootuid2" $number "$chroot" /.host/entry)" &&
		Verbose "$sname: fetched installed file dependencies." ||
		Fatal "$sname: failed to fetch installed file dependencies."
	deps="$(printf %s "$deps" |grep ^/ |LC_ALL=C tr -d [[:blank:]])"

	for file_deps in $deps; do
		for mpoint in $known_mountpoints; do
			if [ "$mpoint" = "$file_deps" ]; then
				[ -z "$required_mountpoints" ] &&
					required_mountpoints="$mpoint" ||
					required_mountpoints="$required_mountpoints,$mpoint"
				break
			fi
		done
	done
	required_mountpoints="$(printf %s "$required_mountpoints" |tr -s , '\n' |LC_ALL=C sort -u |tr -s '\n' ,)"
	required_mountpoints="${required_mountpoints%,}"
	Verbose "$sname: calculated mount points: $required_mountpoints"
fi

# Build the package.
purge_chroot_out

cat >"$entry" <<__EOF__
$entry_header

mkdir -p $verbose -m700 -- "\$TMPDIR"
cat >>/usr/src/.rpmmacros <<EOF
%packager \$(rpmquery -p --qf '%{PACKAGER}' "$(quote_arg "$sname")")
EOF

/.host/rebuild "$(quote_arg "$sname")"
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"

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

# Execute sisyphus_check.
[ -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
	cat >"$entry" <<__EOF__
$entry_header

sisyphus_check --no-check="$(quote_arg "$no_sisyphus_check_out")" /.out
__EOF__

	wlimit_time_elapsed=300 wlimit_time_idle=300 wlimit_bytes_written=65536 \
	"$chrootuid2" $number "$chroot" /.host/entry &&
		Verbose "$sname: sisyphus_check passed." ||
		Fatal "$sname: sisyphus_check failed."
fi

# Copy binaries from chroot.
make_repo

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:-$def_target}"/RPMS.hasher/ --

purge_chroot_in
purge_chroot_out

update_repo
