#!/bin/sh
##
#  Korinf project
#
#  Common rpm build packages in chrooted Linux system
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009
#
#  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/>.
##

# We believe BUILDERHOME is clean already here

kormod buildargs

# used BUILDERHOME to put in src.rpm
convert_rpm_to_target()
{
	local dist=$1

        [ -n "$BUILDERHOME" ] || BUILDERHOME=~/tmp/korinfer

	# rpmbph will use DISTRNAME/DISTRVERSION and so on

	# FIXME: when build to remote dist, we need generic src.rpm
	echo "Convert '$BUILDSRPM' for $dist distro"
	rm -f $BUILDERHOME/tmp/$BUILDNAME*
	if [ -n "$MAKESPKG" ] ; then
		$RPMBPH -v -n -z $BUILDSRPM
		BUILTRPM=~/$RPMSDIR
		return 0
	else
		ETERDESTSRPM=$BUILDERHOME/tmp $RPMBPH -v -n $BUILDSRPM
		BUILTRPM=$BUILDERHOME/$RPMSDIR
	fi

	# Get real src.rpm name
	TARGETSRPM=$(echo $BUILDERHOME/tmp/$BUILDNAME*.src.rpm | head -n1)
	[ -r "$TARGETSRPM" ] && [ -f "$TARGETSRPM" ] || { warning "Can't read target $TARGETSRPM src.rpm" ; return 1 ; }
	$SUDO chown $LOCUSER $TARGETSRPM

	return 0
}

# set target vars before package build
# can change BUILDARCH
set_target_var()
{
	# defines PKGFORMAT, PKGVENDOR, RPMVENDOR
	set_target_pkg_env # (from etersoft-build-utils)

	[ -z "$PKGFORMAT" ] && { warning "PKGFORMAT is empty" ; return 1 ; }

	[ "$PKGVENDOR" = "gentoo" ] || return 0

	# HACK for Gentoo
	if [ -n "$BOOTSTRAP" ] ; then
		[ "$PKGFORMAT" = "tbz2" ] && PKGFORMAT=rpm
	else
		[ "$PKGFORMAT" = "tbz2" ] && PKGFORMAT=tar.bz2
	fi
	return 0
}

build_rpms()
{
	local dist=$1

	# create needed files in home directory
	init_home
	INTBUILT=/home/$INTUSER/$RPMSDIR

	RPMARGS="$(get_rpm_args $dist $BUILDNAME)"

	echo "Build '$BUILDSRPM' for $dist distro with args '$RPMARGS'"
	# FIXME: problem if system's rpm does not recognize our src.rpm (as Mandriva 2007.1/2008)
	RPMBUILDROOT="/home/$INTUSER/tmp/$BUILDNAME-buildroot"

	RPMBUILDNODEPS=""
	# Skip buildreqs checking on non RPM systems
	[ "$PKGFORMAT" = "rpm" ] || RPMBUILDNODEPS="--nodeps"
	# hack: we set target to rpm before for Gentoo
	[ "$PKGVENDOR" = "gentoo" ] && RPMBUILDNODEPS="--nodeps"

	# TODO: we need do strip with some rpm rule? or during binary rpm converting
	# --without debug is wine only hack
	DISABLEDEBUGFLAG=""
	[ "$PKGFORMAT" = "rpm" ] || DISABLEDEBUGFLAG="--without debug"
	[ "$ALLOWPUBLICDEBUG" = "1" ] || DISABLEDEBUGFLAG="--without debug"

	CMDBUILD="rpmbuild -v --rebuild $RPMBUILDNODEPS "$RPMARGS" $DISABLEDEBUGFLAG --define='_unpackaged_files_terminate_build 0' --buildroot $RPMBUILDROOT /home/$INTUSER/tmp/$(basename $TARGETSRPM) --target $BUILDARCH"
	CMDAFTERBUILD="cat ~/tmp/rpm-tmp.* ; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/config.log; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/config.h ; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/include/config.h"

	# Control file for check build result
        LOGFAILFILE="$BUILDERHOME/RPM/log/$BUILDNAME.log.failed"
	rm -f "$LOGFAILFILE" "$LOGFAILFILE.clog"

	echo "Chrooting as $INTUSER and run $CMDBUILD command in $BUILDARCH system"
	$NICE setarch $BUILDARCH $SUDO chroot $BUILDROOT \
		su - $INTUSER -c "export LANG=C ; export LC_MESSAGES=C ; umask 002 ; mkdir -p ~/RPM/log ; rm -f ~/tmp/rpm-tmp.* ; $CMDBUILD || touch ~/RPM/log/$BUILDNAME.log.failed ; ( $CMDAFTERBUILD ) >~/RPM/log/$BUILDNAME.log.failed.clog "

	if [ -r "$LOGFAILFILE" ] ; then
		rm -f "$LOGFAILFILE" ; warning "build failed"
		[ -r "$LOGFAILFILE.clog" ] && mv "$LOGFAILFILE.clog" "${LOGFILE/.log/.conf.log}"
		return 1
	fi

	# Workaround again flow target dirs
	pushd $BUILTRPM || { warning "can't chdir to $BUILTRPM" ; return 1; }
	test -d i586 && mv -f i586/* ./
	test -d x86_64 && mv -f x86_64/* ./
	test -d noarch && mv -f noarch/* ./
	popd

	# Set and expand MAINFILELIST, EXTRAFILELIST
	fill_filelist
}

