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


# Internal: printout args with add *.$TARGET
expand_filelist()
{
        [ -n"$TARGET" ] || fatal "TARGET is empty"
        declare -a ar
        ar=( $@ )
        #[ -z "$1" ] && return
        for i in `seq 0 $(( ${#ar[@]} - 1))`; do
                echo -n "${ar[i]}*.${TARGET} "
                # hack for Gentoo
                #echo -n "${ar[i]}*.* "
        done
}

prepare_filelist()
{
	[ -z "$MAINFILESLIST$EXTRAFILESLIST" ] && fatal "Logical error with MAINFILESLIST EXTRAFILESLIST"
	# expand file masks by TARGET
	EXPMAINFILES=$(expand_filelist $MAINFILESLIST)
	EXPEXTRAFILES=$(expand_filelist $EXTRAFILESLIST)

	# check for ebuild if gentoo
	if [ "$dist_name" = "Gentoo" ] ; then
		EXPMAINFILES="$EXPMAINFILES ${BUILDNAME}*.ebuild"
	fi

	[ -n "$ADEBUG" ] && echo "EXPMAINFILES: $EXPMAINFILES EXPEXTRAFILES: $EXPEXTRAFILES"
	[ -z "$EXPMAINFILES$EXPEXTRAFILES" ] && fatal "Logical error with EXPMAINFILES EXPEXTRAFILES"

}

build_dist_pkg()
{
case $dist_name in
"Windows")
	return 1;
	;;
"FreeBSD")
	kormod build/freebsd
	kormod build/rpm
	# FIXME
	BUILDERHOME=~/tmp/korinfer
	BUILDROOT=~/tmp/korinfer
	mkdir -p $BUILDERHOME $BUILDROOT
	set_target_var || return 1

	logit "convert src.rpm to target $TARGETPKGDISTRO" convert_rpm_to_target $dist || return 1
	logit "build $dist" build_bsd $dist || return 1
	#logit "build in FreeBSD $dist_ver" build_bsd || return 1
	logit "convert RPM to $TARGET" convert_bsd || return 1
	prepare_filelist
	logit "copying built packages to $dist" copying_bsd || return 1
	[ -n "$BOOTSTRAP" ] && logit "install built" install_bsd
	logit "cleaning" cleaning_bsd || return 1
	;;

"OpenSolaris")
	kormod build/solaris
	logit "build in Solaris $dist_ver" build_solaris || return 1
	prepare_filelist
	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 $dist_ver" build_in_hasher || return 1
	prepare_filelist
	logit "copying built packages to $dist" copying_packages || return 1
	;;

# Generic way
*)
	kormod build/archlinux
	kormod build/gentoo
	kormod build/rpm
	logit "mount root dir" mount_linux || return 1
	set_target_var || return 1

	logit "convert src.rpm to target $TARGETPKGDISTRO" convert_rpm_to_target $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 "build RPM package" build_rpms $dist || return 1
	prepare_filelist

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

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

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

