#!/bin/bash

#  Authors     [Robert Meolic (meolic@uni-mb.si)]
#  Revision    [$Revision: 31 $]
#  Date        [$Date: 2013-04-06 07:52:12 +0200 (sob, 06 apr 2013) $]
#
#  Copyright   [This file is part of Bdd Scout package.
#               Copyright (C) 2008, 2013 UM-FERI
#               UM-FERI, Smetanova ulica 17, SI-2000 Maribor, Slovenia
#
#               Biddy 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.
#
#               Biddy 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://www.debian.org/doc/maint-guide/
# https://wiki.ubuntu.com/PackagingGuide/Complete

VERSION=`sed "" VERSION`
NAME="bddscout"
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 "CREATE TGZ PACKAGE 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/bddscout.dirs "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/bddscout-ifip.dirs "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/bddscout-ifip-data.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"

cp VERSION README README.Linux CHANGES "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp bddscout.h bddscout.c bddscoutMain.c bddscout.tcl "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp bddscoutDecls.h bddscoutStubInit.c bddscoutStubLib.c "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp pkgIndex.tcl "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp bddscoutIFIP.c bddscoutIFIP.tcl "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp pkgExtensionIFIP.tcl "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp Makefile Makefile.Linux "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp example.bdd example.bf "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp ../bddview/bddview.tcl ../bddview/example.bddview "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/cath
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/ex
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/hachtel
mkdir "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/plasco
cp IFIP/README IFIP/*.txt "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP
cp IFIP/cath/* "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/cath
cp IFIP/ex/* "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/ex
cp IFIP/hachtel/* "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/hachtel
cp IFIP/plasco/* "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"/IFIP/plasco

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"
#debuild -us -uc
#popd
dpkg-buildpackage -S
mkdir `pwd`/../source
mv `pwd`/../*.changes `pwd`/../*.dsc `pwd`/../source/
cp `pwd`/../*.gz `pwd`/../source/
pdebuild --auto-debsign --buildresult `pwd`/.. -- --allow-untrusted
rm -f `pwd`/../*source.changes
popd
rm -fr "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"

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