#!/bin/sh
# vim: expandtab sw=4 ts=4 sts=4:

# Usage: make-release [branch]

set -e

repo=gammu

version=`sed -n 's/set (GAMMU_VERSION "\([^"]*\)".*)/\1/p' CMakeLists.txt`

srcdir=`pwd`

CMAKE_EXTRA=""
if [ "x$1" = "x--debug" ] ; then
    shift
    CMAKE_EXTRA="$CMAKE_EXTRA -DCMAKE_BUILD_TYPE=Debug"
fi

dotag=0
if [ "x$1" = "xbranch" ] ; then
    checkout=HEAD
    dotag=1
    # Pre release checks
    if grep -q '^20[0-9][0-9]???? - ' ChangeLog ; then
        echo 'ChangeLog does not seem to be finalised, aborting!'
        exit 1
    fi
    shift
elif [ "x$1" = "x" ] ; then
    checkout=HEAD
else 
    checkout=$1
    version=$1
    shift
fi

tmp=`mktemp -dt $repo-build-XXXXXX`
cd $tmp
echo Working in $tmp
echo "Creating release $version from $checkout"
git clone --shared $srcdir $repo-$version
cd $repo-$version
git checkout $checkout
find . -name .git | xargs -r rm -rf
find . -type f -name .gitignore | xargs -r rm -f
find . -type f -name .gitmodules | xargs -r rm -f
cd ..

echo 'Creating source tarballs...'
tar cfz $repo-$version.tar.gz $repo-$version
tar cfj $repo-$version.tar.bz2 $repo-$version
tar cfJ $repo-$version.tar.xz $repo-$version
zip -r $repo-$version.zip $repo-$version

# We're done
echo "Release is in $tmp directory"
ls -lh $tmp

cd "$srcdir"

if [ $dotag -eq 1 ] ; then
    # Tag the release
    git tag -s -m "Tag release $version" "$version"
    # Upload to sf.net
    ./admin/upload-release $version $tmp
fi
