#!/bin/sh
# Copyright 2011 Simon Cross <hodgestar@gmail.com>
# GPL - see COPYING for details

SIGNER=$1
if [ "x$SIGNER" = "x" ] ; then
    echo "Usage: sutekh-makedeb <signer>"
    echo "  * Signer is the name and email address of the package signer."
    echo "  It is also used to select the key to sign with."
    exit 1
fi

SUITES="oneiric natty maverick lucid"
SUTEKH_VERSION=`PYTHONPATH=. python -c "from sutekh import SutekhInfo; print SutekhInfo.SutekhInfo.VERSION_STR"`
DEB_VERSION=`echo "$SUTEKH_VERSION" | sed -e "s/\(rc\|a\|b\)/~\1/"`
SDIST_FILE="dist/Sutekh-${SUTEKH_VERSION}.tar.gz"
DEBPATCH_FOLDER="deb-patches"

make_deb () {

    SUITE=$1
    EXTRA_CFG="deb_dist/sutekh-$SUITE.cfg"

    # make extra config file
    touch "$EXTRA_CFG"
    echo "[Sutekh]" >> $EXTRA_CFG
    echo "Debian-Version: 1~$SUITE" >> $EXTRA_CFG
    echo "Forced-Upstream-Version: $DEB_VERSION" >> $EXTRA_CFG

    PATCHFILE=""
    if [ -f "$DEBPATCH_FOLDER/$SUITE.diff" ]; then
       # Add suite-specific patch file to the extra config file
       cp "$DEBPATCH_FOLDER/$SUITE.diff" deb_dist
       PATCHFILE="-p deb_dist/$SUITE.diff"
    elif [ -f "$DEBPATCH_FOLDER/makedeb.diff" ]; then
       # Add general patch file if it exists
       cp "$DEBPATCH_FOLDER/makedeb.diff" deb_dist
       PATCHFILE="-p deb_dist/makedeb.diff"
    fi

    # build source tarball and .dsc
    py2dsc -z "$SUITE" -x "$EXTRA_CFG" $PATCHFILE "$SDIST_FILE"

    # build .changes file (see https://github.com/astraw/stdeb/issues/37)
    cd deb_dist/sutekh-${DEB_VERSION}
    debuild -S -e"$SIGNER"
    cd ../..

}

# santize environment
rm -rf deb_dist
mkdir deb_dist

# make source tarball
python setup.py sdist

# build Debian source package for each suite
for suite in $SUITES ; do
    echo "================================="
    echo "Making .deb for: $suite"
    echo "================================="
    make_deb $suite
done

# don't upload for now
echo "================================="
echo "Now do:"
echo " dput ppa:sutekh/ppa <source.changes>"
