#!/bin/bash

# Copyright 2019, gregor herrmann <gregoa@debian.org>
# Released under the WTFPL

die() {
	echo "$1" >&2
	exit 1
}

dh_testdir || die "Not a source package …"

PKG=$(dpkg-parsechangelog -S Source)
VER=$(dpkg-parsechangelog -S Version)
[ -n "$PKG" ] && [ -n "$VER" ] || die "Source or Version not found in debian/changelog."

if git commit --dry-run -a > /dev/null ; then
	git add debian/changelog
	git commit -m "releasing package $PKG version $VER" -m 'Gbp-Dch: Ignore'
fi

if git commit --dry-run -a > /dev/null ; then
	die "git tree dirty …"
fi

read -n 1 -p "dgit push-source? y/N " DGIT
if [ "$DGIT" = "y" ]; then
	echo
	dgit --gbp push-source
	if [ $? -ne 0 ]; then
		read -n 1 -p "dgit --deliberately-not-fast-forward push-source? y/N " DGIT1
		if [ "$DGIT1" = "y" ]; then
			echo
			dgit --gbp --deliberately-not-fast-forward push-source
		fi
	fi
	if [ $? -eq 0 ]; then
		read -n 1 -p "dpt push? y/N " DPT
		if [ "$DPT" = "y" ]; then
			echo
			dpt push
		fi
	fi
fi
