#!/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.]

VERSION=`sed "" source/VERSION`
NAME="est2ed"
OS=`uname | sed "s/_.*//;s/[0-9]//g"`

mkdir -p "distribution"

if [ -e "distribution/version-$VERSION" ]
then
  echo "CANNOT CHANGE AN EXISTING VERSION!"
  exit 0
fi

echo ""
echo "************************"
echo "CREATING NEW VERSION ..."
echo "************************"
echo ""

mkdir "distribution/version-$VERSION"
mkdir "distribution/version-$VERSION/$NAME-$VERSION"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/gui"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/bdd"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/pa"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/versis"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/mc"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/strucval"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/ccs"
mkdir "distribution/version-$VERSION/$NAME-$VERSION/data"

cd source
cp VERSION CHANGES COPYING "../distribution/version-$VERSION/$NAME-$VERSION"
cp README "../distribution/version-$VERSION/$NAME-$VERSION"
cp est.h est2ed.c est2ed.tcl "../distribution/version-$VERSION/$NAME-$VERSION"
cp pkgIndex.tcl "../distribution/version-$VERSION/$NAME-$VERSION"
cp Makefile Makefile.Linux Makefile.SunOS Makefile.MINGW Makefile.Darwin "../distribution/version-$VERSION/$NAME-$VERSION"
cp ../../myinterface/mi.tcl "../distribution/version-$VERSION/$NAME-$VERSION"
cp ../../myinterface/miDialog.tcl "../distribution/version-$VERSION/$NAME-$VERSION"
cd gui
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/gui"
cd ..
cd bdd
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/bdd"
cd ..
cd pa
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/pa"
cd ..
cd versis
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/versis"
cd ..
cd mc
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/mc"
cd ..
cd strucval
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/strucval"
cd ..
cd ccs
rsync -rC * "../../distribution/version-$VERSION/$NAME-$VERSION/ccs"
cd ..
cd ..
cd data
rsync -rC * "../distribution/version-$VERSION/$NAME-$VERSION/data"
cd ..

mkdir "distribution/version-$VERSION/$NAME-$VERSION"/debian
mkdir "distribution/version-$VERSION/$NAME-$VERSION"/debian/source
cp debian/c* "distribution/version-$VERSION/$NAME-$VERSION"/debian
cp debian/r* "distribution/version-$VERSION/$NAME-$VERSION"/debian
cp debian/*.dirs "distribution/version-$VERSION/$NAME-$VERSION"/debian
cp debian/source/* "distribution/version-$VERSION/$NAME-$VERSION"/debian/source

pushd "distribution/version-$VERSION/$NAME-$VERSION"
dch -M --create --package "$NAME" -v "$VERSION"
cp "debian/changelog" "../changelog.deb"
popd

mkdir "distribution/version-$VERSION/$NAME-$VERSION"/rpm
mkdir "distribution/version-$VERSION/$NAME-$VERSION"/rpm/SPECS
sed -e "s/THISVERSION/$VERSION/g" -e "s/THISRELEASE/0/g" \
    "rpm/SPECS/est2ed.spec" \
    > "distribution/version-$VERSION/$NAME-$VERSION/rpm/SPECS/est2ed.spec"

pushd "distribution/version-$VERSION"
echo "* `LC_ALL=en_GB date +"%a %b %d %Y"` - meolic" > changelog.rpm
echo "- Initial release." >> changelog.rpm
vi changelog.rpm
popd

pushd "distribution/version-$VERSION"
tar -czf "$NAME-$VERSION.tar.gz" "$NAME-$VERSION"
mv "$NAME-$VERSION.tar.gz" "$NAME"_"$VERSION.initial.tar.gz"
rm -fr "$NAME-$VERSION"
popd

echo ""
echo "**********************"
echo "TGZ PACKAGE COMPLETED!"
echo "**********************"
echo ""
