#!/bin/bash

function print_usage(){
    cat <<END
Usage: $0 <srpm> [scope1 [scope2 ....]]
    This command does koji scratch build for given fedora and epel releases.

Parameters:
    srpm: SRPM file to be scratch-built with koji.

    scopes: releases of what to build. Multiple values are allowed.
        Valid values:
            rawhide: Build rawhide.

	    fedora: Build actives fedora releases, including Rawhide.

	    fedora_1: Build the latest supported fedora releases.
	        This is one release eariler than rawhide.

	    fedora_2: Build the second latest supported fedora releases.
	        This is two releases eariler than rawhide.

            f22 f21 ...: Build the specified fedora releases.

	    epel: Build the currently supported EPEL releases.

	    epel_1: Build the latest supported EPEL releases.

	    epel_2: Build the second latest supported EPEL releases.

	    epel7 el6 ... : The EPEL releases to be built.

        If scopes is not specified, then rawhide and active 
	fedora and EPEL releases are built,
       	as if "rawhide fedora epel" are specified.
END
}
##=== Variable Definition ===
ScriptDir=$(readlink -f `dirname $0`)
declare EXIT_OK=0
declare EXIT_FATAL_UNSPECIFIED=1
declare EXIT_FATAL_INVALID_OPTIONS=3
declare EXIT_FATAL_MISSING_DEPENDENCY=4
declare EXIT_FATAL_UNKNOWN_MODULE=5
declare EXIT_FATAL_FAIL=5
declare EXIT_ERROR_FAIL=20
declare EXIT_RETURN_FALSE=40

##=== Dependency Checking ===
for d in Modules cmake-fedora/Modules ${ScriptDir}/../Modules /usr/share/cmake/Modules;do
    if [ -r $d/CmakeFedoraScript.cmake ];then
	CMakeFedoraScriptCMake=$d/CmakeFedoraScript.cmake
    fi
done
if [ -z "${CMakeFedoraScriptCMake}" ];then
    echo "[Error] CmakeFedoraScript.cmake is not found" > /dev/stderr
    exit $EXIT_FATAL_MISSING_DEPENDENCY
fi

CMakeFedoraKojiCmd=${ScriptDir}/cmake-fedora-koji
if [ ! -x ${CMakeFedoraKojiCmd} ];then
    echo "[Error] cmake-fedora-koji is not found" > /dev/stderr
    exit $EXIT_FATAL_MISSING_DEPENDENCY
fi

CMakeFedoraPkgdbCmd=${ScriptDir}/cmake-fedora-pkgdb
if [ ! -x ${CMakeFedoraPkgdbCmd} ];then
    echo "[Error] cmake-fedora-pkgdb is not found" > /dev/stderr
    exit $EXIT_FATAL_MISSING_DEPENDENCY
fi

for cmd in curl fedpkg  ;do
    CMakeFedoraScriptOptArray=(-D cmd=find_program verbose_level=1  )
    CMakeFedoraScriptOptArray+=( -D "names=$cmd")
    CmdPath=`cmake "${CMakeFedoraScriptOptArray[@]}" -P ${CMakeFedoraScriptCMake}`
    if [ $? -ne 0 ];then
	exit $EXIT_FATAL_MISSING_DEPENDENCY
    fi

    VarName=`tr a-z A-Z <<<$cmd`_CMD
    eval "$VarName=$CmdPath"
done

##=== Parameter Parsing ===
if [ $# = 0 ]; then
    print_usage
    exit $EXIT_FATAL_INVALID_OPTIONS
fi

Srpm=$1
shift

if [[ -z $Srpm ]];then
    print_usage
    exit $EXIT_FATAL_INVALID_OPTIONS
else
    Srpm=`readlink -f $Srpm`
fi

if [[ ! -r "$Srpm" ]];then
    echo "[Fatal] Failed to read $Srpm" > /dev/stderr
    exit $EXIT_FATAL_INVALID_OPTIONS
fi

#TargetArray=($($CMakeFedoraKojiCmd target "$@" | xargs) )
TargetArray=($($CMakeFedoraKojiCmd branch "$@" | xargs) )

echo -n "Targets to process:"
(IFS=' ' echo "${TargetArray[@]}")

Failed=

for t in "${TargetArray[@]}";do
    ## Workaround Bug 1186994 - koji error: SysCallError: (-1, 'Unexpected EOF') when scratch build SRPM 
    ## if ! ${KOJI_CMD} build --scratch $t $Srpm; then
    if ! ${FEDPKG_CMD} --dist $t scratch-build --srpm $Srpm; then
	Failed+=" $t"
    fi
done

if [ -n "$Failed" ]; then
    echo "Failed targets:$Failed" > /dev/stderr
    exit $EXIT_ERROR_FAILED
fi
exit $EXIT_OK

