#!/bin/sh
#
# See also https://bugs.debian.org/804722#31

set -e

# Move to this Standards-Version.
SV_LATEST=4.7.0

PKG="$1"

if [ -z "$PKG" ]; then
    echo "usage: $0 <PACKAGE>"
    exit 1
fi

if [ -e "${PKG}-salsa" ]; then
    echo "error: ${PKG}-salsa already exist, aborting."
    exit 1
fi

if [ -z "ALLOWFAILURE" ] ; then
    ALLOWFAILURE=false
fi

# Fetch every snapshotted source package.  Manually loop until all
# transfers succeed, as 'gbp import-dscs --debsnap' do not fail on
# download failures.
until debsnap --force -v $PKG || $ALLOWFAILURE ; do sleep 1; done
mkdir ${PKG}-salsa; cd ${PKG}-salsa
git init
# Specify branches to override any debian/gbp.conf file present in the
# source package.  Use DEP-14 branch layout.
gbp import-dscs  \
    --debian-branch=debian/latest \
    --upstream-branch=upstream/latest \
    --pristine-tar ../source-$PKG/*.dsc

# Document branch layout for gbp
inifile +inifile debian/gbp.conf +create +section DEFAULT +key debian-branch +value debian/latest
inifile +inifile debian/gbp.conf +section DEFAULT +key upstream-branch +value upstream/latest
git add debian/gbp.conf
git commit -m "Added d/gbp.conf to describe branch layout." debian/gbp.conf

# Add Vcs pointing to Salsa Debian project (must be manually created
# and pushed to).
if ! grep -q ^Vcs- debian/control ; then
    awk "BEGIN { s=1 } /^\$/ { if (s==1) { print \"Vcs-Browser: https://salsa.debian.org/debian/$PKG\"; print \"Vcs-Git: https://salsa.debian.org/debian/$PKG.git\" }; s=0 } { print }" < debian/control > debian/control.new && mv debian/control.new debian/control
    git commit -m "Updated vcs in d/control to Salsa." debian/control
fi

# Tell gbp to enforce the use of pristine-tar.
inifile +inifile debian/gbp.conf +section DEFAULT +key pristine-tar +value True
git add debian/gbp.conf
git commit -m "Updated d/gbp.conf to enforce the use of pristine-tar." debian/gbp.conf

# Update to latest Standards-Version.
SV="$(grep ^Standards-Version: debian/control|awk '{print $2}')"
if [ $SV_LATEST != $SV ]; then
    sed -i "s/\(Standards-Version: \)\(.*\)/\1$SV_LATEST/" debian/control
    git commit -m "Updated Standards-Version from $SV to $SV_LATEST." debian/control
fi

# Sort debian control files
wrap-and-sort -at
git commit -m "Use wrap-and-sort -at for debian control files" debian/*

if [ ! -e debian/source/format ] ; then
    mkdir debian/source
    echo '3.0 (quilt)' > debian/source/format
    git add debian/source/format
    git commit -m "Set Debian source package format to 3.0 (quilt)." debian/source/format
fi

if grep -q pkg-config debian/control; then
    sed -i s/pkg-config/pkgconf/ debian/control
    git commit -m "Replaced obsolete pkg-config build dependency with pkgconf." debian/control
fi

if grep -q libncurses5-dev debian/control; then
    sed -i s/libncurses5-dev/libncurses-dev/ debian/control
    git commit -m "Replaced obsolete libncurses5-dev build dependency with libncurses-dev." debian/control
fi

lintian-brush --no-update-changelog

# Create changelog entry
gbp dch --qa
