#!/bin/sh
# 2003-2017 (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 buildsrpm

RESULT=0
REMCOM=
COMMIT=

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

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echo
	echog "Options:"
	echog "   -u  sign and run gear build task after build"
	echog "   -i  install built packages in test hasher (via loginhsh -t)"
	echog "   -f  force tag set (see rpmbs --help)"
	echo
	echog "Ext. options:"
	echog "   -c  build without stuff (without previous built packages)"
	echog "   -s  sign (and move to dir ETERDESTSRPM if defined) after build"
	echog "   -e  sign and run gear test only task girar build"
	echog "   -t  make temporary commit before build (rpmbb like behavior)"
	echog "   -l  lazy cleanup (clean before build, not after)"
	echog "   -w  build with gear --hasher myhsh directly"
	echog "   -b REPONAME  binary repository name (p6, t6, c7, p8 and so on)"
}

PASSEDARGS=
while getopts :hieufclstwb: opt; do
    case $opt in
    h) phelp; exit 0;;
    t) COMMIT="--commit" ;;
    u|f|c|e|s|l) PASSEDARGS="$PASSEDARGS -$opt" ;;
    w) THOROUGH=1 ;;
    i) INSTALLPKG=1 ;;
    b) BINARYREPONAME=$OPTARG ;;
    +?) 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 
	[ "$OPTIND" -gt 0 ] && shift $(($OPTIND - 1))
fi

LISTRPMARGS="$PASSEDARGS $*"

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

set_girar_host $1 && shift

parse_cmd_pre_spec "$@"
mygetopts $LISTARGS
# see functions/alt:set_binaryrepo() for BINARYREPONAME
set_binaryrepo $MENV

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

# assure we have only specs or src.rpm
LISTNAMES=$(repodirs_to_specs $LISTNAMES)

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

if [ -n "$THOROUGH" ] ; then
	# TODO: use gear --hasher for fix macro problem (https://bugzilla.altlinux.org/show_bug.cgi?id=31673)
	docmd time gear --hasher -- $ETERBUILDBIN/myhsh $MENVARG $(drop_args "$LISTRPMARGS" f u a A) $LISTBUILT
	exit
else
	# We do not not pass args for myhsh here...
	# FIXME: не обрабатывает два пропускаемых вместе параметра
	pack_src_rpm $COMMIT $(drop_args "$LISTRPMARGS" s u c e l f a A)

	# build in hasher
	docmd time $ETERBUILDBIN/myhsh $MENVARG $(drop_args "$LISTRPMARGS" f u a A) $LISTBUILT
	RESULT=$?
fi

# if ok and test install is requested, do it
if [ "$RESULT" = "0" ] && [ -n "$INSTALLPKG" ] ; then
	BINPACKAGES=$($ETERBUILDBIN/myhsh $MENVARG -b $LISTBUILT | estrlist reg_exclude debuginfo -) || fatal "Can't get binary packages for $LISTBUILT"

	estrlist isempty "$BINPACKAGES" && fatal "There is no binary packages after build"

	echo
	echog "List of binary packages:"
	print_list $BINPACKAGES
	echo

	# Note: use showcmd due console input or some other conflict
	showcmd $ETERBUILDBIN/loginhsh -Y -t -i -b $BINARYREPO $BINPACKAGES
	$ETERBUILDBIN/loginhsh -Y -t -i -b $BINARYREPO $BINPACKAGES
	RESULT=$?

	echog "Remove packages from test hasher dir ..."
	docmd $ETERBUILDBIN/myhsh -r $MENVARG $BINPACKAGES
fi

# remove generated src.rpm
for i in $LISTBUILT ; do
	estrlist has $i $LISTNAMES || rm -fv $i
done

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

# 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

if rhas "$LISTNAMES" .spec ; then
	is_gear $LISTNAMES && TSKLIST="$LISTNAMES"
else
	TSKLIST="$LISTBUILT"
fi

docmd $ETERBUILDBIN/rpmbs $GIRARHOST -b $BINARYREPO $RPMBSARGS $TSKLIST

