#!/bin/bash
set -euo pipefail
cd $(dirname $0)/../..

C_BLUE_FG="\e[34m"
C_RESET="\e[0m"

PROGNAME=$(basename $0)

# JRE comes from https://adoptopenjdk.net/releases.html

JRE_LINUX_URL=https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u282-b08/OpenJDK8U-jre_x64_linux_hotspot_8u282b08.tar.gz
JRE_LINUX_SHA256=3b2e2c6ad3ee04a58ffb8d629e3e242b0ae87b38cfd06425e4446b1f9490f521

JRE_MACOS_URL=https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u282-b08/OpenJDK8U-jre_x64_mac_hotspot_8u282b08.tar.gz
JRE_MACOS_SHA256=9e7a40d570d5151aae23a2fb017359248f5fb82c547c3ecd860c992770228afb

JRE_WINDOWS_URL=https://github.com/AdoptOpenJDK/openjdk8-binaries/releases/download/jdk8u282-b08/OpenJDK8U-jre_x64_windows_hotspot_8u282b08.zip
JRE_WINDOWS_SHA256=58f2bbf0e5abc6dee7ee65431fd2fc95cdb2c3d10126045c5882f739dda79c3b

PACKR_URL=https://github.com/libgdx/packr/releases/download/4.0.0/packr-all-4.0.0.jar
PACKR_SHA256=e2047f5b098bd5ca05150a530f3ada3a7f07bd846be730b92378180bdd3d8be2

DIST_OUT_BASE_DIR=$PWD/dist-out
SRC_DESKTOP_JAR="desktop/build/libs/desktop-1.0.jar"
DST_DESKTOP_JAR=$DIST_OUT_BASE_DIR/pixelwheels.jar

# Files added to the archives
PACKAGING_DIR=$PWD/tools/packaging
PACKAGING_COMMON_DIR=$PACKAGING_DIR/common
PACKAGING_MACOS_DIR=$PACKAGING_DIR/macos
PACKAGING_LINUX_DIR=$PACKAGING_DIR/linux

. tools/functions.sh

copy_desktop_jar() {
    echo "Copying desktop.jar"
    mkdir -p $DIST_OUT_BASE_DIR
    cp "$SRC_DESKTOP_JAR" "$DST_DESKTOP_JAR"
}

# Create archive for a given platform, using packr
# Parameters:
# - $1 path to jre archive
# - $2 packr platform name
create_archive() {
    local jre=$1
    local platform=$2

    local dist_name=pixelwheels-$VERSION-$platform
    local out_dir=$DIST_OUT_BASE_DIR/$dist_name
    local extra_args=""

    if [ "$platform" = "mac" ] ; then
        # On macOS the directory passed to --output is the bundle itself, so
        # create it as a subdirectory of $out_dir and add a .app extension
        out_dir=$out_dir/$dist_name.app

        # On macOS the jar must be called with -XstartOnFirstThread so that
        # LWJGL3 can process input events.
        # Game won't start if we don't do this.
        extra_args="--vmargs XstartOnFirstThread
            --icon $PACKAGING_MACOS_DIR/pixelwheels.icns
            --bundle com.agateau.pixelwheels"
    fi

    echo -e "${C_BLUE_FG}Creating archive for $platform${C_RESET}"

    rm -rf "$out_dir"

    echo "Running packr"
    set -x
    java -jar "$PACKR_PATH" \
        --jdk "$jre" \
        --platform "$platform" \
        --minimizejre soft \
        --executable pixelwheels \
        --mainclass com.agateau.pixelwheels.desktop.DesktopLauncher \
        --vmargs Xmx1G \
        --classpath "$DST_DESKTOP_JAR" \
        --removelibs "$DST_DESKTOP_JAR" \
        --output "$out_dir" \
        $extra_args
    set +x

    echo "Copying install data"
    # We do not use $out_dir here because on macOS it would put the README
    # *inside* the bundle
    cp $PACKAGING_COMMON_DIR/README.html "$DIST_OUT_BASE_DIR/$dist_name"

    if [ "$platform" = "linux64" ] ; then
        cp -a $PACKAGING_LINUX_DIR/* "$out_dir"
    fi

    echo "Make sure all files are writable"
    # Packr generates some read-only files in the jre dir. This causes trouble
    # when trying to replace them during upgrade.
    chmod -R u+w $out_dir

    echo "Creating zip"
    (
        cd $DIST_OUT_BASE_DIR
        rm -f $dist_name.zip
        zip -qr $dist_name.zip $dist_name
        rm -rf $dist_name
    )
}

if [ "$#" -ne 1 ] ; then
    die "Usage: create-installers <VERSION>"
fi

VERSION="$1"

download_to_cache $PACKR_URL $PACKR_SHA256
PACKR_PATH=$LOCAL_PATH

download_to_cache $JRE_LINUX_URL $JRE_LINUX_SHA256
JRE_LINUX_PATH=$LOCAL_PATH

download_to_cache $JRE_MACOS_URL $JRE_MACOS_SHA256
JRE_MACOS_PATH=$LOCAL_PATH

download_to_cache $JRE_WINDOWS_URL $JRE_WINDOWS_SHA256
JRE_WINDOWS_PATH=$LOCAL_PATH

copy_desktop_jar

create_archive $JRE_LINUX_PATH linux64

create_archive $JRE_MACOS_PATH mac

create_archive $JRE_WINDOWS_PATH windows64
