#!/bin/bash
#
# build_rpm
#

VERSION="1.2"
top_directory=~/husky_build

die()
{
    printf '%s\n' "$1" >&2
    exit 1
}

show_help()
{
    cat <<EOF

Download the necessary sources and build RPMs for the programs specified
in PROGRAMS setting in huskymak.rpm.cfg and for the libraries they depend on.
Usage:
    build_rpm [-d|--dir TOP_DIRECTORY] [-r|--chroot CONFIG] \
              [-n|--no-update] [-j JOBS]
    build_rpm [-v|--version]
    build_rpm [-h|-\?|--help]
Options:
    -d TOP_DIRECTORY
    --dir TOP_DIRECTORY
        It is the same TOP_DIRECTORY as you have used in init_rpm_build.

    -r CONFIG
    --chroot CONFIG
    --chroot=CONFIG
        Use specified chroot configuration as defined in
        ~/.config/mock/<CONFIG>.cfg or /etc/mock/<CONFIG>.cfg. This option may
        be used for cross-build. Optionally if CONFIG ends in '.cfg', it is
        interpreted as full path to config file. If the option is not specified,
        uses the chroot config linked to by /etc/mock/default.cfg.

    -n
    --no-update
        Do not update Makefile, huskymak.rpm.cfg, husky.spec.in,
        spec_configure, build_rpm

    -j JOBS
        JOBS is a positive integer. It specifies the number of makefile
        recipes to run simultaneously. If you omit the option, make runs
        as many recipes simultaneously as possible. To switch the parallel
        makefile execution off, use "-j 1".

    -v
    --version
        Print the script version and exit.

    -h
    -?
    --help
        Print this help and exit.

EOF
}

help=0
config=
no_update=0
jobs=-j

while :
do
    case $1 in
        -h|-\?|--help)
            help=1
            break
            ;;
        -v|--version)
            echo "version = $VERSION"
            exit
            ;;
        -d|--dir)
            if [ "$2" ]
            then
                top_directory="$2"
                [ "${top_directory%"${top_directory#?}"}" != '/' ] && \
                    [ "${top_directory%"${top_directory#??}"}" != '~/' ] && \
                    die 'ERROR: the directory must start with "/" or "~/"'
                shift
            else
                die 'ERROR: "-d" or "--dir" requires a non-empty option argument'
            fi
            ;;
        -r|--chroot)
            if [ -z "$(whereis -b mock | cut -d: -f2)" ]
            then
                die 'ERROR: to use "--chroot" (or "-r") you have to install mock'
            fi
            if [ "$2" ]
            then
                config="-r $2"
                help=0
                shift
            else
                die 'ERROR: "--chroot" requires a non-empty option argument'
            fi
            ;;
        --chroot=?*)
            if [ -z "$(whereis -b mock | cut -d: -f2)" ]
            then
                die 'ERROR: to use "--chroot" (or "-r") you have to install mock'
            fi
            config="-r ${1#*=}"
            help=0
            ;;
        --chroot=)
            die 'ERROR: "--chroot" requires a non-empty option argument'
            ;;
        -n|--no-update)
            no_update=1
            ;;
        -j)
            if [ "$2" ]
            then
                if [ -z "${2##*[!0-9]*}" ] || [ "$2" -eq 0 ]
                then
                    die 'ERROR: "-j" requires a positive integer argument'
                else
                    jobs=-j$2
                    shift
                fi
            else
                die 'ERROR: "-j" requires a positive integer argument'
            fi
            ;;
        -*)
            printf 'WARN: Unknown option (ignored): %s\n' "$1" >&2
            ;;
        *)
            break
    esac
    shift
done

if [ $help -eq 1 ]
then
    show_help
    exit
fi

# Check that the script is not run by root
[ "$(id -u)" -eq 0 ] && die "DO NOT run this as root"

[ "${top_directory%?}/" = "${top_directory}" ] && top_directory="${top_directory%?}"
cd $top_directory
pushd husky >& /dev/null

make $jobs update
[ "$?" -ne 0 ] && exit 1

restart=0

if [ "$no_update" -eq 0 ] && \
   [ -n "$(diff $top_directory/husky/Makefile huskybse/Makefile)" ]
then
    cp -a -f huskybse/Makefile ./
fi

if [ "$no_update" -eq 0 ] && \
   [ -n "$(diff $top_directory/husky/husky.spec.in huskybse/husky.spec.in)" ]
then
    cp -a -f huskybse/husky.spec.in ./
fi

if [ "$no_update" -eq 0 ] && \
   [ -n "$(diff $top_directory/spec_configure huskybse/script/spec_configure)" ]
then
    cp -a -f huskybse/script/spec_configure $top_directory/
fi

if [ "$no_update" -eq 0 ] && \
   [ -n "$(diff $top_directory/huskymak.rpm.cfg.new huskybse/huskymak.rpm.cfg)" ]
then
    mv $top_directory/huskymak.rpm.cfg.new $top_directory/huskymak.rpm.cfg.old
    cp -a huskybse/huskymak.rpm.cfg $top_directory/huskymak.rpm.cfg.new
    if [ ! -e ./huskymak.cfg ]
    then
        cp -a $top_directory/huskymak.rpm.cfg.new huskymak.cfg
    fi
    echo
    echo "\"huskybse/huskymak.rpm.cfg\" has changed"
    echo
    echo "A new version of \"huskybse/huskymak.rpm.cfg\" is now"
    echo "in \"$top_directory/huskymak.rpm.cfg.new\" and its old version"
    echo "is now in \"$top_directory/huskymak.rpm.cfg.old\"."
    echo "Please adjust your \"$top_directory/husky/huskymak.cfg\" to the changes"
    echo "in \"$top_directory/huskymak.rpm.cfg.new\" and run \"build_rpm\" once more."
    echo
    restart=1
fi

if [ "$no_update" -eq 0 ] && \
   [ -n "$(diff $top_directory/build_rpm huskybse/script/build_rpm)" ]
then
    cp -a -f huskybse/script/build_rpm $top_directory/
    if [ "$restart" -ne 1 ]
    then
    echo
    echo 'The script "build_rpm" has changed. Please restart it.'
    echo
    restart=1
    fi
fi

[ "$restart" -eq 1 ] && exit

../spec_configure
popd

tar --exclude=.git -czf husky.tar.gz husky
if [ -n "$(whereis -b mock | cut -d: -f2)" ]
then
    mock $config --buildsrpm --spec husky/husky.spec --sources husky.tar.gz --resultdir=./result
    SRCRPM=$(echo result/*.src.rpm)
    mock $config -D "jobs $jobs" --no-clean --resultdir=./result --rebuild $SRCRPM
elif [ -n "$(whereis -b rpmbuild | cut -d: -f2)" ]
then
    rpmbuild -ta -D "jobs $jobs" husky.tar.gz
else
    echo "You have not installed both \"rpmbuild\" and \"mock\"; please, install them."
fi
