#!/bin/sh
# 2003-2005 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# $Id: myhsh,v 1.6 2005/09/21 22:20:17 lav Exp $
# 06.02.05
# 15.04.05 add other repository support
# 20.08.05 add /proc support
#   SRPM-  hasher
#      ( -m)
# TODO: -s - build src with sign when build is completed
# TODO: -u

if [ ! -f /etc/rpm/etersoft-build-functions ] ; then
	echog "NOTE: /etc/rpm/etersoft-build-functions does not exist"
	echog "Please fix this path in $0 file if your wish local install"
	exit 1
fi
. /etc/rpm/etersoft-build-functions

build_list_and_args "$@"

#############################
name=${0##*/}
Descr="$name - build package via hasher"
Usage="Usage: $name [-m -s -u] spec..."

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -m - mail result log"
	echog " -s - sign package(s)"
	echog " -u - sign and upload package(s) to Incoming"
}

while getopts :hsum opt; do
    case $opt in
    h) phelp; exit 0;;
    s) SIGN="-s" ;;
    u) UPLOADNOW="-su" ;;
    m) TOMAIL=1 ;;
    +?) 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;;
    esac
done
 
# remove args that were options
shift $((OPTIND - 1))

if [ $# -lt 1 ]; then
    echog "$Usage"
	echog "Use -h for help."
    exit
fi


# Start as mail helper
function mail_build()
{
	$0 "$@" 2>&1 | mutt "$OWNERMAIL" -s "Result of rebuild at $HOSTNAME at `date`"
}

if [ -n "$TOMAIL" ]; then
	echog "Build via email"
	( mail_build $LISTNAMES $LISTARGS ) >/dev/null 2>&1 &
	echog "Build request is accepted for: $LISTNAMES" 
	echog "Report will be sending to: $OWNERMAIL"
	exit 0
fi

RESULT=0

HASHERDIR=$HASHERDIR$MENVARG
mkdir -p $LOGDIR $HASHERDIR
if [ -d $HASHERDIR/lockdir ]
then
	echog "Hasher $HASHERDIR in use at `date`."
	echo -n "Wait for unlocking"
	while test -d $HASHERDIR/lockdir ;	do
		echo -n "." ; sleep 3
	done
	echo
fi

echog "List of packages: \$LISTNAMES"

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

HASHERARG="--no-sisyphus-check-in=changelog,gpg,packager --apt-config=$APTCONF --target $DEFAULTARCH $LISTRPMARGS"
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"
	rm -f $LOGFILE.failed
	# enable /proc support only if require it
	rpm --requires -p $i | grep "/proc" && ARG="$HASHERARG --mountpoints=/proc" || ARG="$HASHERARG"
	echog "Build in hasher: $i (arg: $ARG, distro: $MENV, hasherdir: $HASHERDIR)"

	(hsh -v $ARG $HASHERDIR $i || touch $LOGFILE.failed) | tee $LOGFILE
	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  #############"
	LISTBUILTSH="$LISTBUILTSH$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

for i in $LISTBUILTSH ; do
	echo -e "\t$i"
done

if [ "$RESULT" = "0" ] ; then
	if [ -n "$SIGN$UPLOADNOW" ] ; then
		rpmbs $SIGN $UPLOADNOW $LISTBUILTSH
	fi
fi

echo
exit $RESULT
