#!/bin/sh
##
#  Korinf project
#
#  Common script for build any package in build farm
#
#  Copyright (c) Etersoft <http://etersoft.ru> 2005, 2006, 2007, 2009,2012
#  Copyright (c) Vitaly Lipatov <lav@etersoft.ru> 2009, 2012
#
#  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


if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
	echo "Use: korinf [-r version ] [System/Version] package.spec|PackageName [TARGETDIR] [options]" >&2
	echo "     with package.spec use target as rpmpub" >&2
	echo "     with packageName build in BuildFarm" >&2
	echo "     TARGETDIR - target dir instead default" >&2
	echo "     version - set target project version" >&2
	echo "you can use following options:" >&2
	echo "	-v - set version of the target project" >&2
	print_build_project_options >&2
	exit 1
fi

# publish to unstable by default
ALPHA=
if [ "$1" = "-v" ] ; then
	ALPHA=$2
	ALPHAARG="$1 $2"
	shift 2
fi

PARAM=
# if some other args
if [ -n "$1" ] && [ -z "${1/-?/}" ] ; then
	PARAM=$1
	shift
fi

# if some 2nd args
if [ -n "$1" ] && [ -z "${1/-?/}" ] ; then
	PARAM="$PARAM $1"
	shift
fi

if [ -n "$3" ] && is_dirpath "$3"; then
	# System/Version|ALL package target/path
	REBUILDLIST="$1"
	BUILDNAME="$2"
	ETERDESTSRPM="$3"
	shift 3
elif [ -n "$2" ] && is_dirpath "$2"; then
	# package target/path
	BUILDNAME="$1"
	ETERDESTSRPM="$2"
	shift 2
elif [ -n "$2" ] ; then
	# System/Version|ALL package
	REBUILDLIST="$1"
	BUILDNAME="$2"
	shift 2
elif [ -n "$1" ] ; then
	# package
	BUILDNAME="$1"
	shift
else
	fatal "Cannot recognize '$@' options"
fi

# TODO: for BuildFarm we had packagename by default
SUBDIR=

[ -n "$PARAM" ] && [ ! "$@" = " " ] && echo "Run with additional options: '$PARAM $@'"

if [ -n "${BUILDNAME/*.spec/}" ] ; then
	# if package name, not spec
	[ -n "$ETERDESTSRPM" ] || ETERDESTSRPM=$BUILDFARMDIR

	# rearrange REBUILDLIST
	OQUIET=$QUIET
	QUIET=1 TARGETPATH=$ETERDESTSRPM set_rebuildlist
	QUIET=$OQUIET
	build_project $ETERDESTSRPM "$BUILDNAME" "$SUBDIR" $PARAM $@
	exit $?
else
	# if run with spec
	SPECNAME=$BUILDNAME
	if [ -z "$ETERDESTSRPM" ] ; then
		ETERDESTSRPM=$(get_etersoft_srpm_path $SPECNAME "$ALPHA")
		if [ ! -d "$ETERDESTSRPM" ] ; then
			ETERDESTSRPM=$BUILDFARMDIR/sources
		fi
	fi
	BUILDNAME=$(eval_spec $SPECNAME | get_var "Name")
	$ETERBUILDBIN/rpmpub $ALPHAARG $SPECNAME $ETERDESTSRPM || exit 1
fi

# Build from spec (if default BuildFarm, get from spec)
if [ "$ETERDESTSRPM" = "$BUILDFARMDIR" ] ; then
	ETERDESTSRPM=$(get_etersoft_srpm_path $SPECNAME "$ALPHA")
fi

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

build_project $ETERDESTSRPM/.. "$BUILDNAME" "$SUBDIR" $PARAM $@
