#!/bin/bash

#  Authors     [Robert Meolic (meolic@uni-mb.si)]
#  Revision    [$Revision: 51 $]
#  Date        [$Date: 2013-04-03 17:57:12 +0200 (sre, 03 apr 2013) $]
#
#  Copyright   [This file is part of Biddy.
#               Copyright (C) 2006, 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://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
# 3. sudo pbuilder update (do this just before creating a package)

# We are using biddy in other projects thus we upload the resulting deb files
# We are using local repository created and maintained by mini-dinstall
# 1. prepare .mini-dinstall.conf (use documentation)
# 2. dput -l local libbiddy*.changes

VERSION=`sed "" VERSION`
MAJORVERSION=`cut -c1 VERSION`
NAME="libbiddy$MAJORVERSION"
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/libbiddy1.dirs "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"
cp debian/libbiddy1-dev.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 CHANGES "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp biddy.h biddyInt.h biddyMain.c biddyStat.c "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp Makefile Makefile.Linux "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"

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`/../*.changes `pwd`/../*.dsc `pwd`/../source/
cp `pwd`/../*.gz `pwd`/../source/
pdebuild --auto-debsign --buildresult `pwd`/..
rm -f `pwd`/../*source.changes
popd
rm -fr "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"

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