#!/bin/sh -x
##
#  Korinf project
#
#  After build install functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-2010, 2012
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009-2012
#
#  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()
{
	assert_var DISTRNAME INTBUILT EXPMAINFILES EPMCMD
	
	# try use preinstalled epm
	####
	# for gentoo epm not use !
	####
	if [ $PKGVENDOR == "gentoo" ]
	then
		run_in_chroot "cd $INTBUILT && ls -a && ./${INSTALLBUILTSCRIPTNAME}"
		
		local RESULTRET=$?
		
		if [ "$ADEBUG" == "1" ]
		then
			echo
			echo "---------------------------------------------"
			echo "after run_in_chroot result : $RESULTRET"
			echo "---------------------------------------------"
			echo
		fi
		
		if [ "$RESULTRET" != "0" ]
		then
			set_build_failed
		fi
		return
	else
		run_in_chroot "cd $INTBUILT && epm --verbose --auto --force install ${EXPMAINFILES} ${EXPEXTRAFILES}" && return
	fi

	# if status is skipped
	if [ "$?" = "22" ] ; then
		return 22
	fi

	# it is failed for all packages except eepm and rpm-build-altlinux-compat
	if [ -z "$INITIALBOOTSTRAP" ] ; then
		set_build_failed
		return
	fi

	# Bootstrap install eepm (get command from host's epm)
	INSTALLCOMMAND=$(DISTRNAME=$DISTRNAME $EPMCMD --auto --show-command-only --nodeps install) || { warning "Can't get install command" ; return 1 ; }
	run_in_chroot "cd $INTBUILT && yes | $INSTALLCOMMAND ${EXPMAINFILES} ${EXPEXTRAFILES}" #|| set_build_failed
	if [ "$BUILDNAME" = "eepm" ] ; then
		run_in_chroot "epm -h >/dev/null" || set_build_failed
	fi
}

# Install required packages for build under the system (f.i., Debian/6.0)
# Assure we have needed build requirements
install_build_requires()
{
	local reqs dist=$1
	load_mod repl rpm

	assert_var BUILDNAME BUILDARCH BUILDSRPM PKGFORMAT
	assert_var BUILDERHOME

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

	# create needed files in home directory
	#init_home

	# Note: some additional requires we add in rpmbph
	local buildsystempkgs="libtool automake autoconf rpm-build glibc-devel gzip bzip2 pkg-config cpio patch gcc coreutils grep make rpm gawk"

	# hack to prepare Debian based system
	[ "$PKGFORMAT" = "deb" ] && buildsystempkgs="$buildsystempkgs alien fakeroot debhelper"

	reqs=$(get_rpmpkg_requires -p "$BUILDSRPM" | clean_pkgreq)
	reqs=$(convert_pkgreqs_to_target $reqs $buildsystempkgs | xargs -n 1000 echo)

	[ -z "$reqs" ] && return

	# Appeared the backslashed quotes are required if package names contain parentheses
	run_in_chroot "epm --auto --verbose --skip-installed Install \"$reqs\"" || run_in_chroot "epm --auto --verbose --skip-installed install \"$reqs\""
}
