#!/bin/bash

#  Authors     [Robert Meolic (meolic@uni-mb.si)]
#  Revision    [$Revision: 27 $]
#  Date        [$Date: 2010-07-22 12:55:07 +0200 (Thu, 22 Jul 2010) $]
#
#  Copyright   [This file is part of Biddy.
#               Copyright (C) 2006, 2007, 2008, 2009, 2010 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`
MAJORVERSION=`cut -c1 VERSION`
NAME="libbiddy$MAJORVERSION"
OS=`uname | sed "s/_.*//;s/[0-9]//g"`

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"
cp debian/* "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION/debian"

cp VERSION CHANGES COPYING "distribution/version-$VERSION/release-$RELEASE/$NAME-$VERSION"
cp README "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 -v "$VERSION-$RELEASE" 
cp "debian/changelog" "../../changelog.deb"
debuild -us -uc
cd ..
rm -fr "$NAME-$VERSION"
popd

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