#!/bin/sh
##
#  Korinf project
#
#  Main korinf script for build packages
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005-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/>.
##

# Example in the three lines:
#  kormod korinf
#  export REBUILDLIST=Fedora/10
#  build_package your-package-name


kormod message main check_built


# build binary packages according to REBUILDLIST
# arg: package_name [HELPER]
build_package()
{
	# FIXME: for compatibility
	set_log_dir

	#echo
	#echo "=============================================== "
	echo | write_report

	local BUILDNAME
	test -n "$1" && BUILDNAME=$1 || fatal "Missed build_rpm param"

	# disable changelog editing
	export EDITOR=

	MAINFILESLIST=$MAINFILES
	EXTRAFILESLIST=$EXTRAFILES

	# FIXME: remove HELPER support when unneeded
	# Use default value for simple backward compatibility
	if [ -z "$MAINFILESLIST$EXTRAFILESLIST" ] ; then
		if [ "$2" = "HELPER" ] ; then
			EXTRAFILESLIST="${BUILDNAME}"
		else
			MAINFILESLIST="${BUILDNAME}"
		fi
	fi

	if [ -z "$QUIET" ] ; then
		echo "Build $BUILDNAME package"
		echo "Source dir: $SOURCEPATH"
		echo "Target dir: $TARGETPATH"
	fi

	if [ -z "$SOURCEPATH" ] || [ -z "$TARGETPATH" ] ; then
		fatal "Source or target path is not set"
	fi

	if [ -n "$CHECKPACKAGE" ] ; then
		check_built_package "$BUILDNAME"
		return $?
	fi

	main_build
}


# build source packages according to REBUILDLIST
# args: package_name
build_srcrpm()
{
	MAKESPKG=1
	build_package $@
}

# args: 1) path to project dir (where versions) 2) package name [3) target subdir ] [ 4) other project version ]
build_project()
{
	local SP=$1
	local PACKAGE=$2
	local TP=$3
	shift 3

	# 4 arg: possible it is options
	if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
		echo "Korinf build script:"
		echo "Usage: script [target_system] [project_version]"
		echo "	target_system - Mandriva/2009 or test, all, deb, etc."
		echo "	project_version - 1.0.10 or last"
		echo "you can use following options:"
		echo "	-b - buildstrap (install package(s) after build)"
		echo "	-q - enable quiet mode"
		echo "	-c - print build status list"
		echo "	-f - force rebuild package ever if it up-to-date"
		echo "	-r - install packages required for build package"
		fatal "Help message"
	fi

	if [ "$1" = "-q" ] ; then
		QUIET=1
		shift
	fi

	if [ "$1" = "-c" ] ; then
		CHECKPACKAGE=1
		shift
	fi

	if [ "$1" = "-b" ] ; then
		BOOTSTRAP=1
		REBUILDPACKAGE=1
		shift
	fi

	if [ "$1" = "-f" ] ; then
		REBUILDPACKAGE=1
		shift
	fi

	if [ "$1" = "-r" ] ; then
		INSTALLREQUIREDPACKAGE=1
		shift
	fi

	# 4/5 arg:
	# do not shift args more
	if [ -n "$1" ] ; then
		REBUILDLIST=$1
	fi

	# 5/6 arg:
	if [ -z "$2" ] ; then
		test -d "$SP/last" && SP=$SP/last
	else
		SP=$SP/$2
	fi

	# enable VERBOSE
	if [ -z "$QUIET" ] ; then
		export VERBOSE=1
	fi

	TARGETPATH=$(readlink -f $SP)
	SOURCEPATH=$TARGETPATH/sources

	[ -n "$TARGETPATH" ] || fatal "Cannot find $SP, check it"

	PROJECTVERSION=$(basename $TARGETPATH)
	[ -n "$ADEBUG" ] && echo "PROJECTVERSION=$PROJECTVERSION"

	# set according to base TARGETPATH, so set before TP
	set_rebuildlist

	[ -z "$TP" ] || TARGETPATH=$TARGETPATH/$TP

	build_package $PACKAGE
}
