#!/bin/bash
##
#  Korinf project
#
#  Main build cycle 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 mount copying install log
kormod distro


exit_handler()
{
    local rc=$?
    trap - EXIT
    warning "Interrupted, exiting..."
    end_umount
    exit $rc
}

main_build()
{

trap exit_handler EXIT HUP INT QUIT PIPE TERM

[ -n "$ADEBUG" ] && echo "BUILDNAME: $BUILDNAME"

# HACK: Url for remote building
SOURCEURL=$(echo $SOURCEPATH | sed -e "s|/var/ftp/|ftp://server/|g")

# On systems with portage change wine package name
# FIXME: set PACKAGE individually
[ "$BUILDNAME" = "wine" ] && PACKAGE="wine-etersoft-public" || PACKAGE=$BUILDNAME

# IMPROVE: get src.rpm from some private dir
BUILDSRPM=$(get_src_package "$SOURCEPATH" $BUILDNAME || fatal "Can't find package for $BUILDNAME")

PACKAGEVERSION=$(rpm -qp "$BUILDSRPM" --queryformat="%{VERSION}")
[ -n "$PACKAGEVERSION" ] || fatal "Can't get version from $BUILDSRPM package"

PACKAGERELEASE=$(rpm -qp "$BUILDSRPM" --queryformat="%{RELEASE}" | sed -e "s|^alt|eter|g")
[ -n "$PACKAGERELEASE" ] || fatal "Can't get release from $BUILDSRPM package"

if [ -n "$NIGHTBUILD" ] ; then
	rpm --checksig $BUILDSRPM || fatal "GPG check failed"
else
	if [ -n "$QUIET" ] ; then
		rpm --checksig $BUILDSRPM >/dev/null || warning "GPG check failed"
	else
		rpm --checksig $BUILDSRPM || warning "GPG check failed"
	fi
fi

# disable gear using due pre hasher repacking problem
export IGNOREGEAR=1

CMDRE=$(get_distro_list $REBUILDLIST)
[ -z "$CMDRE" ] && fatal "build list is empty"

for dist in $CMDRE ; do

	# fill after hasher
	BUILTBINPKGLIST=

	# Set target dir for built packages
	DESTDIR=$TARGETPATH/$dist
	# Set target URL for get tarballs or checking
	DESTURL=$(echo $DESTDIR | sed -e "s|$FTPDIR/|$PUBLICURL/|g")

	# skip linked destination (TODO: do not handle it at all)
	[ -L "$DESTDIR" ] && echo "$dist: Skip linked target" && continue

	[ "$dist" = "Windows" ] && echo "Skip $dist" && continue

	# FIXME: Hack
	[ "$TARGETPRIVPATH" ] && DESTDIR=$TARGETPRIVPATH

	# fills dist_arch, dist_name, dist_ver from dist
	parse_dist_name $dist
	[ -z "$dist_name" ] && warning "Empty dist_name for $dist"
	BUILDARCH=$dist_arch

	# Initialize LOGDIR and LOGFILE variables
	init_dist_log "$dist"

	if do_need_build_package $BUILDNAME || [ -n "$REBUILDPACKAGE" ] || [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
		#logit "`basename $BUILDSRPM .src.rpm`"
		logit "package $BUILDNAME"
	else
		logit "$BUILDNAME is up-to-date" SKIPPED
		# remove fatal flag if checking is passed
		rm -fv "$DESTDIR/log/$BUILDNAME.autobuild.failed"
		echo `date` $BUILDNAME on $dist PASSED | write_report
		continue
	fi

	# Clean log
	reset_dist_log "$dist"

	# Run build and check result
	if build_dist_pkg ; then
		logit "copying log" copying_log || return 1
	else
		# Get error, umount if do not need debug
		if [ -z "$ADEBUG" ] ; then
			logit "umount" end_umount
		fi

		logit "copying log from broken build" copying_log

		# Stop immediately if we in auto build
		if [ -n "$NIGHTBUILD" ] ; then
			echo `date` $BUILDNAME on $dist FATAL | write_report
			fatal "Fatal autobuild"
		fi
	fi

	echo `date` $BUILDNAME on $dist DONE | write_report
done

# disable trap before exit
trap - EXIT
}
