#!/bin/bash
# 2008 Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain


# Universal rpmbuild function
# 1st: rpmbuild, hsh, buildreq
# 2nd: spec name(s) (only one if git, we will skip it)
# other parameters
uni_rpmbuild()
{
	local RET=0
	# hack due quotes
	# just for fill by something
	local ONEPARAM="--quiet"
	local TWOPARAM="--quiet"
	local THRPARAM="--quiet"
	local FOURPARAM="--quiet"
	local FIVEPARAM="--define='_unpackaged_files_terminate_build 0'"
	local COMMIT=""
	local COMMANDAFTER=""

	if [ "$1" = "--commit" ] ; then
		shift
		COMMIT="--commit"
		COMMANDAFTER="git reset"
	fi

	local SPECNAME="$1"
	local SPECDIR=`dirname $SPECNAME`
	local GEAR=gear

	if [ -n "$USE_LEGACY_COMPRESSION" ] ; then
		# FIXME: do not override -v
		ONEPARAM="--define=_source_payload w9.gzdio"
	fi

	if [ -n "$USE_VENDOR" ] ; then
		THRPARAM="--define=_vendor $USE_VENDOR"
	fi
	
	if [ -n "$RPMTOPDIR" ] ; then
		TWOPARAM="--define=_topdir $RPMTOPDIR"
	fi

	if [ -n "$CCACHE_ENABLE" ] && [ -n "$CCACHE_DIR" ] ; then
		FOURPARAM="--define=__ccache_dir $CCACHE_DIR"
	fi

	if is_gear $SPECDIR ; then
		[ -f "$SPECNAME" ] || fatal "run uni_rpmbuild with spec as 2nd parameter"
		shift # skip spec name
		#[ -n "$SPECDIR" ] && [ "$SPECDIR" != "." ] && warning "Spec is not in git dir (SPECDIR='$SPECDIR')"
		# FIXME: use spec name as project name
		BASESPECNAME=$(basename "$SPECNAME")
		ALTGEARRULESFILE=.gear/rules-${BASESPECNAME/.spec/}
		ALTGEARRULES=$(git rev-parse --git-dir)/../$ALTGEARRULESFILE
		GEARRULES=
		if [ -r "$ALTGEARRULES" ] ; then
			echo "Build with alternate rules $ALTGEARRULESFILE"
			GEARRULES=--rules="$ALTGEARRULESFILE"
		fi
		# build package without MENV checking
		if true || [ "$MENV" = "SS" ] ; then
			$NICE $GEAR $COMMIT $GEARRULES --rpmbuild -- $RPMBUILD "$ONEPARAM" "$TWOPARAM" "$THRPARAM" "$FOURPARAM" "$FIVEPARAM" $@ || RET=$?
			[ -z "$COMMANDAFTER" ] || $COMMANDAFTER
		else
			fatal "Build backported src.rpm from git is unsupported now"
			# build src.rpm via hasher (on ALT)
#			$NICE gear-hsh --build-args="-bs" --rpmbuild -- $COMMAND "$ONEPARAM" $@
			$NICE $GEAR --hasher -- myhsh --build-prog=$ETERBUILDDIR/functions/rebuild $@ || RET=$?
		fi
	else
		mkdir -p $RPMTOPDIR/BUILD $RPMTOPDIR/SRPMS
		$NICE $RPMBUILD "$ONEPARAM" "$TWOPARAM" "$THRPARAM" "$FOURPARAM" "$FIVEPARAM" $@ || RET=$?
	fi
	[ -n "$VERBOSE" ] && echo "Run $RPMBUILD $ONEPARAM $TWOPARAM $THRPARAM $FOURPARAM $FIVEPARAM $@" || :
	return $RET
}


uni_buildreq()
{
	local RET=0
	local BUILDREQPARAM="$1"
	shift
	local SPECNAME=$(realpath "$1")
	shift
	local SPECDIR=`dirname $SPECNAME`
	local GEARBUILDREQ=gear-buildreq

	if is_gear $SPECDIR ; then
		$NICE $GEARBUILDREQ $BUILDREQPARAM --commit -- "$SPECNAME" $@ || RET=$?
	else
		$NICE buildreq $BUILDREQPARAM "$SPECNAME" $@ || RET=$?
	fi
	return $RET
}

uni_rpminstall()
{
	local RET=0
	local TWOPARAM="-v"

	if [ -n "$RPMTOPDIR" ] ; then
		TWOPARAM="--define=_topdir $RPMTOPDIR"
	fi

	#if is_gear $SPECDIR ; then
	#	$NICE $GEARBUILDREQ --commit -- $@ || RET=$?
	#else
		echo -n "Install package "
		mkdir -p $RPMTOPDIR/SOURCES $RPMTOPDIR/SRPMS $RPMTOPDIR/RPMS
		rpm -iv "$TWOPARAM" $@ || RET=$?
	#fi
	return $RET
}

# remove source and spec
uni_rpmrm()
{
	local TWOPARAM="-v"

	if [ -n "$RPMTOPDIR" ] ; then
		TWOPARAM="--define=_topdir $RPMTOPDIR"
	fi

	$RPMBUILD "$TWOPARAM" --rmsource --rmspec $@
}


# LISTNAMES, options in arg
# build LISTBUILT list with src.rpm full path
pack_src_rpm()
{
local i COMMIT

if [ "$1" = "--commit" ] ; then
	shift
	COMMIT="--commit"
fi

ARGS=$@
[ -n "$VERBOSE" ] && echo "LISTNAMES=$LISTNAMES ARGS=$ARGS" || :
ARGS="$ARGS --nodeps"
for i in $LISTNAMES
do
	
	if [ -z ${i/*rpm/} ] ; then
		# if rpm not spec, guess it is src.rpm
		#NAMESRPMIN=$(basename $i)

		if [ -f "$i" ] ; then
			LISTBUILT="$LISTBUILT $(realpath $i)"
			build_rpms_name_by_srpm "$i" $ARGS
		else
			fatal "pack_src_rpm: Can't find '$i'"
		fi
	else
		# Получаем названия пакетов по спеку
		# BASENAME, RELEASE, VERSION, NAMESRPMIN, NAMERPMIN
		build_rpms_name "$i" $ARGS
		if [ -n "${SIGN}" ]; then
			echog "Try to packing \$NAMESRPMIN package for sign"
			add_changelog $i
			if [ $? = "0" ]; then
				echog "Spec file \$i had not ChangeLog entry. It have added now, check it and enter your command again."
				echog "Wait for ten seconds."
				sleep 10
				exit 1
			fi
			uni_rpmbuild $COMMIT $i -bs $ARGS $NODEPS --target $SYSARCH
		else
			echog "Just packing \$NAMESRPMIN"
			uni_rpmbuild $COMMIT $i -bs $ARGS $NODEPS --target $SYSARCH || fatal "Error with rpmbuild"
		fi

		# Check the package really built
		if [ -f "$RPMTOPDIR/SRPMS/$NAMESRPMIN" ] ; then
			LISTBUILT="$LISTBUILT $RPMTOPDIR/SRPMS/$NAMESRPMIN"
		else
			fatal "Can't find '$NAMESRPMIN' in '$RPMTOPDIR/SRPMS'"
		fi
	fi

done
LISTBUILT=$(strip_spaces "$LISTBUILT")
test -z "$LISTBUILT" && fatal "Error: List for build is empty. Check if file is exist."
#LISTRPMARGS=`echo ${LISTRPMARGS} | sed -e "s/--nodeps//g"`
}

check_log()
{
	local LOGFILE i RES
	echo
	echo "-------------------------------------------------------"
	echog "Check log for..."
	LOGFILE=$1
	
	if grep -q 'ld: warning: libstdc++\.so\.5, needed by [^ ]\+, may conflict with libstdc++\.so\.6' $LOGFILE ; then
		echog "Error found: libstdc++.so.5/6 conflicts. Check the log."
		return 1
	fi

	if grep -q 'will always overflow destination buffer' $LOGFILE ; then
		echog "Error found: will always overflow destination buffer. Check the log."
		return 1
	fi

	grep 'python-strict' $LOGFILE && warning "python-strict used!!!"
	NC1="command not found"
	NC2="-march=athlon -mtune=athlon-xp"
	#for i in ${NC1} ; do
	grep -- '$NC1' $LOGFILE && warning "'$NC1' in build output (some errors in a scripts)"
	grep -- '$NC2' $LOGFILE && warning "'$NC2' in build output (forbidden) "
	#done
	return 0
}

