#!/usr/bin/bash
#
# ENGDIR is two levels above the current directory
DIR=`pwd`
export ENGDIR=${DIR%/*/*}
export QTBINDIR=`qtpaths --binaries-dir`
export QTPLUGINSDIR=`qtpaths --plugin-dir`
export QTTRANSDIR="${QTPLUGINSDIR%/*}/translations"
if [ -z "$QTBINDIR" ]; then
    echo "qtpaths utility is not in PATH, which should be pointing to the Qt5 bin directory"
    exit
fi
#
echo "Engauge:         $ENGDIR"
echo "Qt binaries:     $QTBINDIR"
echo "Qt plugins:      $QTPLUGINSDIR"
echo "Qt translations: $QTTRANSDIR"
#
echo " "
where g++
where mingw32-make
echo " "
#
read -p 'If the g++ compiler and mingw32-make tools are first in a directory like C:\QtOpenSource\Qt5.5.1\Tools\mingw492_32\bin, press Enter to continue, otherwise Control-C and fix the PATH variable'
#
export FROMQCH="$ENGDIR/bin/documentation/engauge.qch"
export FROMQHC="$ENGDIR/bin/documentation/engauge.qhc"
export FROMICO="$ENGDIR/src/img/digitizer.ico"
export FROMDLL="$QTBINDIR"
if ([ ! -f "$FROMQCH" ] || [ ! -f "$FROMQHC" ]); then
    echo "File $FROMQCH and/or $FROMQHC does not exist. Run $ENGDIR/help/build.mingw to create those files"
    exit
fi
#
echo " "
ls -ls $FROMQCH | awk '{print $7,$8,$9,$10}'
ls -ls $FROMQHC | awk '{print $7,$8,$9,$10}'
echo " "
#
read -p 'If the help input files are up to date, press Enter to continue'
#
VERSION_NUMBER_CODE=`fgrep -e 'char *VERSION_NUMBER' ../../src/util/Version.cpp | awk -F '"' '{print $2}'`
VERSION_NUMBER_WIX=`fgrep -e 'Version' engauge.wxs | grep -v Installer | awk -F "'" '{print $2}'`
#
echo " "
echo "Version number in C++ code (Version.cpp)=$VERSION_NUMBER_CODE"
echo "Version number in Windows WiX installer (engauge.wxs)=$VERSION_NUMBER_WIX"
echo " "
#
read -p 'If the version numbers are correct, press Enter to continue'
#
VERSION_NUMBER_CODE=`echo $VERSION_NUMBER_CODE | sed 's/\./_/g'`
#
echo "***Copying help input files"
mkdir -p documentation
cp $FROMQCH  ../../bin/documentation
cp $FROMQHC  ../../bin/documentation
#
echo "***Copying DLL libraries from $FROMDLL"
cp $FROMDLL/libwinpthread-1.dll ../../bin/
#
echo "***Copying icon file"
cp $FROMICO  ../../bin
#
# Notes:
# 1) Need gcc 4 bundled with Qt rather than gcc 3 at /usr/bin to prevent 'unrecognized command line
#    option -fno-keep-inline-dllexport'
# 2) Running 'make -j 4' exhausts system resources which hangs the build
# 3) windeployqt builds the release files in the same directory as the executable
# 4) PATH must include $QTBINDIR but in the /C/... format rather than the C:/... format that $QTBINDIR is in
# 5) qt dislikes mingw to the point that it creates Makefiles with mangled library names, which we fix using sed
echo "***Rebuilding release executable" &&
export PATH="$QTBINDIR:$PATH"
export ENGAUGE_RELEASE=1 &&
cd ../.. &&
qmake engauge.pro &&
sed 's/-lQt/-lQt5/g' Makefile >Makefile~ &&
sed 's/551//g' Makefile~ >Makefile &&
mingw32-make clean &&
mingw32-make &&
cd dev/windows &&
echo "***Rebuilding release directory" &&
windeployqt -release ../../bin/engauge.exe &&
echo "***Compiling release files" &&
candle.exe engauge.wxs &&
candle.exe WixUI_InstallDir_NoLicense.wxs &&
echo "***Creating MSI" &&
light.exe -ext WixUIExtension -ext WixUtilExtension engauge.wixobj WixUI_InstallDir_NoLicense.wixobj -o digit-exe-windows-32-bit-installer-$VERSION_NUMBER_CODE.msi
#
echo "***Copying files for creating zip file" 
mkdir -p "Engauge Digitizer/bearer"
mkdir -p "Engauge Digitizer/documentation"
mkdir -p "Engauge Digitizer/iconengines"
mkdir -p "Engauge Digitizer/imageformats"
mkdir -p "Engauge Digitizer/platforms"
mkdir -p "Engauge Digitizer/printsupport"
mkdir -p "Engauge Digitizer/sqldrivers"
mkdir -p "Engauge Digitizer/translations"
cp $QTPLUGINSDIR/bearer/qgenericbearer.dll "Engauge Digitizer/bearer"
cp $QTPLUGINSDIR/bearer/qnativewifibearer.dll "Engauge Digitizer/bearer"
cp $FROMQCH "Engauge Digitizer/documentation"
cp $FROMQHC "Engauge Digitizer/documentation"
cp $QTPLUGINSDIR/iconengines/qsvgicon.dll "Engauge Digitizer/iconengines"
cp $QTPLUGINSDIR/imageformats/qdds.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qgif.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qicns.dll "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qico.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qjp2.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qjpeg.dll "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qmng.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qsvg.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qtga.dll  "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qtiff.dll "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qwbmp.dll "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/imageformats/qwebp.dll "Engauge Digitizer/imageformats"
cp $QTPLUGINSDIR/platforms/qwindows.dll "Engauge Digitizer/platforms"
cp $QTPLUGINSDIR/printsupport/windowsprintersupport.dll "Engauge Digitizer/printsupport"
cp $QTPLUGINSDIR/sqldrivers/qsqlite.dll   "Engauge Digitizer/sqldrivers"
cp $QTPLUGINSDIR/sqldrivers/qsqlmysql.dll "Engauge Digitizer/sqldrivers"
cp $QTPLUGINSDIR/sqldrivers/qsqlodbc.dll  "Engauge Digitizer/sqldrivers"
cp $QTTRANSDIR/*.qm  "Engauge Digitizer/translations"
cp ../../LICENSE                 "Engauge Digitizer"
cp ../../bin/D3Dcompiler_47.dll  "Engauge Digitizer"
cp ../../bin/engauge.exe         "Engauge Digitizer"
cp ../../bin/libEGL.dll          "Engauge Digitizer"
cp ../../bin/libgcc_s_dw2-1.dll  "Engauge Digitizer"
cp ../../bin/libGLESV2.dll       "Engauge Digitizer"
cp ../../bin/libstdc++-6.dll     "Engauge Digitizer"
cp ../../bin/libwinpthread-1.dll "Engauge Digitizer"
cp ../../bin/opengl32sw.dll      "Engauge Digitizer"
cp ../../bin/Qt5*.dll            "Engauge Digitizer"
#
echo "***Building zip file"
/C/cygwin64/bin/zip -r digit-exe-windows-32-bit-without-installer-file-$VERSION_NUMBER_CODE.zip "Engauge Digitizer"
