#!/bin/sh

TEMP=$(getopt -o 'aed:pt:' -n "$(basename "$0")" -- "$@")
if [ $? -ne 0 ]; then
        echo 'Terminating...' >&2
        exit 1
fi
eval set -- "$TEMP"
unset TEMP

T=
TT=n
ED=n
DEB=
PYPI=y
VERS=
while true ; do
    case "$1" in
        '-a')
            TT=y
            shift ;;
        '-d')
            DEB="$2"
            shift 2 ;;
        '-p')
            PYPI=n
            shift ;;
        '-e')
            ED=y
            shift ;;
        '-t')
            T="$2"
            shift 2 ;;
        '--')
            break ;;
        *)
            echo "Unknown argument '$1'" >&2
            exit 1
    esac
done

set -ex
if test -n "$T" ; then
    :
elif test "$TT" = "y" ; then
    T=$(git describe --tags | sed -e 's/-.*//')
else
    T=$(git describe --tags --exact-match)
fi
TT=$(echo $T | sed -e 's/^[a-z]*//')

if test -n "$DEB" ; then
    git describe "$DEB" >/dev/null
elif git describe --all deb >/dev/null 2>&1; then
    DEB=deb
elif git describe --all debian >/dev/null 2>&1; then
    DEB=debian
else
    echo "Neither 'deb' nor 'debian' branches found." >&2
    exit 1
fi

B="$(git rev-parse --abbrev-ref HEAD)"
if test "$B" = "$DEB" ; then
    echo "You can't be on the '$DEB' branch when you do this." >&2
    exit 1
fi

git checkout "$DEB"
git merge --no-commit "$B"
D=$(dpkg-parsechangelog -S distribution)
if dpkg-parsechangelog -S version | grep -qs -- - ; then V="-1" ; else V=".1"; fi
debchange --distribution $D --force-distribution -v "$TT$V" "Merge"
git add debian/changelog
if test $ED = y ; then
    echo "Edit some stuff, then 'exit'"
    bash
fi
git commit -a -m "Merge to $T"
debch
git checkout "$B"
if test $PYPI = "y" ; then
    if test -s setup.py || test -s pyproject.toml ; then
        make pypi
    fi
fi
