#!/bin/bash
#
#  Script to start build a debian package
#  and other functions to keep the package up-to-date
#
#  Copyright 2003 by Geert Stappers under the terms of GPL v2.0
#


if [ "G${0}" != "Gdeb/build" ]
then
  echo We expect you to run this script as \`deb/build\`
  echo  to make sure you are in the toplevel directory of this project.
  exit 1
fi

if [ ! -x ./configure ]
then
  echo Assuming you did a fresh CVS checkout,
  echo gonna run \`./autogen.sh\` for you to create the initial Makefiles.
  ./autogen.sh
fi

DEBIAN_FILES="rules control changelog copyright docs compat
 conglomerate.menu conglomerate_icon.xpm
 NEWS watch"

#
if [ ! -f debian/control ]
then
  mkdir -p debian/patches
  # directories that we will use
  echo coping  deb directory into debian
  for DF in ${DEBIAN_FILES}
  do
    cp deb/${DF} debian
  done
  echo copy also the patch files
  cp deb/patches/P* debian/patches
fi

#
if [ ! -x debian/rules ]
then
  chmod +x debian/rules
  # that is usefull when you take the diff.gz to an other system
  # with `patch`, which doesn't set the execute bits.
fi

# assuming the user has debian development stuff installed
# ( this is the place where a test on such executables should done )

update()
# changes in 'debian' should go back into 'deb'
{
  echo apply this diff file as
  echo  "patch -p0 < deb.update"
  for DF in $( cd debian ; find . -type f | sed -e 's@^\./@@' )
  do
   if [ -f deb/$DF ]
   then
     diff -uN deb/$DF debian/$DF | \
     sed -e 'sG^--- deb/G--- debian/G' -e 'sS^\+\+\+ debian/S+++ deb/S'
   else
     diff -uN /dev/null debian/$DF | sed -e 'sS^\+\+\+ debian/S+++ deb/S'
   fi
   if [ ! -s debian/$DF ]
   then
     # echo empty file debian/$DF
     echo "--- /dev/null   2003-09-13 14:12:39.000000000 +0000
+++ deb/$DF 2003-10-06 21:54:07.000000000 +0000
@@ -0,0 +1 @@
+created"
     echo "--- deb/${DF}.created   2003-09-13 14:12:39.000000000 +0000
+++ deb/$DF 2003-10-06 21:54:07.000000000 +0000
@@ -1 +0,0 @@
-created"
   fi
  done
}

diffgz()
# generate the  .diff.gz
{
  CWD=$(basename $(pwd) )
  ( cd .. ; dpkg-source -b ${CWD} )
}

#
if [ $# > 1 ]
then
  case $1 in
  version=*)
    NWRLS=$( echo $1 | awk -F= '{print $2}')
    dch -v $NWRLS "New upstream release."
  ;;
  update*)
    echo generating deb.update
    update > deb.update
    exit
  ;;
  changes*)
    echo generating _arch.changes
    dpkg-genchanges
    exit
  ;;
  diff*)
    echo generating .diff.gz
    diffgz
    exit
  ;;
  sign*)
    echo do debrsign manual
    exit
  ;;
  h*)
    echo version=x.y.z update changes diff sign help
    exit
  ;;
  esac
fi

echo gonna build the package
if [ ! -f build-stamp ]
then
  dpkg-buildpackage -uc -us
else
  debian/rules binary
fi


echo one directory level up, are these .deb files:
ls -ltr ../*.deb

# end of script
