#!/bin/sh
##
#  Korinf project
#
#  After build install functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-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/>.
##


# 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 $NICE dpkg -i --force-all $INTBUILT/*${BUILDNAME}*.$TARGET"
			#$SUDO chroot $BUILD $NICE dpkg -i --force-overwrite "$INTBUILT/*${BUILDNAME}*.$TARGET"
			RES=$?
			;;
		"tgz") # Slackware
			$SUDO chroot $BUILDROOT $NICE installpkg "$INTBUILT/*${BUILDNAME}*.$TARGET"
			RES=$?
			;;
		"tar.gz") # Hack for ArchLinux
			$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

	convert_rpm_to_target $dist
	# create needed files in home directory
	init_home

	LISTPKG=$(rpmquery --requires -p $BUILDERHOME/tmp/$TARGETSRPM | clean_pkgreq | sed -e "s|rpm-build-altlinux-compat||g")
	DISTRVENDOR=`ROOTDIR=$BUILDROOT /usr/bin/distr_vendor -d`
	case $DISTRVENDOR in
		"ALTLinux"|"Ubuntu"|"Debian"|"PCLinux")
			CMD="apt-get update && apt-get -y --force-yes install $LISTPKG"
			;;
		"LinuxXP"|"Fedora"|"ASPLinux"|"CentOS"|"RHEL"|"Scientific")
			CMD="yum -y install $LISTPKG"
			;;
		"Mandriva")
			CMD="urpmi --auto --no-verify-rpm $LISTPKG"
			;;
		"SUSE")
			echo "zypper install $PKGNAME" || exit 1
			CMD="yes | zypper install $LISTPKG"
			;;
		*)
			echo "Skipping unknown DISTRVENDOR $DISTRVENDOR"
			;;
	esac
	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 || touch /home/$INTUSER/tmp/$BUILDNAME.log.failed"

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