#!/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 status
kormod distro check_built

mainbuild()
{
local BUILTLOGFILE=$ALOGDIR/distrobuilt.log
local RES=0
rm -f $BUILTLOGFILE
for dist in $(get_distro_list $REBUILDLIST) ; do

	# FIXME: add i586 support
	#dist is x86_64/Name/Version or Name/Version
	echo $dist >>$BUILTLOGFILE

	# Set target dir for built packages
	DESTDIR=$TARGETPATH/$dist

	local LINKED=$(get_linked_system $dist)

	# FIXME: Hack for pseudo build 32 bit wine on x86_64 systems
	if rhas "$BUILDNAME" "wine" ; then
		local WINELINKED=$(get_linked_x86_64_wine_system $dist)
		[ -n "$WINELINKED" ] && LINKED=$WINELINKED
	fi

	if [ -n "$LINKED" ] ; then
		if [ ! -L "$DESTDIR" ] && [ -d "$DESTDIR" ] ; then
			warning "Removing target dir for replace to link"
			rm -rfv "$DESTDIR" && success
		fi

		# Note: Small hack
		local LFROM=../$LINKED
		rhas "$dist" x86_64 && LFROM=../../$LINKED

		if [ -L "$DESTDIR" ] ; then
			# TODO: check linking
			#logit "Link $dist to $(readlink $DESTDIR) already exists" SKIPPED
			if [ "$(readlink "$DESTDIR")" != "$LFROM" ] ; then
				warning "Overwrite already existed link $(readlink $DESTDIR) to $LFROM"
				rm -rfv "$DESTDIR"
			else
				logit "Linked to $(readlink $DESTDIR)" SKIPPED
			fi
		fi
		if [ ! -L "$DESTDIR" ] ; then
			mkdir -p "$(dirname "$DESTDIR")"
			logit "Create link $dist to $LINKED" ln -s "$LFROM" "$DESTDIR"
		fi
		# replace dist with linked
		dist="$LINKED"
		# Set target dir for built packages
		DESTDIR="$TARGETPATH/$dist"
		# if already built
		grep -q "$dist" $BUILTLOGFILE && continue
	fi

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

	# disabled, never change exists links!
	# remove linked destination if exists
	#if [ -L "$DESTDIR" ] ; then
	#	warning "$dist: Removing link target"
	#	rm -vf $DESTDIR
	#fi

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

	# fills $BUILDARCH, $DISTRNAME, $DISTRVERSION
	parse_dist_name $dist
	[ -z "$DISTRNAME" ] && fatal "Empty DISTRNAME for $dist"

	# fills $PKGFORMAT, $PKGVENDOR, $RPMVENDOR
	set_target_var

	INITIALBOOTSTRAP=
	# detect if we will not epm use possibility
	if [ "$BUILDNAME" == "eepm" ] || [ "$BUILDNAME" == "rpm-build-altlinux-compat" ] ; then
		INITIALBOOTSTRAP=1
	fi

	# Initialize LOGDIR and LOGFILE variables
	init_dist_log "$dist"

	if [ -n "$FORCEREBUILDPACKAGE" ] ; then
		loginfo "forced build package $BUILDNAME"
	else 
		if is_build_failed ; then
			if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
				loginfo "install for broken build"
			elif [ -z "$REBUILDPACKAGE" ] ; then
				logit "build was already broken in manual build" SKIPPED
				echo "`date` $BUILDNAME on $dist ALREADY FAILED" | write_report
				RES=1
				continue
			else
				loginfo "rebuild previously failed package $BUILDNAME"
			fi
		else
			#if [ -n "$INSTALLREQUIREDPACKAGE" ] ; then
			#	logit "skip install for built package" SKIPPED
			#	continue
			if ! do_need_build_package $BUILDNAME ; then
				logit "$BUILDNAME is up-to-date" SKIPPED
				# remove fatal flag if checking is passed
				clear_build_status
				echo `date` $BUILDNAME on $dist PASSED | write_report
				continue
			else
				loginfo "build package $BUILDNAME"
			fi
		fi
	fi

	# Clean log
	reset_dist_log "$dist"

	# Run build and check result
	if build_dist_pkg ; then
		logit "copying log" copying_log || fatal "Can't copying log"
	else
		[ -z "$INSTALLREQUIREDPACKAGE" ] && logit "copying sources of broken package" copying_sources

		# 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
		set_build_failed

		# Stop immediately if we in auto build
		if [ -n "$NIGHTBUILD" ] ; then
			echo `date` $BUILDNAME on $dist FATAL | write_report
			fatal "Fatal autobuild"
		fi
		RES=1
	fi
print_status_code()
{
	[ "$1" = "0" ] && echo "TRUE" && return
	echo "FALSE ($1)"
}
	echo `date` "$BUILDSRPM on $dist DONE with status '$(print_status_code $RES)'" | tee -a $LOGFILE | write_report
done

return $RES
}
