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

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

	kormod build/freebsd
	kormod build/rpm

	# used in convert_rpm_to_target
	BUILDERHOME=~/tmp/korinfer
	mkdir -p $BUILDERHOME


	logit "convert src.rpm to target $PKGVENDOR" convert_rpm_to_target $dist || return 1
	logit "build $dist" build_bsd $dist || return 1
	logit "convert RPM to $PKGFORMAT" convert_bsd || return 1

	logit "copying built packages to $dist" copying_bsd || return 1
	[ -n "$BOOTSTRAP" ] && logit "install built" install_bsd
	logit "cleaning" cleaning_bsd || return 1
	;;

"OpenSolaris")
	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install 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

	logit "build in hasher $DISTRVERSION" build_in_hasher || return 1
	logit "copying to FTP dir" copying_packages || return 1
	;;

# Generic way
*)
	#kormod build/archlinux
	kormod build/gentoo
	kormod build/rpm
	kormod check_reqs

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

	if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		logit "install requires" install_req $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

	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

	if [ -z "$MAKESPKG" ] && [ -z "$BOOTSTRAP" ] ; then
		logit "test built packages requirements" test_package_requirements || return 1
	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
}

