#!/bin/bash
# Buildpkg script for producing RPM packages. Does not require root access.

# This is useful for debugging
set -x
# Buildbot exports some useful env variables.
# Check for $AMVER.  I couldn't come up with a good way to detect it.
if [ -z $AMVER ]; then
    AMVER=amanda-2.6.0p2
fi
# Check for AMTARBALL variable.
if [ -z $AMTARBALL ]; then 
    AMTARBALL=$AMVER.tar.gz
fi

# Check for AMTARBALL file, if it's not there, create it.
if [ ! -f ${AMTARBALL} ]; then
    mkdir ${AMVER}
    cp -Rfp * ${AMVER}/
    tar -cf ${AMTARBALL} -z ${AMVER}
    rm -rf ${AMVER}
fi

# Check for the packaging dirs.
if [ -z $AMPKGDIR ]; then
    AMPKGDIR=${PWD}
fi
if [ ! -d ${AMPKGDIR} ]; then
    mkdir ${AMPKGDIR}
fi
cd ${AMPKGDIR}

if [ -d rpm ]; then
    rm -rf rpm
fi
mkdir rpm
mkdir rpm/SOURCES
mkdir rpm/SRPMS
mkdir rpm/SPECS
mkdir rpm/BUILD
mkdir rpm/RPMS

# Make a copy of the tarball with the name that rpmbuild expects
cp ${AMTARBALL} rpm/SOURCES/${AMVER}.tar.gz
cp packaging/rpm/amanda.spec rpm/SPECS/amanda.spec
# Rpmbuild requires absolute paths.  annoying.  If you need to change the 
# default value of some rpm.spec variable, just pass extra --define options.
# this is useful for changing %amanda_release or %amanda_version
rpmbuild -ba --define "_topdir ${AMPKGDIR}/rpm" \
             ${AMPKGDIR}/rpm/SPECS/amanda.spec 
cp rpm/RPMS/*/*.rpm . || exit 1
cp rpm/SRPMS/*.rpm . || exit 1
