#!/bin/sh

if [ $# -ne 2 ]
then
   echo "Usage: $0 pkg_name version"
   exit
fi

# package name must be of lower case (a-z), digits (0-9) and (+-.)
NAME=$1
TARGET_VERNO=$2

TARGET=${PWD}
SRCTARGET=${NAME}-${TARGET_VERNO}
PKGDIR=${TARGET}/debpackage
SRCPKGDIR=${PKGDIR}/${SRCTARGET}

# cleanup old stuff
rm -rf ${PKGDIR}
mkdir ${PKGDIR}

# export source into package
mkdir -p ${SRCPKGDIR}

# this captures any local changes, which allows building a package without
# comitting something which may not actually work
bzr ls --versioned | tar -cf - -T - | tar -xf - -C ${SRCPKGDIR}

#
# create the Debian Source Package
#
cat >> ${SRCPKGDIR}/debian/control << EOFDEB
Standards-Version: 3.8.1
Source: libecbufr
Section: libs
Priority: optional
Maintainer: libecbufr-dev
Homepage: https://launchpad.net/libecbufr

Package: libecbufr
Architecture: any
Provides: libecbufr
Conflicts: libecbufr
Replaces: libecbufr
Suggests: libcgi-formbuilder-perl
Description: Environment Canada BUFR Library
Homepage: https://launchpad.net/libecbufr
EOFDEB

cd ${PKGDIR}

dpkg-source -b ${SRCTARGET}

if [ $? -eq 0 ]; then
   rm -rf ${SRCPKGDIR}
	echo "Created Debian Source package in ${PKGDIR}"
	exit 0
fi

echo "Debian source package creation failed!"
exit 1
