#!/bin/bash

set -e
set -v

VERSION=$1
BASEDIR=`pwd`

# make sure we are in a source dir
if [ ! -e build.xml ]; then
    echo "This does not look a like a source directory. Exiting..."
    exit 1
fi

mkdir -p build/rpm/BUILD build/rpm/RPMS build/rpm/SOURCES build/rpm/SPECS build/rpm/SRPMS

cp dist/pegasus-source-$VERSION.tar.gz build/rpm/SOURCES/

# special sauce for el7
DIST=`./release-tools/getosid | perl -pe 's/rhel/.el/'`
if (rpmbuild --showrc | grep ": dist" | grep "el7") >/dev/null 2>&1; then
    perl -pi -e 's@BuildRequires:  ant, ant-nodeps, ant-apache-regexp, java7-devel, gcc, groff@BuildRequires:  ant, ant-apache-regexp, gcc@' pegasus.spec
fi

cp pegasus.spec build/rpm/SPECS/

rpmbuild -ba --define "_topdir $BASEDIR/build/rpm" --define "dist $DIST" pegasus.spec

find build/rpm -name \*.rpm -exec cp {} dist/ \;
rm -f dist/*debug*
mv dist/pegasus-*.src.rpm dist/pegasus-$VERSION.src.rpm
cd dist
echo "Generated RPMs:"
ls -l *.rpm


