# For debugging:
#  1) snapcraft define desktop-qt5
#  2) export QT_DEBUG_PLUGINS=1
#  3) export SNAPCRAFT_DEBUG=true
# Lessons learned:
#  1) executable must be built into root directory or else the relative path breaks the snap so executable is not found
#  2) snapcraft must be run in the root directory or else the executable is not included in the snap package
#  3) desktop-qt5 only snaps a minimal qt application, so other packages must be added by us
#  4) sql plugins must be added so sqlite is available, which prevents a runtime error message about missing qsqlite
#     that disappeared when the help feature started working
#
# Error like 'Authorization header missing' is fixed with 'snapcraft login'

echo "Run in Kubuntu 16.04 virtual machine"

echo "Removing existing snap, if any, so it will not be confused with new snap if this script is interrupted"
sudo snap remove engauge-digitizer

# Build into the build directory
echo "Removing previous build, if any"
rm -rf build
rm -rf documentation

mkdir -p build/desktop-file
mkdir -p build/meta/gui
cd build
BUILD=`pwd`

# Make sure official qt is used
PATH=/usr/bin:$PATH
LD_LIBRARY_PATH=

sed 's/DESTDIR = bin/DESTDIR = ./g' ../../../engauge.pro >engauge.pro

cp ../snapcraft.yaml .
cp ../../engauge-digitizer.desktop desktop-file
cp ../../../src/img/digitizer-32.png meta/gui/icon.png
cp -r ../../../help .
cp -r ../../../src .

export POPPLER_INCLUDE=/usr/include/poppler/qt5
export POPPLER_LIB=/usr/lib/x86_64-linux-gnu
qmake engauge.pro CONFIG+=log4cpp_null CONFIG+=pdf
make clean
make -j4

cd help
./build.bash $BUILD/documentation
cd ..

snapcraft clean engauge-digitizer
snapcraft
sudo snap install engauge-digitizer_*.snap --devmode --dangerous
echo "Remaining steps:"
echo "1) Test the just-installed snap executing 'engauge-digitizer',"
echo "2) 'snapcraft push build/engauge-digitizer-*.snap'"
echo "3) 'snapcraft list-revisions engauge-digitizer' to see revision number"
echo "4) 'snapcraft release engauge-digitizer <revision> stable'"
echo "5) log into https://dashboard.snapcraft.io/snaps, select newest upload and then click on Release buttons"
echo "6) on the web page, click Make Public button"
