#!/bin/sh
##
#  Korinf project
#
#  After build install 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 tarball

# install built packages
install_built()
{	
	local NODEPS
	if [ -n "$WITHOUTEBU" ] ; then
		echo "Skip install due boot strap mode"
		return 0
	fi

	echo "Install built $TARGET packages to $BUILDROOT root dir..."
	# Do not install debug packages
	#rm -f $BUILTRPM/*debug*
	#BUILDFILES=`echo *${BUILDNAME}*$TARGET`
	#popd
	# Note: немного странно с кавычками и разворачиванием звёздочек
	case $TARGET in
		"deb") # Debian/Ubuntu
			$SUDO chroot $BUILDROOT su - -c "LC_ALL=C cd $INTBUILT && $NICE dpkg -i --force-all ${EXPMAINFILES} ${EXPEXTRAFILES}"
			RES=$?
			;;
		"tgz") # Slackware
			# FIXME: use ${EXPMAINFILES} ${EXPEXTRAFILES}
			$SUDO chroot $BUILDROOT $NICE installpkg "$INTBUILT/*${BUILDNAME}*.$TARGET"
			RES=$?
			;;
		"tar.gz") # Hack for ArchLinux
			# FIXME: use ${EXPMAINFILES} ${EXPEXTRAFILES}
			$SUDO chroot $BUILDROOT su - -c "LC_ALL=C $NICE pacman -U --force --nodeps /home/$INTUSER/abs/$PACKAGE/*${BUILDNAME}*.pkg.$TARGET"
			#$SUDO chroot $BUILDROOT su - -c "cd / ; ls -1 $INTBUILT/*${BUILDNAME}*.$TARGET | xargs -n1 --no-run-if-empty $NICE tar xfvz "
			RES=$?
			;;
		*)
			# First time install without dependences
			# DURING_INSTALL for disable start_service
			[ -n "$WITHOUTEBU" ] && NODEPS=--nodeps
			[ -n "$BOOTSTRAP" ] && NODEPS=--nodeps
			$SUDO chroot $BUILDROOT su - -c "LC_ALL=C cd $INTBUILT && DURING_INSTALL=1 $NICE rpm -Uvh ${EXPMAINFILES} ${EXPEXTRAFILES} --force $NODEPS"
			RES=$?
			#[ "$RES" = "0" ] || warning "install status: failed"
			# Hack: broken return status on some systems
			#RES=0
			;;
	esac
	return $RES
}

# install required packages for build
install_req()
{
	local LISTPKG CMD LOGFAILFILE DISTRVENDOR dist=$1
	load_mod repl rpm

	convert_rpm_to_target $dist

	# create needed files in home directory
	init_home

	# FIXME: remove already installed packages (f.i. if installed package does not exists in repo, install failed on Mandriva)
	LISTPKG=$(rpmquery --requires -p "$TARGETSRPM" | clean_pkgreq | sed -e "s|rpm-build-altlinux-compat||g")
	DISTRVENDOR=`ROOTDIR=$BUILDROOT /usr/bin/distr_vendor -d`
	CMD=$(get_install_package_command $DISTRVENDOR)
	echo "Install build required packages $LISTPKG with '$CMD' command"

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

	$NICE setarch $BUILDARCH $SUDO chroot $BUILDROOT \
		su - -c "export LANG=C ; export LC_ALL=C ; $CMD $LISTPKG || touch /home/$INTUSER/tmp/$BUILDNAME.log.failed"

	[ -r "$LOGFAILFILE" ] && { rm -f "$LOGFAILFILE" ; warning "install failed" ; return 1 ; }
	return 0
}
