#!/bin/bash

# (C) 2015-2020 Christoph Berg <myon@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.

set -eu

# basic variables

template_dir="/usr/share/postgresql-common/dh_make_pgxs/debian"

DIRECTORY="$(basename $PWD)"
NAME="${DIRECTORY%-*}" # Upstream name
VERSION="${DIRECTORY##*-}"
URL="https://FIXME/$NAME"

# options

while getopts "fh:n:v:" opt ; do
    case $opt in
        f) FORCE=yes ;;
        h) URL="$OPTARG" ;;
        n) NAME="$OPTARG" ;;
        v) VERSION="$OPTARG" ;;
        *) exit 5 ;;
    esac
done
shift $((OPTIND - 1)) # shift away args

# more variables

SOURCE="${NAME//_/-}" # Debian name
EXTNAME="$(echo $SOURCE | sed -e 's/^\(postgresql\|pgsql\|pg\)-//')" # binary package name suffix
DHVERSION="$(dpkg-query --showformat '${Version}' --show debhelper)"
COMPAT="${DHVERSION%%.*}"
STANDARDS_VERSION="$(apt-cache show debian-policy | sed -n 's/Version: \(.*\)\..*/\1/p' | head -n1)"
USERNAME=${LOGNAME:-${USER:-root}}
MAINTAINER_NAME=$(getent passwd $USERNAME | cut -d : -f 5 | sed -e 's/,.*//')
: ${DEBEMAIL:=$USERNAME@localhost}
YEAR=$(date +%Y)

echo "Upstream (-n): $NAME"
echo "Version (-v):  $VERSION"
echo "Source:        $SOURCE ($VERSION-1)"
echo "Binaries:      postgresql-PGVERSION-$EXTNAME ($VERSION-1)"
echo "Uploader:      $MAINTAINER_NAME <$DEBEMAIL>"
echo "Homepage (-h): $URL"
echo
if [ -t 0 ]; then
    echo -n "Press Enter to continue, ^C to abort "
    read
fi

# install files

install_dir ()
{
    local directory="debian/$1"
    #[ -z "$directory" ] && return
    [ -d "$directory" ] && return
    echo "Creating $directory/"
    mkdir "$directory"
}

install_template ()
{
    local template="$1"

    if [ "${FORCE:-}" ] || ! [ -e debian/$template ]; then
        echo "Installing debian/$template"
        sed -e "s/@COMPAT@/$COMPAT/g" \
            -e "s/@EXTNAME@/$EXTNAME/g" \
            -e "s/@NAME@/$NAME/g" \
            -e "s/@STANDARDS_VERSION@/$STANDARDS_VERSION/g" \
            -e "s/@SOURCE@/$SOURCE/g" \
            -e "s/@MAINTAINER_NAME@/$MAINTAINER_NAME/g" \
            -e "s/@DEBEMAIL@/$DEBEMAIL/g" \
            -e "s;@URL@;$URL;g" \
            -e "s/@YEAR@/$YEAR/g" \
            "$template_dir/$template" > "debian/$template"
        if [ -x $template_dir/$template ]; then
            chmod +x "debian/$template"
        fi
    else
        echo "Keeping existing debian/$template"
    fi
}

mkdir -p debian

for template in $(find $template_dir -mindepth 1 | sort); do
    case $template in
        *.swp|*~) continue ;; # skip vim stuff
    esac
    basename=${template##$template_dir/}
    if [ -d $template ]; then
        install_dir "$basename"
    else
        install_template "$basename"
    fi
done

if [ "$COMPAT" -lt "12" ]; then
    sed -i -e "s/debhelper-compat[^,]*/debhelper (>= $COMPAT)/" debian/control*
    echo "$COMPAT" > debian/compat
fi

echo "Updating debian/control from debian/control.in"
pg_buildext updatecontrol

if [ "${FORCE:-}" ] || ! [ -e debian/changelog ]; then
    rm -f debian/changelog
    echo "Creating debian/changelog"
    if [ -x /usr/bin/dch ]; then
        dch --create --package "$SOURCE" --newversion "$VERSION-1"
    else
        cat > debian/changelog <<-EOT
	$SOURCE ($VERSION-1) UNRELEASED; urgency=medium

	  * Initial release. (Closes: #XXXXXX)

	 -- $MAINTAINER_NAME <$DEBEMAIL>  $(date -R)
	EOT
    fi
else
    echo "Keeping existing debian/changelog"
fi
