#!/bin/bash
# Copyright (C) 2014 by Bundesamt f�r Sicherheit in der Informationstechnik
# Software engineering by Intevation GmbH
#
# This file is Free Software under the GNU GPL (v>=2)
# and comes with ABSOLUTELY NO WARRANTY!
# See LICENSE.txt for details.

#NSSDIR is hackish as this will be replaced by static compiling 
# anyway
TMPDIR=$(mktemp -d)
TMPINST=$(mktemp)

EXEFILES=$(find . -name trustbridge-certificate-installer.exe -o -name trustbridge.exe -o -name trustbridge-nss-installer.exe)
HELPDIR=@CMAKE_BINARY_DIR@/doc/help/client/html

cp $EXEFILES $TMPDIR
cp -r $HELPDIR $TMPDIR/doc

echo "; This file is autogenerated." > @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
echo "; This file is autogenerated." > @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh
OLDDIR=$(pwd)
cd $TMPDIR
for file in `find * -name \*.exe`; do
    echo File \"\${files_dir}\${path_sep}$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
    if [ "$file" = "trustbridge-nss-installer.exe" ]; then
        # The nss installer is handled specially on uninstallation.
        continue
    fi
    echo Delete \"\$INSTDIR\\$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh
done
cd $TMPDIR/doc

for curdir in `find * -maxdepth 0 -type d -not -path .`; do
    cd $curdir
    curpath=\"\$INSTDIR\\doc\\$curdir\"
    echo SetOutPath $curpath >> @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
    for file in `find * -maxdepth 0 -type f`; do
        echo File \"\${files_dir}\${path_sep}doc\${path_sep}$curdir\${path_sep}$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
        echo Delete \"\$INSTDIR\\doc\\$curdir\\$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh
    done
    echo RMDir $curpath >> @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh
    cd -
done

echo SetOutPath \"\$INSTDIR\\doc\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
for file in `find * -maxdepth 0 -type f -not -path .buildinfo`; do
    echo File \"\${files_dir}\${path_sep}doc\${path_sep}$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_in.nsh
    echo Delete \"\$INSTDIR\\doc\\$file\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh
done
echo RMDir \"\$INSTDIR\\doc\" >> @CMAKE_CURRENT_BINARY_DIR@/filelist_un.nsh

cd $OLDDIR
EST_SIZE=$(du -s $TMPDIR | cut -f 1)

echo $TMPDIR
export LC_ALL="de_DE.latin-1" 

makensis -Dfiles_dir=$TMPDIR \
    -Dcompany="Bundesamt f�r Sicherheit in der Informationstechnik" \
    -Dplugin_dir="@CMAKE_CURRENT_BINARY_DIR@" \
    -Dversion_number=@PROJECT_VERSION@ \
    -Dsetupname="$TMPINST" \
    -Dproductname="TrustBridge" \
    -Ddescription="TrustBridge" \
    -Dpath_sep="/" \
    -Dinfo_url="http://wald.intevation.org/projects/trustbridge/" \
    -Dsize=$EST_SIZE \
    -DWRITE_UNINSTALLER \
    -Dproductname_short="TrustBridge" @CMAKE_SOURCE_DIR@/packaging/trustbridge.nsi
wine $TMPINST /S || true
mv ~/.wine/drive_c/tmp-uninstaller/Uninstall.exe $TMPDIR
rmdir ~/.wine/drive_c/tmp-uninstaller
rm $TMPINST

makensis -Dfiles_dir=$TMPDIR \
    -Dcompany="Bundesamt f�r Sicherheit in der Informationstechnik" \
    -Dplugin_dir="@CMAKE_CURRENT_BINARY_DIR@" \
    -Dversion_number=@PROJECT_VERSION@ \
    -Dsetupname="@CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@.exe" \
    -Dproductname="TrustBridge" \
    -Ddescription="TrustBridge" \
    -Dpath_sep="/" \
    -Dinfo_url="http://wald.intevation.org/projects/trustbridge/" \
    -Dsize=$EST_SIZE \
    -Dproductname_short="TrustBridge" @CMAKE_SOURCE_DIR@/packaging/trustbridge.nsi

osslsigncode sign -certs @CMAKE_SOURCE_DIR@/ui/tests/data/codesign/codesigning.pem \
   -key @CMAKE_SOURCE_DIR@/ui/tests/data/codesign/codesigning.key \
   -h sha256 -in @CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@.exe \
   -out @CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@-signed.exe
mv @CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@-signed.exe @CMAKE_BINARY_DIR@/TrustBridge-@PROJECT_VERSION@.exe

rm -r $TMPDIR
