#!/bin/bash

#  This file is part of EST (Efficient Symbolic Tools).
#  Copyright (C) 2003, 2012
#  UM-FERI, Smetanova ulica 17, SI-2000 Maribor, Slovenia
#
#  EST is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  EST is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public
#  License along with this program; if not, write to the Free
#  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA 02110-1301 USA.]

# Some help on building rpm packages:
# http://www.rpm-based.org/how-to-create-rpm-package
# http://www.rpm.org/max-rpm/
# http://kmymoney2.sourceforge.net/phb/rpm-example.html
# http://dries.ulyssis.org/apt/packages/gnunet/gnunet-spec.html

VERSION=`sed "" source/VERSION`

if [ $# != 1 ]
then
  echo "Usage: ./package-rpm RELEASE_NUMBER"
  exit 0
else
  RELEASE=$1
fi

if [ ! -d "distribution/version-$VERSION" ]
then
  echo "CREATE TGZ PACKAGE FIRST!"
  exit 0
fi

if [ ! -d "distribution/version-$VERSION/release-$RELEASE" ]
then
  echo "CREATE DEBIAN PACKAGE FIRST!"
  exit 0
fi

echo ""
echo "************************"
echo "CREATING NEW RELEASE ..."
echo "************************"
echo ""

mkdir "distribution/version-$VERSION/release-$RELEASE/rpm"
mkdir "distribution/version-$VERSION/release-$RELEASE/rpm/BUILD"
mkdir "distribution/version-$VERSION/release-$RELEASE/rpm/RPMS"
mkdir "distribution/version-$VERSION/release-$RELEASE/rpm/SOURCES"
mkdir "distribution/version-$VERSION/release-$RELEASE/rpm/SPECS"
mkdir "distribution/version-$VERSION/release-$RELEASE/rpm/SRPMS"

pushd "distribution/version-$VERSION/release-$RELEASE"
mv ../changelog.rpm ../changelog.rpm.old
echo "* `LC_ALL=en_GB date +"%a %b %d %Y"` - meolic" > ../changelog.rpm
echo "- " >> "../changelog.rpm"
cat ../changelog.rpm.old >> ../changelog.rpm
rm ../changelog.rpm.old
vi "../changelog.rpm"
cd rpm
echo "%_topdir	`pwd`" > ~/.rpmmacros

cp ../"est2ed_$VERSION.orig.tar.gz" "SOURCES/est2ed-$VERSION.tar.gz"
sed -e "s/THISVERSION/$VERSION/g" -e "s/THISRELEASE/$RELEASE/g" \
    "../../../../rpm/SPECS/est2ed.spec" \
    > "SPECS/est2ed.spec"
cat "../../changelog.rpm" >> "SPECS/est2ed.spec"
cd SPECS
rpmbuild -bb est2ed.spec
cd ..

rm "SOURCES/est2ed-$VERSION.tar.gz"
cp ../"libest2_$VERSION.orig.tar.gz" "SOURCES/libest2-$VERSION.tar.gz"
rm "SPECS/est2ed.spec"
sed -e "s/THISVERSION/$VERSION/g" -e "s/THISRELEASE/$RELEASE/g" \
    "../../../../rpm/SPECS/libest2.spec" \
    > "SPECS/libest2.spec"
cat "../../changelog.rpm" | sed "s/est2ed/libest2/g" >> "SPECS/libest2.spec"
cd SPECS
rpmbuild -bb libest2.spec
cd ../..

cp rpm/RPMS/*/*.rpm .
rm -fr rpm
popd

echo ""
echo "*******************"
echo "PACKAGES COMPLETED!"
echo "*******************"
echo ""
