#!/usr/bin/bash
#
# Setup package for build
#
# Usage: lpf-setup [-a arch] [-e eula-license] <topdir> <spec> [ source... ]
#
# -a arch: Adds a --target $arch to the rpmbuild command, forces the
#          target achitecture to a fixed value instead of default host arch.
#          Typically used on i686-only binary packages.
# -e eula-license:
#          Displays license file and requires user to accept it before build.
# topdir   Where lpf package structure is created, like DESTDIR.
# [source...]
#          All sources which cannot be retrieved with spectool i. e., without
#          a sane url.
#
scriptdir=$( dirname $(readlink -fn $0))

config=$( mktemp )
eula=''
while [[ "$1" == -* ]]; do
    case "$1" in
        '-a')    shift
                 echo "rpmbuild_opts=\"-bb --target $1\"" > $config
                 shift
                 ;;
        '-e')    shift
                 eula=$1
                 shift
                 ;;
    esac
done

if [ -f "$1" ]; then
    # Compatibility mode for old [eula] <topdir> <spec> ...
    eula=$1
    shift
fi
topdir=$1; shift
spec=${1%.in}; shift

pkg=$(basename $spec .spec)

install  -d $topdir/usr/share/lpf/packages/$pkg/SOURCES
install  -d $topdir/usr/share/lpf/packages/$pkg/eula
install  -dm 755  $topdir/var/lib/lpf/packages/$pkg
echo 'untriaged' >  $topdir/var/lib/lpf/packages/$pkg/state

install -pm 644 -D  /usr/share/applications/lpf.desktop \
    $topdir/usr/share/applications/lpf-$pkg.desktop
sed  -e '/^Name/s/=.*/= lpf '"$pkg/" \
     -e '/^Comment/s/=.*/= Use lpf system to build '"$pkg/" \
     -e "/^Exec/s/\$/ $pkg/" \
     -i $topdir/usr/share/applications/lpf-$pkg.desktop

[ -n "$eula" ] && cp -a $eula $topdir/usr/share/lpf/packages/$pkg/eula
chmod 644 $config
[ -s "$config" ] && cp -a $config $topdir/usr/share/lpf/packages/$pkg/CONFIG
cp -a $spec.in $topdir/usr/share/lpf/packages/$pkg/${pkg}.spec
for arg in $*; do
    cp $arg $topdir/usr/share/lpf/packages/$pkg/SOURCES
done
rm -f $config


# vim: set expandtab ts=4 sw=4:
