#!/bin/sh
#
# build_rpm - build an .rpm package for rntrack
#   Prerequisite: "mock" should be installed
#                 this script should be in the directory for which "rntrack"
#                 is a subdirectory
#   Usage:  ./build_rpm [config]
#                 config - one of the configs listed by `mock --list-chroots`
#   Result: the built regular and debuginfo packages are
#           in the "result" subdirectory
#

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

# Check that the script is in the directory for which "rntrack"
# is a subdirectory
[ -d rntrack ] || die "No 'rntrack' subdirectory"

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

[ -z "$1" ] && CONFIG=default || CONFIG="$1"
echo "Building rntrack for $CONFIG"
rm -rf ./result
mkdir -p result
pushd rntrack > /dev/null 2>&1
git pull
git archive --prefix=rntrack/ -o ../result/rntrack.tar.gz HEAD
mock -r $CONFIG --buildsrpm --spec MakeFiles/linux/rntrack.spec \
    --sources ../result/rntrack.tar.gz --resultdir=../result
popd > /dev/null 2>&1
SRCRPM=$(echo result/*.src.rpm)
mock -r $CONFIG --no-clean --resultdir=./result --rebuild $SRCRPM
