#!/bin/bash
##
#  Korinf project
#
#  Main build related functions
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 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/>.
##

kormod convert filelist

build_dist_pkg()
{
# FIXME: more correctly use PKGVENDOR here
case $DISTRNAME in
"Windows")
	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install build requires" SKIPPED
		return 0
	fi

	kormod build/windows

	logit "build $dist" build_windows $dist || return 1

	logit "copying built packages to $dist" copying_windows || return 1
	logit "cleaning" clean_windows || return 1
	;;

"OpenSolaris")
	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install build requires" SKIPPED
		return 0
	fi

	kormod build/solaris

	# FIXME: rewrite build rules
	# used in convert_rpm_to_target
	BUILDERHOME=/var/empty

	logit "build in $DISTRNAME $DISTRNAME" build_solaris || return 1

	logit "copying built packages to $dist" copying_solaris || return 1
	logit "cleaning" cleaning_solaris || return 1
	;;

# build in hasher
"ALTLinux")
	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install requires" SKIPPED
		return 0
	fi

	kormod build/hasher
	kormod writedeps

	logit "build in hasher $DISTRVERSION" build_in_hasher || return 1
	if [ "$BUILDARCH/$DISTRNAME/$DISTRVERSION" = "i586/ALTLinux/Sisyphus" ] ; then
		logit "write dependencies" write_altdeps || return 1
	fi
	logit "copying to FTP dir" copying_packages || return 1
	;;

# Generic way
*)
	kormod build/rpm
	kormod check_reqs

	logit "mount root dir" mount_linux $dist || return 1

	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install build requires only" install_build_requires $dist || return 1
		logit "umount" end_umount || return 1
		return 0
	fi

	logit "convert src.rpm to target $PKGVENDOR" convert_rpm_to_target $dist || return 1

	if [ -z "$BOOTSTRAP$INITIALBOOTSTRAP" ] ; then
		logit "assure build requires" install_build_requires || return 1
	fi

	logit "build RPM package" build_rpms $dist || return 1

	if [ "$PKGFORMAT" != "rpm" ] && [ -z "$MAKESPKG" ] ; then
		logit "convert RPM to $PKGFORMAT ($PACKAGERELEASE$PKGVENDOR)" convert_rpm || return 1
	fi

	# skip checking for correct eepm buildstrap
	if [ -z "$BOOTSTRAP$INITIALBOOTSTRAP" ] ; then
		# TODO: move to before build, but have no built packages list there (like correct EXPMAINFILES)
		if [ -z "$MAKESPKG" ] ; then
			logit "test built packages requirements" test_package_requirements || return 1
			if [ "$PKGFORMAT" = "rpm" ] ; then
				logit "test real built packages requirements" test_package_files_requirements || return 1
			fi
		fi
	fi

	if [ -n "$BOOTSTRAP" ] && [ -z "$MAKESPKG" ]; then
		logit "install built packages" install_built || return 1
	fi

	logit "copying to FTP dir" copying_packages || return 1
	logit "umount" end_umount || return 1
	;;
esac
}

