#!/bih/sh

#
# Qt4 distribution RPM archive creator.
#
# NOTE: This script requires the basic set of unix tools such as cp, mkdir,
# pwd, readlink, find etc.
#

#
# Defaults
#

spec_file=qt4.spec

#
# Functions
#

die() { echo "ERROR: $@"; cd "$start_dir"; exit 1; }

run()
{
    echo "$@"
    "$@" || die \
"Last command failed (exit status $?)."
}

cmd_all()
{
    local src_base="$1"

    run $env_cmd rpmbuild \
        -D "QT_SOURCE_TREE $src_base" \
        -ba $spec_file
}

cmd_build()
{
    local src_base="$1"

    run $env_cmd rpmbuild \
        -D "QT_SOURCE_TREE $src_base" \
        -D "skip_prep_export 1" -D "skip_prep_clean 1" \
        --short-circuit -bc $spec_file
}

cmd_install()
{
    local src_base="$1"

    run $env_cmd rpmbuild \
        -D "QT_SOURCE_TREE $src_base" \
        -D "skip_prep_export 1" -D "skip_prep_clean 1" \
        --short-circuit -bi $spec_file
}

cmd_rpm()
{
    run $env_cmd rpmbuild \
        -D "skip_prep_export 1" -D "skip_prep_clean 1" \
        -D "skip_build 1" -D "skip_install 1" \
        --short-circuit -bb $spec_file
}

#
# Main
#

script_path=$(readlink -e $0)
script_dir=${script_path%/*}
script_name=$(basename $0)

start_dir=$(pwd)

[ -f "$start_dir/env.sh" ] && . "$start_dir/env.sh"

env_cmd=
[ -f "$start_dir/env.cmd" ] && env_cmd="cmd /c env.cmd"

# Parse arguments

cmd_help()
{
    echo \
"
Usage:
  $script_name all <srcdir>     Do everything (RPM, SRPM, ZIP)
  $script_name build <srcdir>   Build product (in BUILD/<product>)
  $script_name install <srcdir> Install product (to BUILDROOT/<product>)
  $script_name rpm              Build RPMs only

Options:
  <srcdir>      Qt SVN source tree location
"
}

case "$1" in
    all|build|install)
        if [ -n "$2" ]; then
            [ -d "$2" ] || die "'$2' is not a directory."
            cmd_$1 $(echo "$2" | tr '\\' '/')
        else
            cmd_help
        fi;;
    rpm) cmd_rpm;;
    -h|-?|--help|*) cmd_help;;
esac

# end of story

exit 0
