#!/bin/sh

set -x

if [ -d debian -a -d .git ] ; then
  gbp import-dscs --debsnap --create-missing-branches --pristine-tar `basename "$(pwd)"`
  exit 0
fi

if [ "$1" = "" ] ; then
  echo "Usage: `basename $0` PACKAGE"
  exit 1
fi

gbp import-dscs --debsnap --create-missing-branches --pristine-tar $1

if [ $? ] ; then
  echo "gbp failed.  Any chance this was due to $1/debian/gbp.conf which might define different branch names than default?"
  if [ -e $1/debian/gbp.conf ] ; then
    echo "You might like to try setting those branches"
    GBPOPTIONS=""
    if grep -q debian-branch $1/debian/gbp.conf ; then
      GBPOPTIONS="--$(grep debian-branch $1/debian/gbp.conf | tr -d ' ')"
    fi
    if grep -q upstream-branch $1/debian/gbp.conf ; then
       GBPOPTIONS="${GBPOPTIONS} --$(grep upstream-branch $1/debian/gbp.conf | tr -d ' ')"
    fi
    rm -rf $1
    gbp import-dscs --debsnap --create-missing-branches $GBPOPTIONS --pristine-tar $1
  fi
fi

echo "In case of problems use --no-sign-tags"
