#!/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

# add rpm-dir to global ETERBUILD_APTREPO
function __try_add_rpm_dir()
{
	local COMPONENT="$2"
	if [ ! -d "$1/RPMS.$COMPONENT" ] ; then
		#test -z "$VERBOSE" || warning "Cannot find already built packages repo RPMS.main"
		return
	fi
	# FIXME: it works only for Sisyphus, needs /extra too...
	ETERBUILD_APTREPO="$ETERBUILD_APTREPO
rpm-dir file:$(dirname $1) $(basename $1) $COMPONENT"
#	[ -z "$VERBOSE" ] || echo "base dir found, add aptrepo $ETERBUILD_APTREPO"
}

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

	#set_target_type $(get_altdistr_mod $DISTRVERSION)
	BINARYREPONAME=$DISTRVERSION set_binaryrepo >>$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
	ETERBUILD_APTREPO=''

	# HACK: try to use previous built packages
	__try_add_rpm_dir $DESTDIR main
	__try_add_rpm_dir $DESTDIR/extra main

	if echo "$DESTDIR" | grep -q /pvt/ ; then
		local TRTOPUB="$(echo "$DESTDIR" | sed -e "s|/pvt/|/pub/|" -e "s|WINE-Network|WINE|" -e "s|WINE-Local|WINE|" -e "s|WINE-SQL|WINE|")"
		__try_add_rpm_dir $TRTOPUB main
		__try_add_rpm_dir $TRTOPUB/extra main
	fi

	export ETERBUILD_APTREPO

	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 [ "$BINARYREPO" = "Sisyphus" ] ; then
			BUILDCOMMAND="$RPMBS -s -w"
		else
			BUILDCOMMAND="$RPMBP -b $BINARYREPO"
		fi
		$BUILDCOMMAND $BUILDSRPM || { warning "Cannot build srpm" ; return 1 ; }
		return
	fi

	# build binary packages
	if [ "$BINARYREPO" = "Sisyphus" ] ; then
		BUILDCOMMAND="$RPMBSH -w"
	else
		BUILDCOMMAND="$RPMBPH -b $BINARYREPO"
	fi

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


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

	# Set and expand MAINFILELIST, EXTRAFILELIST
	fill_filelist
}

