#!/usr/bin/env bash

set -euo pipefail

JRE_ROOT="${HOME}/.cache/velo.port/jre/21"

error_exit() {
    echo "WARNUNG: Die velo.port-Installation scheint beschädigt zu sein. Die JRE kann nicht ermittelt werden." >&2
    exit 1
}

[[ -d "$JRE_ROOT" ]] || error_exit

JRE_PATH="$(find "$JRE_ROOT" -mindepth 1 -maxdepth 1 -type d | sort | head -n 1)"

[[ -n "$JRE_PATH" ]] || error_exit

JAVA_FILE="${JRE_PATH}/Contents/Home/bin/java"

[[ -x "$JAVA_FILE" ]] || error_exit

SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

BIN_FOLDER_NAME="lib"
JAR_NAME="vp-launcher.jar"
ICON_NAME="vplauncher.icns"

SOURCE_BIN_FOLDER="${SOURCE_DIR}/${BIN_FOLDER_NAME}"
SOURCE_JAR_FILE="${SOURCE_DIR}/${JAR_NAME}"
SOURCE_ICON_FILE="${SOURCE_DIR}/${ICON_NAME}"

APP_NAME="velo.port Schnellstart"
APP_BUNDLE="${HOME}/Applications/${APP_NAME}.app"

CONTENTS_DIR="${APP_BUNDLE}/Contents"
MACOS_DIR="${CONTENTS_DIR}/MacOS"
RESOURCES_DIR="${CONTENTS_DIR}/Resources"

DESKTOP_ALIAS="${HOME}/Desktop/${APP_NAME}.app"

mkdir -p "${HOME}/Applications"
mkdir -p "$MACOS_DIR"
mkdir -p "$RESOURCES_DIR"

if [[ -d "$SOURCE_BIN_FOLDER" ]]; then
    # shellcheck disable=SC2115
    rm -rf "${RESOURCES_DIR}/${BIN_FOLDER_NAME}"
    cp -R "$SOURCE_BIN_FOLDER" "${RESOURCES_DIR}/${BIN_FOLDER_NAME}"
else
    echo "WARNUNG: Quellordner nicht gefunden: $SOURCE_BIN_FOLDER"
fi

if [[ -f "$SOURCE_JAR_FILE" ]]; then
    cp "$SOURCE_JAR_FILE" "${RESOURCES_DIR}/${JAR_NAME}"
else
    echo "WARNUNG: Quelldatei nicht gefunden: $SOURCE_JAR_FILE"
fi

if [[ -f "$SOURCE_ICON_FILE" ]]; then
    cp "$SOURCE_ICON_FILE" "${RESOURCES_DIR}/${ICON_NAME}"
else
    echo "WARNUNG: Quelldatei nicht gefunden: $SOURCE_ICON_FILE"
fi

TMP_LAUNCHER="$(mktemp)"

cat > "$TMP_LAUNCHER" <<EOF
#!/bin/bash

JAVA_FILE="${JAVA_FILE}"

APP_DIR="\$(cd "\$(dirname "\$0")/.." && pwd)"
RESOURCES_DIR="\${APP_DIR}/Resources"

exec "\${JAVA_FILE}" \
    -jar "\${RESOURCES_DIR}/vp-launcher.jar"
EOF

cp "$TMP_LAUNCHER" "${MACOS_DIR}/launcher"
chmod +x "${MACOS_DIR}/launcher"

rm "$TMP_LAUNCHER"

TMP_PLIST="$(mktemp)"

cat > "$TMP_PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>

    <key>CFBundleExecutable</key>
    <string>launcher</string>

    <key>CFBundleIdentifier</key>
    <string>de.veloport.vplauncher</string>

    <key>CFBundleName</key>
    <string>velo.port Schnellstart</string>

    <key>CFBundleDisplayName</key>
    <string>velo.port Schnellstart</string>

    <key>CFBundlePackageType</key>
    <string>APPL</string>

    <key>CFBundleVersion</key>
    <string>1.0</string>

    <key>CFBundleShortVersionString</key>
    <string>1.0</string>

    <key>CFBundleIconFile</key>
    <string>vplauncher</string>

</dict>
</plist>
EOF

cp "$TMP_PLIST" "${CONTENTS_DIR}/Info.plist"

rm "$TMP_PLIST"

osascript <<EOF
tell application "Finder"
    set desktopFolder to POSIX file "${HOME}/Desktop"
    set appFile to POSIX file "${APP_BUNDLE}"

    try
        delete file "${APP_NAME}" of desktop
    end try

    try
        delete file "${APP_NAME}.app" of desktop
    end try

    set newAlias to make alias file to appFile at desktopFolder
    set name of newAlias to "${APP_NAME}"
end tell
EOF

echo "Installation abgeschlossen."
