#!/bin/bash
# 2003-2006, 2008 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
# ���������� ��������� SRPM-������� � hasher
# -i ������������� � �������� hasher ����� ����������

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/functions/common
load_mod hasher

#############################
Usage="Usage: $name [-m -s -u -i -c] src.rpm..."
function mygetopts()
{
name=${0##*/}
Descr="$name - build package via hasher"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -s - sign package(s)"
	echog " -u - sign and upload package(s) to Incoming"
	echog " -i - install built packages in test hasher"
	echog " -c - build without stuff (previous built packages)"
}

while getopts :hmic opt; do
    case $opt in
    h) phelp; exit 0;;
    i) TESTINSTALL=1;;
	c) HASHERARG="--without-stuff $HASHERARG";;
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
#    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
	?) OPTIND=$((OPTIND-1)); break;
    esac
done
 
# remove args that were options
if [ $# -gt 0 ]; then 
	shift $((OPTIND - 1))
fi

LISTRPMARGS=$@
}


parse_cmd_pre "$@"
mygetopts $LISTARGS

RESULT=0

HASHERDIR=$HASHERDIR$MENVARG
mkdir -p $LOGDIR $HASHERDIR || fatal "cannot mkdir"
check_locking

HSH=/usr/bin/hsh
if [ ! -x "$HSH" ] ; then
	echo "Please install hasher package before use it"
	exit 1
fi

echog "List of packages: \$LISTNAMES"

if [ -z "${LISTNAMES/*spec/}"  ] ; then
	echo "!!!!!!!!"
	fatal "Please use src.rpm as argument, not spec"
fi

# Skip checking for tar arg
if [ -n "${LISTNAMES/*tar/}" ] ; then
	echog -n "Checking with sisyphus_check... with $HASHER_NOCHECK skipping"
	sisyphus_check --no-check=$HASHER_NOCHECK --files $LISTNAMES || fatal "check for sisyphus failed"
	echog "OK"
fi

# export OURAPTCONF with temp. file contains correct path to sources.list
prepare_aptconfig

# TODO: for add $LISTRPMARGS we need skip function for delete "our" params
HASHERARG="--no-sisyphus-check=$HASHER_NOCHECK --apt-config=$OURAPTCONF --target $DEFAULTARCH $HASHERARG"
if [ "$MENV" != "SS" ] && [ "$MENV" != "DD" ]; then
	HASHERARG="--build-args \"--with=build_$MENV\" --build-args \"--with=build_backport\" $HASHERARG"
fi
LISTBUILTSH=""
for i in $LISTNAMES
do
	# ��������! ��� ������ �� ����������� ��������� ��������� ���� ���������
	# --nprocs=2 
	# Note: hasher is already use renice for low priority
	LOGFILE="$LOGDIR/`basename $i .src.rpm`$MENVARG.log"
	echo "Build in hasher ($MENV)" >$LOGFILE
	rm -f $LOGFILE.failed
	# enable /proc support only if require it
	#rpm --requires -p $i | grep "/proc" && ARG="$HASHERARG --mountpoints=/proc" || ARG="$HASHERARG"
	# we do not need check cases for mount points, thanks ldv@ 
	ARG="$HASHERARG --mountpoints=/proc,/dev/pts"
	echog "Build in hasher: $i (arg: $ARG, distro: $MENV, hasherdir: $HASHERDIR)"

	($HSH $ARG --build-args "--define 'backport_distro ${MENV}'" -- $HASHERDIR $i 2>&1 || touch $LOGFILE.failed) | tee -a $LOGFILE
	test -L $HASHERDIR/BUILD || ln -s chroot/usr/src/RPM/BUILD $HASHERDIR/
	if [ -f $LOGFILE.failed ] ;	then
		RESULT=1
		break
	fi
	check_log $LOGFILE ; RESULT=$?
	test $RESULT = 1 && break
	echog "########## Build of `basename $i` is done in hasher $MENV  #############"
	echo
	LISTBUILTSH="$LISTBUILTSH$i "
	LASTPACKAGE=$i
done

echo
if [ "$RESULT" = "1" ] ; then
	echog "!!!!!!! Hasher: FAILED FAILED FAILED !!!!!!!!!"
	echog "Failed with $i, built only"
else
	echog "Full list of the hashered packages:"
fi
print_list $LISTBUILTSH

echo


if [ "$RESULT" = "0" -a -n "$TESTINSTALL" ] ; then
	# test install (only for last package in list)
	# some key for build hasher from scratch
	echog "Find binary packages for '$(basename $LASTPACKAGE)' in $HASHERDIR/repo/$DEFAULTARCH/RPMS.hasher..."
	BINPACKAGES=`get_binpkg_list $HASHERDIR/repo/$DEFAULTARCH/RPMS.hasher $LASTPACKAGE`
	test -n "$BINPACKAGES" || fatal "Can't find binary packages for $LASTPACKAGE in `ls -l $HASHERDIR/repo/$DEFAULTARCH/RPMS.hasher`"
	echo "List: $BINPACKAGES"
	echo
	loginhsh -t -i $MENVARG $BINPACKAGES || exit 1
fi

# make src.rpm if need
if [ "$RESULT" = "0" ] ; then
	test -n "$LISTRPMARGS" && rpmbs $MENVARG $LISTRPMARGS $LISTBUILTSH
fi

exit $RESULT