#!/bin/bash
# 2003-2009 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
#
# BB - binary build (like rpm option -bb)
#
# Собирает двоичные rpm-пакеты по указанным спекам
# Параметры:
# - название спек-файла

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

# Start as helper
if [ "$1" = "--helper" ]; then
	shift
	cd $RPMTOPDIR/SPECS && rpmbb "$@"
	exit $?
fi

#############################

Usage="Usage: rpmbb [-c -p -i -r -R] [spec]"
function mygetopts()
{
Descr="rpmbb (Build Binary) - build binary rpm from spec or from git"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echo
	echog "Options:"
if is_alt ; then
	echog "   -i  skip build stage, but do install files and package it"
	echog "   -p  packaging only stage"
fi
	echog "   -r  find build requires (run buildrequires)"
	echog "   -R  find build requires during install (run buildrequires with -bi)"
	echo
	echog "Ext. options:"
if is_alt ; then
	echog "   -c  start from compile stage"
fi
	echog "   -d  build with dmake"
}

while getopts :hciprRd opt; do
    case $opt in
    h) phelp; exit 0;;
    c) BUILDOPT="c" ;;
    i) BUILDOPT="i b" ;;
    p) BUILDOPT="b" ;;
    r) BUILDREQ="-bc" ;;
    R) BUILDREQ="-bi" ;;
    d) USEDMAKE="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;;
    ?) OPTIND=$((OPTIND-1)); break;
    esac
done

# FIXME: параметры передаются и дальше (например, -i, -b)
# remove args that were options
if [ $# -gt 0 ]; then 
	[ "$OPTIND" -gt 0 ] && shift $(($OPTIND - 1))
fi

# pass other options to RPM:
LISTRPMARGS=$@
}

parse_cmd_pre_spec "$@"
mygetopts $LISTARGS

prepare_rpmdir
test -z "$LISTNAMES" && fatal "Please run with spec(s) name"
test -n "$LISTRPMARGS" && echog "Run $RPMBUILD with options: '$LISTRPMARGS'"

try_enable_ccache

detect_target_env

assert_var PKGFORMAT
# Set nodeps if run in non rpm system
[ "$PKGFORMAT" = "rpm" ] && NODEPS="" || NODEPS="--nodeps"

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

# instead cbuild req
if [ -n "$BUILDREQ" ] ; then
	uni_buildreq $BUILDREQ $LISTNAMES
	RET=$?
	echog "You can run with -R param for search a dependences for install section of the package (as with buildreq -bi)"
	exit $RET
fi

if [ -n "$BUILDOPT" ] ; then
	is_alt || fatal "Sorry, only ALT Linux rpm supports --short-circuit correctly"
	for it in $BUILDOPT ; do
		uni_rpmbuild --commit $LISTNAMES -b$it $LISTRPMARGS $NODEPS --target $BUILDARCH --short-circuit || exit
	done
	exit 0
fi


for ln in $LISTNAMES
do
	# force SPECDIR
	set_specdir $ln

	cd $SPECDIR || fatal
	build_rpms_name "$ln"
	if ! is_alt ; then
		if ! build_buildroot ; then
			LISTRPMARGS="--buildroot $BUILDROOT $LISTRPMARGS"
		fi
		echo "For non ALT distro clean BuildRoot $BUILDROOT"
		test -n "$BUILDROOT" || fatal "BUILDROOT var is empty"
		rm -rf "$BUILDROOT"
	fi
	LOGFILE="$LOGDIR/`basename $NAMESRPMIN .src.rpm`$MENVARG.log"
	rm -f $LOGFILE $LOGFILE.ok
	#echo "Build in native system" >$LOGFILE
	(uni_rpmbuild --commit $ln -bb $LISTRPMARGS $NODEPS --target $BUILDARCH 2>&1 && touch $LOGFILE.ok) | tee -a $LOGFILE | colorify

	[ -r "$LOGFILE.ok" ]
	RET=$?
	rm -f $LOGFILE.ok

	if [ "$RET" = "0" ] ; then
		check_log $LOGFILE
		RET=$?
	else
		echo
		echog "!!!!!!!! ERROR !!!!!!!"
		fatal "Build failed with $ln"
	fi

	echog "Done with build $NAMESRPMIN"
	date

	cd - >/dev/null
done