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

build_rpms()
{
	assert_var INTUSER RPMSDIR BUILDNAME BUILDSRPM
	assert_var PKGFORMAT PKGVENDOR
	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"

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

	echo "Chrooting as $INTUSER and run script"
	cat <<EOF | run_in_chroot --user "bash -x -" || return
	echo "Run in \$(distr_info)"
	umask 002
	mkdir -p ~/RPM/log
	rm -f ~/tmp/rpm-tmp.*
	# FIXME: has quote problem
	RPMARGS="$RPMARGS"
	RPMCMD=rpmbuild
	DDF="$DISABLEDEBUGFLAG"
	# some systems has no rpmbuild, only rpm (or just install rpm-build package?)
	#which \$RPMCMD 2>/dev/null || { RPMCMD=rpm ; DDF="" ; }

	\$RPMCMD -v --rebuild $RPMBUILDNODEPS \$RPMARGS \$DDF \
		--define='_unpackaged_files_terminate_build 0' \
		--buildroot $RPMBUILDROOT \
		/home/$INTUSER/tmp/$(basename $TARGETSRPM) --target $BUILDARCH
	RES=\$?

	# Workaround again flow target dirs
	pushd /home/$INTUSER/RPM/RPMS/
	test -d i586 && mv -f i586/* ./ && rmdir i586
	test -d x86_64 && mv -f x86_64/* ./ && rmdir x86_64
	test -d noarch && mv -f noarch/* ./ && rmdir noarch
	popd

	exit \$RES
	#( cat ~/tmp/rpm-tmp.* ; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/config.log; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/config.h ; cat ~/$RPMSDIR/../BUILD/${BUILDNAME}*/include/config.h ) >~/RPM/log/$BUILDNAME.log.failed.clog
EOF

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

	# copy remote files to the local filesystem
	if is_remote_build ; then
		mkdir -p $BUILTRPM
		local tmptar
		tmptar=$(basename $TARGETSRPM .src.rpm)
		run_in_chroot --user "cd /home/$INTUSER/RPM/RPMS/ && tar -cf - *" >$BUILTRPM/$tmptar.tar || return
		pushd $BUILTRPM
		tar xfv $BUILTRPM/$tmptar.tar && rm -f $BUILTRPM/$tmptar.tar || return
		popd
	fi

	# Set and expand MAINFILELIST, EXTRAFILELIST
	fill_filelist
}

