#!/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 EPMCMD

	# try use preinstalled epm
	run_in_chroot "cd $INTBUILT && epm --verbose --auto --force install ${EXPMAINFILES} ${EXPEXTRAFILES}" && return

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

	# it is failed for all packages except eepm
	if [ "$BUILDNAME" != "eepm" ] ; then
		set_build_failed
		return
	fi

	INSTALLCOMMAND=$(DISTRNAME=$DISTRNAME $EPMCMD --auto --show-command-only --nodeps install) || fatal "Can't get install command"
	echo "Install built packages with $INSTALLCOMMAND command"
	run_in_chroot "cd $INTBUILT && yes | $INSTALLCOMMAND ${EXPMAINFILES} ${EXPEXTRAFILES}" || set_build_failed
}

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

	assert_var BUILDNAME BUILDARCH BUILDROOT 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

	buildsystempkgs="libtool automake autoconf rpm-build glibc-devel gzip bzip2 pkg-config cpio"

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

	run_in_chroot "epm --auto --verbose --skip-installed install \"$reqs\""
}
