#!/bin/bash

# the behavior of this launcher can be adjusted by uncommenting and modifying the following values
# in the future, this will likely be replaced by an interface within Stencyl
# _MAX_MEM_="4096"
# _EXTRA_JVM_ARGS_=""
# _PRESERVE_TERMINAL_="false"

# Set extra jvm args to this for hidpi displays
#_EXTRA_JVM_ARGS_="-Dsun.java2d.uiScale=2"

cd "$(dirname "$0")/.."

for arg in "$@"; do
    [[ $arg == -cli ]] && _PRESERVE_TERMINAL_="true"
done

stencylData=${XDG_DATA_HOME:="$HOME/.local/share"}/stencyl

if [ -z "$_MAX_MEM_" ]; then _MAX_MEM_="4096"; fi

checkJdk() { #path
    if [ -f "$1/bin/java" ] && "$1/bin/java" --version | grep "openjdk 25" > /dev/null; then
        echo "Found java at $1"
        export JAVA_HOME="$1"
        return 0
    fi
    return 1
}

downloadCheckJdk() { #remoteArchiveId, #localFolderID
    echo ""
    echo "Stencyl Java 25 runtime not found. It can be downloaded automatically."
    echo "[From URL: https://www.stencyl.com/dl/static/runtimes/stencyl/stencyl-jdk/$1]"
    echo "[To Folder: $stencylData/runtimes/stencyl-jdk/$2]"
    echo ""
    while true; do
        read -p "Would you like to download Java? (y/n) " yn
        case $yn in
            [Yy]* ) break;;
            [Nn]* ) return 1;;
            * ) echo "Please answer yes or no.";;
        esac
    done
    mkdir -p "$stencylData/temp/runtimes/stencyl-jdk/$1/.."
    mkdir -p "$stencylData/runtimes/stencyl-jdk/$2"
    rm -r "$stencylData/temp/runtimes/stencyl-jdk/$1"
    echo "Downloading Java runtime..."
    curl --fail --location "https://www.stencyl.com/dl/static/runtimes/stencyl/stencyl-jdk/$1" --output "$stencylData/temp/runtimes/stencyl-jdk/$1" || return 1
    echo "Extracting Java runtime..."
    tar -xzf "$stencylData/temp/runtimes/stencyl-jdk/$1" -C "$stencylData/runtimes/stencyl-jdk/$2" --strip-components=1 || return 1
    rm "$stencylData/temp/runtimes/stencyl-jdk/$1"
    echo "Java is ready. Launching Stencyl."
    checkJdk "$stencylData/runtimes/stencyl-jdk/$2"
    return $?
}

launchStencyl() {


if [ "$_PRESERVE_TERMINAL_" == "true" ]; then

"$JAVA_HOME/bin/java" \
 -Xmx${_MAX_MEM_}m \
 $_EXTRA_JVM_ARGS_ \
  --add-opens=java.desktop/com.sun.imageio.plugins.png=ALL-UNNAMED \
 --add-opens=java.desktop/java.awt=ALL-UNNAMED \
 --add-opens=java.desktop/javax.swing=ALL-UNNAMED \
 --enable-native-access=ALL-UNNAMED \
 --enable-preview \
 -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel \
 -XX:-OmitStackTraceInFastThrow \
 -Xms64m \
 -classpath "lib/stencyl-v5-toolset.jar:lib/*" \
 stencyl.sw.app.Launcher "$@"

else

nohup "$JAVA_HOME/bin/java" \
 -Xmx${_MAX_MEM_}m \
 $_EXTRA_JVM_ARGS_ \
  --add-opens=java.desktop/com.sun.imageio.plugins.png=ALL-UNNAMED \
 --add-opens=java.desktop/java.awt=ALL-UNNAMED \
 --add-opens=java.desktop/javax.swing=ALL-UNNAMED \
 --enable-native-access=ALL-UNNAMED \
 --enable-preview \
 -Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel \
 -XX:-OmitStackTraceInFastThrow \
 -Xms64m \
 -classpath "lib/stencyl-v5-toolset.jar:lib/*" \
 stencyl.sw.app.Launcher "$@" >/dev/null 2>&1 &

sleep 1

fi

}

if checkJdk "runtimes/jre-linux64" && launchStencyl "$@"; then exit 0; fi
if checkJdk "$stencylData/runtimes/stencyl-jdk/25.0.2b329.72-linux64" && launchStencyl "$@"; then exit 0; fi
if downloadCheckJdk "25.0.2b329.72/stencyl-jdk-25.0.2b329.72-linux64.tar.gz" "25.0.2b329.72-linux64" && launchStencyl "$@"; then exit 0; fi

echo ""
echo "Failed to launch Stencyl."
echo ""
read -n1 -r -p "Press any key to continue..." key
exit 1