#!/bin/sh
set -x
ARCH=-i386
EXTRAPBUILDER=/var/cache/pbuilder/extra
# my wrapper over `gbp buildpackage` to detect backports automatically
if dpkg-parsechangelog | awk '/^Version:/ {print $2}' | grep -q '~bpo11' ; then
    echo "This is a backport to bullseye for architecture $ARCH"
    # enable local packages in pbuilder
    if [ -d $EXTRAPBUILDER ] ; then
      rm -rf $EXTRAPBUILDER/release
      ln -s bullseye $EXTRAPBUILDER/release
    fi
    ln -sf .gbp.conf.bullseye$ARCH ~/.gbp.conf
    STABLEVERSION=`grep '~bpo11' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([0-9].*-[.0-9]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
    # versions of backports of Debian native packages (like dh-r) do not necessarily contain a - sign
    if echo $STABLEVERSION | grep -- '-backports' ; then
      # may be this also works in general ...
      STABLEVERSION=`grep '~bpo11' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([.0-9~]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
    fi
else
  if dpkg-parsechangelog | awk '/^Version:/ {print $2}' | grep -q '~bpo8' ; then
    echo "This is a backport to Jessie for architecture $ARCH"
    # enable local packages in pbuilder
    if [ -d $EXTRAPBUILDER ] ; then
      rm -rf $EXTRAPBUILDER/release
      ln -s jessie $EXTRAPBUILDER/release
    fi
    ln -sf .gbp.conf.jessie$ARCH ~/.gbp.conf
    STABLEVERSION=`grep '~bpo8' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([0-9].*-[.0-9]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
    # versions of backports of Debian native packages (like dh-r) do not necessarily contain a - sign
    if echo $STABLEVERSION | grep -- '-backports' ; then
      # may be this also works in general ...
      STABLEVERSION=`grep '~bpo8' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([.0-9~]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
    fi
  else
    if dpkg-parsechangelog | awk '/^Version:/ {print $2}' | grep -q -e '~bpo9' -e '+deb9u[0-9]' ; then
      echo "This is a backport to Stretch for architecture $ARCH"
      # enable local packages in pbuilder
      if [ -d $EXTRAPBUILDER ] ; then
        rm -rf $EXTRAPBUILDER/release
        ln -s stretch $EXTRAPBUILDER/release
      fi
      ln -sf .gbp.conf.stretch$ARCH ~/.gbp.conf
      STABLEVERSION=`grep '~bpo9' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([0-9].*-[.0-9]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
      # versions of backports of Debian native packages (like dh-r) do not necessarily contain a - sign
      if echo $STABLEVERSION | grep -- '-backports' ; then
        # may be this also works in general ...
        STABLEVERSION=`grep '~bpo9' debian/changelog | head -n2 | tail -n1 | sed 's/^[^ ]\+ (\([.0-9~]\+~bpo[0-9+]\+\)) .*-backports[-slopy]*; urgency=.*$/\1/'`
      fi
    else
      echo "Make sure we use sid config"
      ln -sf .gbp.conf.sid$ARCH ~/.gbp.conf
      # enable local packages in pbuilder
      if [ -d $EXTRAPBUILDER ] ; then
        rm -rf $EXTRAPBUILDER/release
        ln -s sid $EXTRAPBUILDER/release
      fi
      STABLEVERSION=""
    fi
  fi
fi
# /usr/bin/gbp buildpackage "$STABLEVERSION -nc" $@ ## -nc = no-clean helps in some cases but breaks in others
if grep -q '^Section:.*/' debian/control ; then
  # for packages in contrib or non-free source-only uploads are not permitted thus do not even create these
  /usr/bin/gbp buildpackage "$STABLEVERSION" $@
  echo "This package was build for `grep '^Section:.*/' debian/control` - do not do a source only upload!"
  rm ../build-area/`grep '^Source:' debian/control | sed 's/^Source: *//'`_*_source.changes
else
  /usr/bin/gbp buildpackage --git-pbuilder-options="--source-only-changes" "$STABLEVERSION" $@
fi
