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

	#local SETARCH=''
	#[ "$TARGETARCH" != "$BUILDARCH" ] && SETARCH=i386

	res=0
	echo "Chrooting as $INTUSER and run script"
	cat <<EOF | run_in_chroot --user $dist "bash -x -" || res=1
	echo "Run in \$(distr_info -e)"
	umask 002
	# FIXME: ~ works?
	mkdir -p ~/RPM/log
	rm -f ~/tmp/rpm-tmp.*
	rm -rf $RPMBUILDROOT
	# 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="" ; }

#		--define='_host $SETARCH-pc-linux-gnu' \
#		--define='_libdir /usr/lib/$SETARCH-linux-gnu' \
#		--define='_libexecdir /usr/lib' \

	#echo
	#echo "\$RPMCMD --target $TARGETARCH --showrc"
	# https://bugs.etersoft.ru/show_bug.cgi?id=14846#c8
	#rpm --target $TARGETARCH --showrc

	#echo "srpm requires:"
	#epm req /home/$INTUSER/tmp/$(basename $TARGETSRPM)

	\$RPMCMD -v --rebuild $RPMBUILDNODEPS \$RPMARGS \$DDF \
		--define='_unpackaged_files_terminate_build 0' \
		--define='_build_id_links none' \
		--define='%debug_package %{nil}' \
		--target $TARGETARCH \
		--buildroot $RPMBUILDROOT \
		/home/$INTUSER/tmp/$(basename $TARGETSRPM)
	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
EOF

	echo "Chrooting as $INTUSER and get logs"
	cat <<EOF | run_in_chroot --user $dist "bash -x -" &>${LOGFILE/.log/.config.log}

	# FIXME: has quote problem
	RPMARGS="$RPMARGS"
	RPMCMD=rpmbuild

	echo
	echo "\$RPMCMD --target $TARGETARCH --showrc"
	# https://bugs.etersoft.ru/show_bug.cgi?id=14846#c8
	rpm --target $TARGETARCH --showrc

	cd /home/$INTUSER/RPM/BUILD/*/
	for logfile in config.log */config.log config.h */config.h ; do
		if [ -s "\$logfile" ] ; then
			echo
			echo "===================== cat \$logfile: ========================"
			cat "\$logfile"
			echo "================== end of \$logfile ========================="
		fi
	done
EOF

	if [ "$res" != "0" ] ; then
		return $res
	fi

	if is_remote_build $dist ; then
		echo "Copying remote built rpm packages to the local filesystem"
		mkdir -p $BUILTRPM
		local tmptar
		tmptar=$(basename $TARGETSRPM .src.rpm)
		#run_in_chroot --user "cd /home/$INTUSER/RPM/RPMS/ && ls -a"
		run_in_chroot --user $dist "cd /home/$INTUSER/RPM/RPMS/ && tar -cf - *.rpm" >$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
}

