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

# Usage: make-release [branch]

set -e

repo=gammu

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

srcdir=`pwd`

signexe() {
    if [ ! -f ~/.id/codesigning.spc ] ; then 
        echo 'Skipping signing, no certificates!'
        return 0
    fi
    if [ "x$NO_SIGN" != "x" ] ; then
        echo 'Skipping signing, disabled!'
        return 0
    fi
    gk-get --user=nijel --object=codesign | signcode \
        -spc ~/.id/codesigning.spc \
        -v ~/.id/codesigning.pvk \
        -$ individual \
        -n "$2" \
        -t http://timestamp.verisign.com/scripts/timstamp.dll \
        -i http://cihar.com/ \
        "$1"
    rm "$1.bak"
}

checkexe() {
    if i586-mingw32msvc-objdump -x "$1" | grep -qi 'DLL Name.*msvcrt.dll' ; then
        echo "Program $1 is linked to msvcrt.dll!"
        exit 1
    fi
}

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

tmp=`mktemp -dt $repo-build-XXXXXX`

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

cd $tmp
echo Working in $tmp
if [ "x$1" = "xbranch" ] ; then
    shift
    rel=RELEASE_`echo -n $version|tr . _`
    svn cp -m "Tag release $version" svn+ssh://mort/home/svn/$repo/trunk svn+ssh://mort/home/svn/$repo/tags/$rel
    svn export svn+ssh://mort/home/svn/$repo/tags/$rel $repo-$version
elif [ "x$1" = "x" ] ; then
    rel=trunk
    svn export svn+ssh://mort/home/svn/$repo/trunk $repo-$version
else
    version="$1"
    shift
    rel=RELEASE_`echo -n $version|tr . _`
    svn export svn+ssh://mort/home/svn/$repo/tags/$rel $repo-$version
fi

echo "Creating release $version from $rel"


echo 'Creating source tarballs...'
tar cfz $repo-$version.tar.gz $repo-$version
tar cfj $repo-$version.tar.bz2 $repo-$version

echo 'Building Windows version...'
mkdir build-win-shared
cd build-win-shared
cmake ../$repo-$version \
    -DCROSS_MINGW=ON \
    -DENABLE_SHARED=ON \
    $CMAKE_EXTRA \
    -DCURL_CROSS_PATH=$srcdir/../../win-cross/curl \
    -DMYSQL_CROSS_PATH=$srcdir/../../win-cross/mysql
# Disabled, it seems to cause crashes:
#    -DGETTEXT_CROSS_PATH=$srcdir/../../win-cross/gettext \
#    -DPOSTGRES_CROSS_PATH=$srcdir/../../win-cross/pgsql \

make -j2
checkexe gammu/gammu.exe
signexe gammu/gammu.exe "Gammu"
cpack
mv Gammu-*-Windows.* ../
cd ..

echo 'Building minimal Windows version...'
mkdir build-win-minimal
cd build-win*minimal
cmake ../$repo-$version \
    -DCROSS_MINGW=ON \
    -DENABLE_SHARED=OFF \
    $CMAKE_EXTRA \
    -DPACKAGE_NAME=Windows-Minimal \
    -DMYSQL_CROSS_PATH= \
    -DPOSTGRES_CROSS_PATH= \
    -DGETTEXT_CROSS_PATH= \

make -j2
checkexe gammu/gammu.exe
signexe gammu/gammu.exe "Gammu"
cpack
mv Gammu-*-Windows-Minimal.* ../
cd ..

# Sign binaries
for bin in $tmp/*.exe ; do
    signexe $bin "Gammu installer"
done

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