#!/bin/sh
# Generate and upload backports for Ubuntu PPA
# Author: Martin Pitt <mpitt@debian.org>

set -e

if [ -n "`bzr modified`" ]; then
    echo "Modified files, aborting" >&2
    exit 1
fi

SRC=`dpkg-parsechangelog  | grep ^Source | cut -f2- -d' '`
if [ "$SRC" = postgresql-9.1 ]; then
    RELEASES='lucid'
elif [ "$SRC" = postgresql-9.2 ]; then
    RELEASES='lucid precise'
else
    RELEASES='lucid precise'
fi

CUR_VERSION=`dpkg-parsechangelog | grep ^Version | cut -f2 -d' '`
SINCE_VERSION=`dpkg-parsechangelog -n2 | grep urgency=| tail -n1 | awk '{print $2}' | tr -d '()'`


export DEBEMAIL=martin.pitt@ubuntu.com

# do first upload with orig tarball, subsequent ones without
ORIG="-sa"
for release in $RELEASES; do
    BP_VERSION=`echo "$CUR_VERSION~${release}" | sed 's/-/-0ppa/'`
    dch -D$release -v"$BP_VERSION" -b 'Backport'
    if [ "$release" = lucid -o "$release" = "natty" -o "$release" = oneiric ]; then
        sed -i 's/dpkg-dev.*| hardening-wrapper/hardening-wrapper/' debian/control
    fi
    if [ "$SRC" = "postgresql-9.1" ]; then
        # libraries are built by -9.2 now, and PPAs disallow building older versions
        perl -000 -ne 'print $_ unless /^Package: lib/' -i debian/control
        sed -i 's/fail-missing/list-missing/' debian/rules
        sed -i 's/python-dev,/python-dev, libpq-dev (>= 9.1),/' debian/control
    fi
    if [ "$release" = "lucid" ]; then
        sed -i 's/-Zxz//' debian/rules 
    fi
    bzr bd -S -- --no-lintian "$ORIG" -v$SINCE_VERSION
    bzr revert --no-backup
    dput ppa:pitti/postgresql ../${SRC}_${BP_VERSION}_source.changes
    ORIG="-sd"
done
