#!/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 log mainbuild mount

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


# build binary packages according to REBUILDLIST
# arg: package_name
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=

	# used in external get_distro_list
	export SKIPBUILDLIST

	MAINFILESLIST=$MAINFILES
	EXTRAFILESLIST=$EXTRAFILES

	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

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

	# always failed if gpg is missed
	rpm --checksig $BUILDSRPM || fatal "GPG check failed"

	# Assure we have no too open perms on private key
	if [ -w "$PRIVATESSHKEY" ] ; then
		chmod 600 "$PRIVATESSHKEY" || fatal "SSH key permissions problem"
	fi

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

	trap exit_handler EXIT HUP INT QUIT PIPE TERM

	if [ -n "$CHECKPACKAGE" ] ; then
		kormod check_built
		check_built_package "$BUILDNAME"
	elif [ -n "$CHECKREQUIRES" ] ; then
		kormod check_reqs
		check_package_requires "$BUILDNAME"
	else
		mainbuild
	fi

	local rc=$?
	# disable trap before exit
	trap - EXIT
	return $rc
}


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

print_build_project_options()
{
	echo "	-b - buildstrap (build if needed, install package(s) after build)"
	echo "	-B - force buildstrap (even if it is up-to-date)"
	echo "	-q - enable quiet mode"
	echo "	-c - print build status list"
	echo "	-f - force rebuild only failed packages"
	echo "	-F - force rebuild all packages even if it is up-to-date"
	echo "	-i - install packages required for build package"
	echo "	-I - force install packages required for build package"
	echo "	-r - check install requires"
	echo "	-R - check install requires with full output"
}

# 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 (lists from etc/lists) or all (by distr.list)"
		echo "	project_version - 1.0.10 or last, testing, unstable"
		echo "you can use following options:"
		print_build_project_options
		fatal "Help message"
	fi

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

	if [ "$1" = "-c" ] ; then
		CHECKPACKAGE=1
		echo "Run checking package status"
		shift
	fi

	if [ "$1" = "-b" ] ; then
		BOOTSTRAP=1
		REBUILDPACKAGE=1
		echo "Run with install built package after done"
		shift
	fi

	if [ "$1" = "-B" ] ; then
		BOOTSTRAP=1
		FORCEREBUILDPACKAGE=1
		echo "Run with install built package after force build done"
		shift
	fi

	if [ "$1" = "-f" ] ; then
		echo "Run with force to rebuild package"
		REBUILDPACKAGE=1
		shift
	fi

	if [ "$1" = "-F" ] ; then
		echo "Run with force to rebuild all packages"
		REBUILDPACKAGE=1
		FORCEREBUILDPACKAGE=1
		shift
	fi

	if [ "$1" = "-i" ] ; then
		echo "Run install required package procedure"
		INSTALLREQUIREDPACKAGE=1
		shift
	fi

	if [ "$1" = "-I" ] ; then
		echo "Force install required package procedure"
		INSTALLREQUIREDPACKAGE=1
		FORCEREBUILDPACKAGE=1
		shift
	fi

	if [ "$1" = "-r" ] || [ "$1" = "-R" ] ; then
		CHECKREQUIRES="$1"
		echo "Run check package requirements status"
		shift
	fi

	# if unknown param
	if [ -n "$1" ] && [ -z "${1/-?/}" ] ; then
		fatal "Unknown option: $1"
	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)

	# set SOURCEPATH, add sources if not yet
	if echo $TARGETPATH | grep -q /sources ; then
		SOURCEPATH=$TARGETPATH
	else
		SOURCEPATH=$TARGETPATH/sources
	fi

	[ -n "$TARGETPATH" ] || fatal "Cannot find target path $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
}
