#!/bin/sh
##
#  Korinf project
#
#  ALT Linux hasher build related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-2010
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2010
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.

#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.

#  You should have received a copy of the GNU Affero General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

load_mod rpm hasher

function build_in_hasher()
{
	assert_var DISTRVERSION BUILDARCH DESTDIR RPMDIR
	assert_var LOGFILE

	set_target_type $(get_altdistr_mod $DISTRVERSION)
	detect_target_env >>$LOGFILE 2>&1
	# use loginhsh -q -d here
	# workaround for detect needed path build for other target arch
	HASHERDIR=$(set_hasherdir; echo $HASHERDIR )
	BUILTRPM=$HASHERDIR/repo/$BUILDARCH/RPMS.hasher

	# HACK: try to use previous built packages
	if [ -d "$DESTDIR/RPMS.main" ] ; then
		# FIXME: it works only for Sisyphus, needs /extra too...
		export ETERBUILD_APTREPO="rpm-dir file:$(dirname $DESTDIR) $(basename $DESTDIR) main"
		[ -z "$VERBOSE" ] || echo "base dir found, add aptrepo $ETERBUILD_APTREPO"
	else
		test -z "$VERBOSE" || warning "Cannot find already built packages repo RPMS.main"
	fi

	test -n "$MENV" || fatal "build_in_hasher: Call me with correct MENV variable"
	echo "Build in hasher: $MENV for $dist..."
	if [ -n "$NIGHTBUILD" ] ; then
		echo "Clean hasher's rpms before nightbuild"
		rm -rf $BUILTRPM/*
	else
		echo "Clean only the same names from hasher"
		rm -rf $BUILTRPM/*${BUILDNAME}*
	fi

	# FIXME: rewrite spec in hasher?
	# FIXME: install rpm package in temp dir
	# we need .rpmmacros for rpmbph/rpmbsh
	#sed -e "s|$INTUSER|$USER|g" < $WINEETER_PATH/sources/rpmmacros >~/.rpmmacros || fatal "Can't copy macros"
if [ ! -r ~/.rpmmacros ] ; then
cat <<EOF >~/.rpmmacros || fatal "Can't copy macros"
%_topdir        /home/$USER/RPM
%_tmppath       /home/$USER/tmp
%_sourcedir %_topdir/SOURCES
%vendor Etersoft
%distribution LINUX@Etersoft
#%_target_cpu i586
%buildhost builder.etersoft.ru
#%_gpg_path %homedir/.gnupg
#%_gpg_name Vitaly Lipatov <lav@altlinux.ru>
%packager Etersoft Builder <support@etersoft.ru>

# see http://wiki.sisyphus.ru/devel/RpmSetup
%_build_name_fmt %{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm
EOF
fi

	# build source package
	if [ -n "$MAKESPKG" ] ; then
		BUILTRPM=$RPMDIR/SRPMS
		if [ "$MENV" = "SS" ] ; then
			BUILDCOMMAND="$RPMBS -s"
		else
			BUILDCOMMAND="$RPMBP $MENVARG"
		fi
		$BUILDCOMMAND $BUILDSRPM || { warning "Cannot build srpm" ; return 1 ; }
		return
	fi

	# build binary packages
	if [ "$MENV" = "SS" ] ; then
		BUILDCOMMAND="$RPMBSH"
	else
		BUILDCOMMAND="$RPMBPH $MENVARG"
	fi

	( HASHER_NOCHECK=nvr,gpg,packager,changelog,deps $BUILDCOMMAND $BUILDSRPM ) ||
		{ warning "Do not hashered" ; [ -n "$ADEBUG" ] || ( setarch $BUILDARCH $ETERBUILDBIN/loginhsh -c $MENVARG) ; return 1 ; }


	TARGETSRPM=$(ls -1 $HASHERDIR/repo/SRPMS.hasher/${BUILDNAME}*.src.rpm | head -n1)

	# Set and expand MAINFILELIST, EXTRAFILELIST
	fill_filelist
}

