#!/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 deb packages:
# http://wiki.debian.org/Packaging
# http://wiki.debian.org/IntroDebianPackaging
# http://developer.ubuntu.com/resources/tools/packaging/

# We are using dch -M
# remove -M if you are not the author of the last changelog entry

# Some help on using pbuilder
# https://wiki.ubuntu.com/PbuilderHowto
# http://wiki.debian.org/PbuilderTricks

# We are using pdebuild which uses pbuilder. Initialize pbuilder as follows:
# 1. install pbuilder
# 2. sudo pbuilder create
# 3a. sudo pbuilder update (do this just before creating a package)
# 3b. sudo pbuilder update --override-config (when using local repository)

# We are using biddy package from local repository maintained by mini-dinstall
# You have to adapt /etc/pbuilderrc (set at least BINDMOUNTS and OTHERMIRROR)
# You should enable local repository after you initialized it (add one package)

VERSION=`sed "" source/VERSION`
NAME="est2ed"
LIBNAME="libest2"
OS=`uname | sed "s/_.*//;s/[0-9]//g"`
DIST=$(lsb_release --short --codename)

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

if [ ! -d "distribution/version-$VERSION" ]
then
  echo "YOU HAVE TO RUN ./package-tgz FIRST!"
  exit 0
fi

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

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

mkdir "distribution/version-$VERSION/release-$RELEASE"
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian/source"
cp debian/compat "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/control "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/copyright "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/est2ed.dirs "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/rules "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/source/format "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian/source"

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

cd source

cp est2ed.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp pkgIndex.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp ../../myinterface/mi.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp ../../myinterface/miDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp ../../bddview/bddview.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp bdd/bddDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp pa/paDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp versis/versisDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp mc/mcDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp strucval/strucvalDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
cp ccs/ccsDialog.tcl "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"

cd gui
cp gui.h guiInt.h guiMain.c guiTcl.c guiIndex.tcl guiDecls.h guiStubInit.c guiStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/gui"
cd ..
cd bdd
cp bdd.h bddInt.h bddMain.c bddRel.c bddUtil.c bddOutput.c bddTcl.c bddDialog.tcl bddDecls.h bddStubInit.c bddStubLib.c Makefile Makefile-Biddy Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bdd"
cd ..
cd pa
cp pa.h paInt.h paMain.c paKod.c paOutput.c paParser.l paParser.y paTcl.c paDialog.tcl paDecls.h paStubInit.c paStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/pa"
cd ..
cd versis
cp versis.h versisInt.h versisMain.c versisThread.c versisComp.c versisEqv.c versisMin.c versisTcl.c versisDialog.tcl versisDecls.h versisStubInit.c versisStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/versis"
cd ..
cd mc
cp mc.h mcInt.h mcMain.c mcThread.c mcDiagnostic.c mcAutomata.c mcMsc.c mcTree.c mcParser.l mcParser.y mcTcl.c mcDialog.tcl mcDecls.h mcStubInit.c mcStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/mc"
cd ..
cd strucval
cp strucval.h strucvalInt.h strucvalMain.c strucvalThread.c strucvalTcl.c strucvalDialog.tcl strucvalDecls.h strucvalStubInit.c strucvalStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/strucval"
cd ..
cd ccs
cp ccs.h ccsInt.h ccsMain.c ccsParser.l ccsParser.y ccsVerilog.l ccsVerilog.y ccsTcl.c ccsDialog.tcl ccsDecls.h ccsStubInit.c ccsStubLib.c Makefile Makefile.Linux "../../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/ccs"
cd ..
cp VERSION README CHANGES "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp Makefile Makefile.Linux est.h "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp est2ed.c "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cd ..

cd data
rsync -rC * "../distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/data"
cd ..

pushd "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp ../../changelog.deb debian/changelog
dch -M -v "$VERSION-$RELEASE"
cp debian/changelog ../../changelog.deb
cd ..
tar -czf "$NAME-$VERSION.tar.gz" "$NAME-$VERSION"
mv "$NAME-$VERSION.tar.gz" "$NAME"_"$VERSION.orig.tar.gz"
cd "$NAME-$VERSION"
dpkg-buildpackage -S
mkdir `pwd`/../source
mv `pwd`/../$NAME*.changes `pwd`/../$NAME*.dsc `pwd`/../source/
cp `pwd`/../$NAME*.gz `pwd`/../source/
pdebuild --auto-debsign --buildresult `pwd`/.. -- --allow-untrusted
rm -f `pwd`/../$NAME*source.changes
popd
rm -fr "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/bin"
rm "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian/changelog"
rm -fr "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian/$NAME"

mv "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION" "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION"
mkdir "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/bin"
rm "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian/est2ed.dirs"
rm "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian/control"
rm "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian/rules"
cp debian/libest2.dirs "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian"
cp debian/libest2-dev.dirs "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian"
cp debian/control-lib "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian/control"
cp debian/rules-lib "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION/debian/rules"

pushd "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION"
sed "s/est2ed/libest2/g" ../../changelog.deb > debian/changelog
cd ..
tar -czf "$LIBNAME-$VERSION.tar.gz" "$LIBNAME-$VERSION"
mv "$LIBNAME-$VERSION.tar.gz" "$LIBNAME"_"$VERSION.orig.tar.gz"
cd "$LIBNAME-$VERSION"
dpkg-buildpackage -S
mv `pwd`/../$LIBNAME*.changes `pwd`/../$LIBNAME*.dsc `pwd`/../source/
cp `pwd`/../$LIBNAME*.gz `pwd`/../source/
pdebuild --auto-debsign --buildresult `pwd`/.. -- --allow-untrusted
rm -f `pwd`/../$LIBNAME*source.changes
popd
rm -fr "distribution/version-$VERSION/release-$RELEASE/$LIBNAME-$VERSION"

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