#!/bin/sh
set -e
cabal=hashed-storage.cabal

darcs wh && {
    echo "There are unrecorded changes. This sounds like a bad idea."
    exit 1
}

version=`grep -i ^version: $cabal | sed -e 's,^version: *,,'`
if test "$1" = "-i"; then
    maj=`echo $version | cut -d. -f1-2`
    min=`echo $version | cut -d. -f3`
    new="$maj.$(($min + 1))"
    sed -i~ -e "/^version: */s,$version,$new, " $cabal
    darcs rec $cabal -am "Bump version to $new"
    darcs tag -m $new
    echo "Hit ^C now to abort and fix up, or enter to continue."
    read
else
    test `darcs chan --from-tag $version --count` = 0 || {
        darcs chan --from-tag $version
        echo "You have patches after current release ($version) tag."
        echo "Sounds like a bad idea to me. Maybe try with -i?"
        exit 1
    }
fi

tar=`cabal sdist | grep "^Source tarball created:" | cut -d: -f2 | cut -c1-`
tar xvzf $tar
dir=`echo "$tar" | sed -e 's,dist/\(.*\)\.tar\.gz$,\1,'`
(cd $dir && cabal configure -ftest && cabal build && cabal test)

echo
echo "### Alles gut. Now run:"
echo "cabal upload -v3 $tar"

