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

# Usage: make-release [branch]

set -e

repo=gammu
#toolchain=cmake/Toolchain-mingw32msvc.cmake
toolchain=cmake/Toolchain-mingw32.cmake
toolchain64=cmake/Toolchain-mingw64.cmake

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

srcdir=`pwd`
if [ -f ~/.id/codesigning.spc -a "x$NO_SIGN" = "x" ] ; then
    sign_passwd=`gk-get --user=nijel --object=codesign || true`
    if [ -z "$sign_passwd" ] ; then
        echo -n "Enter sign password: "
        read sign_passwd
    fi
fi

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
    echo "$sign_passwd" | signcode \
        -spc ~/.id/codesigning.spc \
        -v ~/.id/codesigning.pvk \
        -a sha1 \
        -$ individual \
        -n "$2" \
        -t http://timestamp.verisign.com/scripts/timstamp.dll \
        -i http://cihar.com/ \
        "$1"
    rm "$1.bak"
}

checkexe() {
    # Disabled for now because we don't build Python binaries
    return
    if /usr/i*mingw*/bin/objdump -x "$1" | grep -qi 'DLL Name.*msvcrt.dll' ; then
        echo "Program $1 is linked to msvcrt.dll!"
        exit 1
    fi
}


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 $srcdir $repo-$version
cd $repo-$version
git checkout $checkout
git submodule update --init
find . -type d -name .git | xargs -r rm -rf
find . -type f -name .gitignore | 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

echo 'Building Windows version...'
mkdir build-win-shared
cd build-win-shared
cmake ../$repo-$version \
    -DCMAKE_TOOLCHAIN_FILE=../$repo-$version/$toolchain \
    -DBUILD_SHARED_LIBS=ON \
    -DDLL_LIBMYSQL.DLL=~/win-cross/mysql/bin/libmysql.dll \
    -DMYSQL_INCLUDE_DIR=~/win-cross/mysql/include \
    -DMYSQL_LIBRARIES=~/win-cross/mysql/lib/libmysql.a \
    -DWITH_LibDBI=OFF \
    -DINSTALL_BASH_COMPLETION=OFF \
    $CMAKE_EXTRA \

make -j2
checkexe gammu/gammu.exe
signexe gammu/gammu.exe "Gammu"
cpack -G NSIS
cpack -G ZIP
mv Gammu-*-Windows.* $tmp/
cd ..

mkdir build-win-minimal
echo "Building minimal Windows version..."
cd $tmp/build-win-minimal
cmake ../$repo-$version \
    -DCMAKE_TOOLCHAIN_FILE=../$repo-$version/$toolchain \
    -DBUILD_SHARED_LIBS=ON \
    $CMAKE_EXTRA \
    -DCPACK_SYSTEM_NAME=Windows-Minimal \
    -DWITH_Postgres=OFF \
    -DWITH_MySQL=OFF \
    -DWITH_Libintl=OFF \
    -DWITH_Iconv=OFF \
    -DWITH_CURL=OFF \
    -DWITH_Glib=OFF \
    -DWITH_LibDBI=OFF \
    -DINSTALL_BASH_COMPLETION=OFF \

make -j2
checkexe gammu/gammu.exe
signexe gammu/gammu.exe "Gammu"
cpack -G NSIS
cpack -G ZIP
mv Gammu-*-Windows-Minimal.* $tmp/
cd $tmp

mkdir build-win-64
echo "Building 64-bit Windows version..."
cd $tmp/build-win-64
cmake ../$repo-$version \
    -DCMAKE_TOOLCHAIN_FILE=../$repo-$version/$toolchain64 \
    -DBUILD_SHARED_LIBS=ON \
    $CMAKE_EXTRA \
    -DCPACK_SYSTEM_NAME=Windows-64bit \
    -DWITH_Postgres=OFF \
    -DWITH_MySQL=OFF \
    -DWITH_LibDBI=OFF \
    -DINSTALL_BASH_COMPLETION=OFF \

make -j2
checkexe gammu/gammu.exe
signexe gammu/gammu.exe "Gammu"
cpack -G NSIS
cpack -G ZIP
mv Gammu-*-Windows-64bit.* $tmp/
cd $tmp

if false ; then
mkdir build-win-python
for py in 2.6 ; do
    pynodot=`echo $py | tr -d '.'`
    echo "Building minimal Windows version with Python $py..."
    cd $tmp/build-win-python
    cmake ../$repo-$version \
        -DCMAKE_TOOLCHAIN_FILE=../$repo-$version/$toolchain \
        -DBUILD_SHARED_LIBS=OFF \
        $CMAKE_EXTRA \
        -DCPACK_SYSTEM_NAME=Windows-Minimal \
        -DWITH_Postgres=OFF \
        -DWITH_MySQL=OFF \
        -DWITH_GettextLibs=OFF \
        -DWITH_Iconv=ON \
        -DWITH_CURL=OFF \
        -DWITH_LibDBI=OFF \
        -DPYTHON_LIBRARY=~/.wine/drive_c/Python$pynodot/libs/libpython$pynodot.a \
        -DPYTHON_INCLUDE_PATH=~/.wine/drive_c/Python$pynodot/include/ \
        -DPYTHON_EXECUTABLE=~/.wine/drive_c/Python$pynodot/python.exe \
        -DINSTALL_BASH_COMPLETION=OFF \

    make -j2
    mkdir -p $tmp/$repo-$version/python/build/lib.win32-$py/gammu/
    cp python/gammu/*.py $tmp/$repo-$version/python/build/lib.win32-$py/gammu/
    cp python/gammu/*.pyd $tmp/$repo-$version/python/build/lib.win32-$py/gammu/
    cp python/gammu/*.dll $tmp/$repo-$version/python/build/lib.win32-$py/gammu/
    cd ..
    cd $repo-$version/python
    rm -rf gammu/CMakeFiles
    wine \
        ~/.wine/drive_c/Python$pynodot/python.exe setup.py \
        bdist_wininst --skip-build --target-version $py
    mv dist/*.exe $tmp/
    if [ $py != "2.4" ] ; then
        wine \
            ~/.wine/drive_c/Python$pynodot/python.exe setup.py \
            bdist_msi --skip-build --target-version $py
        mv dist/*.msi $tmp/
    fi
    cd $tmp
done
fi

# Sign binaries (not for python-gammu, it breaks some code underneath)
for bin in $tmp/Gammu*.exe ; do
    signexe $bin "Gammu installer"
done

# 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"
    # Submit to PyPi
    $tmp/$repo-$version/python/setup.py register
    # Upload to sf.net
    ./admin/upload-release $version $tmp
fi
