#!/bin/sh
##
#  Korinf project
#
#  Common script for build any package in build farm
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009, 2012, 2013, 2015
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2012, 2013, 2015
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU 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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

# load common functions, compatible with local and installed script
. `dirname $0`/../share/eterbuild/korinf/common

kormod korinf
load_mod spec rpm etersoft strings

eval lastarg=\${$#}

if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$lastarg" = "-h" ] || [ "$lastarg" = "--help" ] ; then
	echo "Use: korinf [options] <package.spec|PackageName> <System/Version> [TARGETDIR] [TARGETVERSION] [options]"
	echo "     with package.spec use target as rpmpub"
	echo "     with PackageName build in Build Farm"
	echo "     TARGETDIR - target dir for Build Farm instead default"
	echo "     version - target project version"
	echo "Options:"
	print_build_project_options

	echo
	echo "Examples: "
	echo "  $ korinf mc.spec ALTLinux/p7             - build mc package from git repo in ALTLinux/p7 "
	echo "  $ korinf wine Fedora 1.7.50              - build wine package from target dir in all Fedora "
	echo "  $ korinf package SUSE /path/to/sources   - build package from /path/to/sources in all SUSE "
	# return error if run without option
	[ -n "$1" ]
	exit
fi

OPTIONS=
ARGS=

# parse options
while test $# -gt 0
do
	case "$1" in
		-*)
			OPTIONS="$OPTIONS $1"
			;;
		"")
			fatal "Empty args are not supported"
			;;
		*)
			ARGS="$ARGS $1"
			;;
	esac
	shift
done

#             1                    2              3             4
# <package.spec|PackageName> <System/Version> [TARGETDIR] [TARGETVERSION]
# TODO: support "ALTLinux/{p6,p7} SUSE/12"
set -- $ARGS

BUILDNAME="$1"
# in any way we print help if args empty above
[ -n "$BUILDNAME" ] || fatal "Package name of spec file is missed. Use --help for details."

REBUILDLIST="$2"
[ -n "$REBUILDLIST" ] || fatal "System(s) for build is missed. Use --help for details."

# hack
if is_absolute_path "$REBUILDLIST"; then
	fatal "It looks target distro missed before target dir (args: '$1 $2'). Use 'all' for build for all target systems. Use --help for details."
fi

shift 2

ETERDESTSRPM=
ALPHA=
ALPHAARG=

# parse optional args
#      1   < - >    2
# [TARGETDIR] [TARGETVERSION]

if [ -n "$2" ] ; then
	if is_absolute_path "$1"; then
		ETERDESTSRPM="$1"
		# TARGET VERSION
		ALPHA="$2"
		ALPHAARG="-r $2"
	elif is_absolute_path "$2" ; then
		ETERDESTSRPM="$2"
		# TARGET VERSION
		ALPHA="$1"
		ALPHAARG="-r $1"
	else
		fatal "No target between optional arguments '$1 $2'"
	fi
	shift 2
elif is_absolute_path "$1"; then
	ETERDESTSRPM="$1"
	shift 1
elif [ -n "$1" ] ; then
	# TARGET VERSION
	ALPHA="$1"
	ALPHAARG="-r $1"
	shift 1
fi

[ -n "$1" ] && fatal "Unknown params $@"

# do not realize subdir in target dir
SUBDIR=

# build prepared package from target
if ! rhas "$BUILDNAME" "\.spec$" ] ; then

	# use default target
	[ -n "$ETERDESTSRPM" ] || ETERDESTSRPM="$BUILDFARMDIR"

	docmd build_project "$ETERDESTSRPM" "$BUILDNAME" "$SUBDIR" "$REBUILDLIST" $ALPHA $OPTIONS
	exit $?
fi

# build from spec
SPECNAME=$BUILDNAME
BUILDNAME=$(eval_spec $SPECNAME | get_var "Name")

# if have no target in args, need get default
if [ -z "$ETERDESTSRPM" ] ; then
	# try get target from spec
	ETERDESTSRPM=$(get_etersoft_srpm_path $SPECNAME $ALPHA)
	# else use default target
	[ -n "$ETERDESTSRPM" ] || ETERDESTSRPM="$BUILDFARMDIR"
fi

docmd $ETERBUILDBIN/rpmpub $ALPHAARG $SPECNAME $ETERDESTSRPM || exit

test -d "$ETERDESTSRPM" || fatal "You need run with exists dir to publish src.rpm project (I checked $ETERDESTSRPM, it is non exists)"

docmd build_project $(realpath -m $ETERDESTSRPM) "$BUILDNAME" "$SUBDIR" "$REBUILDLIST" $ALPHA $OPTIONS
