#!/bin/bash

# Run this on mark-HP-Notebook in Kubuntu 18.04 (64 bit)
#
# Assumes prerequisites are already installed:
# 1) Starting at https://flatpak.org/setup and picking Kubuntu (versus Ubuntu) leads
#    to steps installing with plasma instead of gnome.
#    Be sure to add the flathub repository
# 2) Then installing SDK according to http://docs.flatpak.org/en/latest/first-build.html.
#    This step will fail if the flathub repository was not added in the previous step.
#    installed:
#      org.freedesktop.Platform
#      org.freedesktop.Platform.Locale
#      org.freedesktop.Sdk
#      org.freedesktop.Platform.GL.nvidia-389-116
#      org.freedesktop.Sdk.Locale
#      org.freedesktop.Platform.html5-codecs
#      org.freedesktop.Platform.VAAPI.Intel
# 3) sudo apt install flatpak-builder
# 4) Install engauge-specific github repo
#      cd $HOME
#      git clone git@github.com:flathub/io.github.markummitchell.Engauge_Digitizer.git
# 5) Running this script gives error about org.kde.Sdk/x86_64/5.11 not installed so
#      flatpak install flathub org.kde.Sdk
#    and pick the version option matching the error message
# 6) Running this script gives error about org.kde.Platform/x86_64/5.11 not installed so
#      flatpak install flathub org.kde.Platform
#    and pick the version option matching the error message
# 7) Got error:
#      Remote listing engauge-digitizer not available; server has no summary file.
#      Check the URL passed to remote-add was valid
#      error: app/io.github.markummitchell.Engauge_Digitizer/x86_64/master not installed
#    fix: flatpak remote-add --user --no-gpg-verify engauge-digitizer \
#         file:///home/mark/io.github.markummitchell.Engauge_Digitizer/repo
# 8) Issue 311 'Flatpak is missing default filename on Save' occurs on at least Mint/Cinnamon,
#    with SaveAs file default file and file extension not set. Fix requires multiple steps to
#    environment (outside of Engauge):
#      + sudo apt install plasma-discover-flatpak-backend     (one time step)
#      + sudo apt install xdg-desktop-portal-kde              (one time step)
#      + find /usr -name xdg-desktop-portal                   (find xdg-desktop-portal executable)
#      + find /usr -name xdg-desktop-portal-kde               (find xdg-desktop-portal-kde executable)
#      + kill xdg-desktop-portal
#      + kill xdg-desktop-portal-kde
#      + export XDG_CURRENT_DESKTOP=KDE
#      + /usr/.../xdg-desktop-portal-kde &
#      + /usr/.../xdg-desktop-portal &                        (this connects later flatpak apps to KDE)
#      + flatpak run io.github.markummitchell.Engauge_Digitizer

if [[ -z $1 ]]; then
    echo "Usage: build_flathub <version>"
    exit
fi    
TARBALLDOWNLOAD="v$1.tar.gz"
TARBALLLOCAL=/tmp/$TARBALLDOWNLOAD
echo "Downloading $TARBALLDOWNLOAD"

echo "Update the version changes in dev/gnome/engauge-digitizer.appdata.xml"
read -n 1 -s -r -p "Press any key to continue"

# If next line fails then create a prelease at github. May need to Publish. Also may need to wait for tarball to be built
wget "https://github.com/markummitchell/engauge-digitizer/archive/$TARBALLDOWNLOAD" --output-document="$TARBALLLOCAL"
CKSUM=`sha256sum $TARBALLLOCAL | awk '{print $1}'`

cd $HOME/io.github.markummitchell.Engauge_Digitizer
rm -rf app >/dev/null
rm -rf repo >/dev/null

JSONFILE=io.github.markummitchell.Engauge_Digitizer.json
TMPJSONFILE=/tmp/$JSONFILE

git pull

# We replace following two lines:
#    "url": "https://github.com/markummitchell/engauge-digitizer/archive/v#.#.tar.gz",
#    "sha256": "..."
# N; flag appends next line to current line
sed 'N;s|"url": "https://github.com/markummitchell/engauge-digitizer/archive/v.\+.gz",\s*\n\s*"sha256":\s*"[0123456789abcdef]\+"|"url": "https://github.com/markummitchell/engauge-digitizer/archive/'$TARBALLDOWNLOAD'",\n                                        "sha256": "'$CKSUM'"|' $JSONFILE >$TMPJSONFILE
mv $TMPJSONFILE $JSONFILE

rm -rf app 
flatpak-builder --ccache --repo=repo --subject="Test" app $JSONFILE

# Uninstall previous if it exists
LISTOLD=`flatpak list --app | grep Engauge_Digitizer`
if [[ ! -z $LISTOLD ]]; then
    flatpak uninstall io.github.markummitchell.Engauge_Digitizer
fi

flatpak remote-add --if-not-exists engauge-digitizer repo --no-gpg-verify
flatpak install engauge-digitizer io.github.markummitchell.Engauge_Digitizer
flatpak run io.github.markummitchell.Engauge_Digitizer

echo "If testing was successful then commit the changes in $HOME/io.github.markummitchell.Engauge_Digitizer."
echo "After approximately one hour the new version should appear at "
echo "https://flathub.org/apps/details/io.github.markummitchell.Engauge_Digitizer"

