#!/bin/sh
# 2003-2010 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
# BSH - build source; use hasher
#
# Создаёт пакет(ы) по указанным спекам и собирает в hasher

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

RESULT=0
REMCOM=
COMMIT=

#############################
Usage="Usage: $name [-r -s -u -m -i -c] [-f] [spec or src.rpm]..."
function mygetopts()
{
name=${0##*/}
Descr="$name - BSH - build [from] source; use hasher"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -r - remote build (on $BUILDSERVER host)"
	echog " -u - sign and run gear build task after build"
	echog " -i - install built packages in test hasher (via loginhsh -t)"
	echog "Ext. options:"
	echog " -c - build without stuff (previous built packages)"
	echog " -s - sign (and move to dir ETERDESTSRPM if defined) after build"
	echog " -t - make temporary commit before build (rpmbb like behavior)"
	echog " -l - lazy cleanup (clean before build, not after)"
	echog " -f - force tag set (see rpmbs --help)"
}

while getopts :hrulsimt opt; do
    case $opt in
    h) phelp; exit 0;;
    r) REMOTEBUILD=1 ;;
    t) COMMIT="--commit" ;;
    +?) 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=$@

}
#############################

set_girar_host $1 && shift

parse_cmd_pre_spec "$@"
mygetopts $LISTARGS

[ -n "$LISTNAMES" ] || fatal "Run with exists specs as arg"

# Skip for prev. packed src.rpm
if echo "$LISTNAMES" | grep -q spec ; then
	# If fail, we already has correct changelog
	add_changelog_helper "- new version" $LISTNAMES
fi

# We do not not pass args for myhsh here...
# FIXME: не обрабатывает два пропускаемых вместе параметра
pack_src_rpm $COMMIT $(drop_args "$LISTRPMARGS" s u U i c l f)

if echo "$LISTNAMES" | grep -q spec ; then
	set_specdir $LISTNAMES
fi

if  [ -n "$REMOTEBUILD" ] ; then
	check_ssh_key
	test -n "$BUILDSERVER" || fatal "Please set BUILDSERVER in config file"
	# FIXME - get via etersoft-build-utils
	REMOTERPMDIR=`ssh $BUILDSERVER echo $HOME`/RPM
	LISTSRPM="$(echo $LISTBUILT | xargs -n1 basename)"
	echo
	echog "############ Build at build server '$BUILDSERVER' (with $LISTRPMARGS options) ############"
	echo
	echog "==== Copying to build server ====="
	rsync -vay --partial --progress --checksum \
		-e ssh $LISTBUILT $BUILDSERVER:$REMOTERPMDIR/SRPMS/ || fatal "Error with rsync"
	# TODO: remove after build
	ssh -t $BUILDSERVER "cd $REMOTERPMDIR/SRPMS && time myhsh $MENVARG $LISTRPMARGS $LISTSRPM"
	RESULT=$?
	date
	echo "-------------------------------"
	test "$RESULT" = "0" && echog "It was remote build at $BUILDSERVER" || echog "There is error during remote build at $BUILDSERVER"
else
	time $ETERBUILDBIN/myhsh $MENVARG $LISTRPMARGS $LISTBUILT
	RESULT=$?
	# remove generated src.rpm
	for i in $LISTBUILT ; do
		echo $LISTNAMES | grep -q $i || rm -f $i
	done

	date
	echo "-------------------------------"
fi

# make src.rpm if build OK and we have options for rpmbs

RPMBSARGS=$(drop_args "$LISTRPMARGS" i c l)
if [ ! "$RESULT" = "0" ] || [ -z "$RPMBSARGS" ]; then
	exit $RESULT
fi

is_gear $SPECDIR && TSKLIST=$LISTNAMES || TSKLIST=$LISTBUILT

exec $ETERBUILDBIN/rpmbs $GIRARHOST $MENVARG $RPMBSARGS $TSKLIST

