#!/bin/sh -x
##
#  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

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

	echo "Install built $PKGFORMAT packages to $BUILDROOT root dir..."
	# Do not install debug packages
	#rm -f $BUILTRPM/*debug*
	#BUILDFILES=`echo *${BUILDNAME}*$PKGFORMAT`
	#popd
	# Note: немного странно с кавычками и разворачиванием звёздочек
	case $PKGFORMAT in
		"deb") # Debian/Ubuntu
			$SUDO chroot $BUILDROOT su - -c "export LC_ALL=C ; cd $INTBUILT && { $NICE dpkg -i ${EXPMAINFILES} ${EXPEXTRAFILES} ; apt-get -f -y install; }"
			RES=$?
			;;
		"tgz") # Slackware
			# FIXME: use ${EXPMAINFILES} ${EXPEXTRAFILES}
			$SUDO chroot $BUILDROOT $NICE installpkg "$INTBUILT/*${BUILDNAME}*.$PKGFORMAT"
			RES=$?
			;;
		"tar.xz") # Hack for ArchLinux
			# FIXME: use ${EXPMAINFILES} ${EXPEXTRAFILES}
			$SUDO chroot $BUILDROOT su - -c "export LC_ALL=C ; $NICE yes | pacman -v -U --force --nodeps /home/$INTUSER/abs/$PACKAGE/*${BUILDNAME}*.pkg.$PKGFORMAT"
			#$SUDO chroot $BUILDROOT su - -c "cd / ; ls -1 $INTBUILT/*${BUILDNAME}*.$TARGET | xargs -n1 --no-run-if-empty $NICE tar xfvz "
			RES=$?
			;;
		"rpm")
			# First time install without dependences
			# DURING_INSTALL for disable start_service
			[ -n "$WITHOUTEBU" ] && NODEPS=--nodeps
			[ -n "$BOOTSTRAP" ] && NODEPS=--nodeps
			$SUDO chroot $BUILDROOT su - -c "export 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
			;;
		*)
			fatal "Target $PKGFORMAT does not supported yet for install"
			;;
	esac

	# mark as failed if install is not completed succefully
	[ "$RES" = 0 ] || set_build_failed

	return $RES
}

filter_out_installed_packages()
{
	# TODO: use this more effectively way
	#for i in $(cat) ; do
	#	rpm -q $i >/dev/null && continue
	#	echo $i
	#done | 
	assert_var PKGFORMAT BUILDARCH BUILDROOT
	case $PKGFORMAT in
		"rpm")
			$NICE setarch $BUILDARCH $SUDO chroot $BUILDROOT \
				su - -c "export LANG=C ; export LC_ALL=C ; xargs rpm -q 2>&1 | grep 'is not installed' | sed -e 's|^.*package \(.*\) is not installed.*|\1|g'" |
					sed -e "s|rpm-build-altlinux-compat||g"
			;;
		"deb")
			$NICE setarch $BUILDARCH $SUDO chroot $BUILDROOT \
				su - -c "export LANG=C ; export LC_ALL=C ; xargs dpkg -L 2>&1 | grep 'is not installed.' | sed -e 's|^Package .\(.*\). is not installed.*|\1|g'" |
					sed -e "s|rpm-build-altlinux-compat||g"
			;;
		*)
			cat
			;;
	esac
}

# install required packages for build under the system (f.i., Debian/6.0)
install_req()
{
	local reqs CMD LOGFAILFILE dist=$1
	load_mod repl rpm

	assert_var BUILDNAME BUILDARCH BUILDROOT BUILDSRPM PKGFORMAT

	# NOTE: possible side effect with redefined DISTRNAME, BUILDARCH, DISTRVERSION
	parse_dist_name $dist

	# create needed files in home directory
	init_home

	#reqs=$(rpmquery --requires -p "$TARGETSRPM" | clean_pkgreq | filter_out_installed_packages)
	reqs=$(get_rpmpkg_requires -p "$BUILDSRPM" | clean_pkgreq)
	reqs=$(convert_pkgreqs_to_target $reqs | filter_out_installed_packages)

	reqs=$(strip_spaces $reqs)

	CMD=$(get_install_package_command $DISTRNAME)
	echo "Install build required packages $reqs with '$CMD' command"

	if [ -z "$reqs" ] ; then
		echo "Skip empty install list"
		return 2;
	fi

	# 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 $reqs || touch /home/$INTUSER/tmp/$BUILDNAME.log.failed"

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