#!/bin/sh -e

help () {
    cat <<EOF
Invoke this script from a $package bzr branch to build source packages for
all target distributions and upload them to a PPA.

Options:

  -p=<PPA>|--ppa=<PPA>  The PPA to upload to. This gets passed to dput, please
                        make sure you have a matching stanza in your ~/.dput.cf

  -k=<KEY>|--key=<KEY>  The GPG key used to sign the packages

  -s|--snapshot         Tweak the Debian revision by including the current bzr
                        revision number in it (e.g. 1.4.0~bzr178-0ubuntu0.8.04)

  -h|--help             Print this help and exit

EOF
    exit
}

#
# Check if we are in a bzr branch
#
if ! [ -d .bzr ] || ! [ -f debian/changelog ]; then
    echo "Error: not in a package bzr branch"
    echo
    help
fi

#
# Set defaults and parse command line arguments
#
ppa=landscape
key=free.ekanayaka@canonical.com
snapshot=no
package=$(dpkg-parsechangelog |grep ^Source|cut -f 2 -d " ")
version=$(dpkg-parsechangelog |grep ^Version|cut -f 2 -d " ")
upstream=$(echo $version | cut -f 1 -d "-")

for i in $*; do
    case $i in
        -p=*|--ppa=*)
                ppa=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
                ;;
        -k=*|--key=*)
                key=`echo $i | sed 's/[-a-zA-Z0-9]*=//'`
                ;;
        -s|--snapshot)
                snapshot=yes
                ;;
        -h|--help)
                help
                ;;
        *)
                echo "Error: unknown option $i"
                echo
                help
                ;;
    esac
done

if [ "$snapshot" = "yes" ]; then
    bzr_rev=$(bzr log -l 1|grep ^revno|cut -f 2 -d " ")
    upstream="$upstream~bzr$bzr_rev"
fi

#
# Clean up from possible previous runs
#
rm -fR ../${package}-*
rm -f ../${package}_*

#
# Export the sources
#
bzr export ../${package}-${upstream}
cd ..
cp -a ${package}-${upstream} ${package}-${upstream}.orig
rm -R ${package}-${upstream}.orig/debian
cd ${package}-${upstream}

#
# Build source packages and upload them
#
releases="hardy_8.04 karmic_9.10 lucid_10.04 maverick_10.10 natty_11.04 oneiric_11.10"

if [ "$snapshot" = "yes" ]; then
    # Snapshot, we'll add a dummy changelog entry like for all releases
    source_opt="-sa"
    releases="$releases natty_11.04"
else
    # Actual release, use the latest changelog entry and upload now
    dpkg-buildpackage -S -sa -k$key
    dput $ppa ../${package}_${version}_source.changes
    source_opt="-sd"
fi

for release in $releases; do

   codename=$(echo $release|cut -f 1 -d _)
   revision=0ubuntu0.$(echo $release|cut -f 2 -d _)
   if ! [ "$snapshot" = "yes" ]; then
       revision=${revision}.0~landscape1
   fi
   version=$upstream-$revision

   if [ "$snapshot" = "yes" ]; then
       message="Snapshot build for $codename"
   else
       message="Built for $codename, no source changes"
   fi
   cp debian/changelog ../   
   dch --force-distribution -b -v $version -D $codename -m $message
   dpkg-buildpackage -S $source_opt -k$key
   dput $ppa ../${package}_${version}_source.changes
   mv ../changelog debian

   source_opt="-sd"

done
