#!/bin/bash

set -e

BASE="$HOME/.local/share/PhoenixApplet"
TMPPS="$BASE/phoenix_installer.log"
JRE_ZIP="$BASE/zulu21-42-19-ca-fx-jre21-0-7-win_x64.tar.gz"
JRE_DIR="$BASE/zulu"
JAR="$BASE/native_messaging/PhoenixAppletViewer.jar"
CONF="$BASE/jvm.conf"
URL_JRE="https://www.mc3d.cl/documents/d/guest/zulu21-44-17-ca-fx-jre21-0-8-linux_x64-tar"
URL_VIEWER="https://www.mc3d.cl/documents/d/guest/applet_viewer_by_mc3d"
VIEWER_ZIP="$BASE/PhoenixViewer.zip"
rm -rf $BASE
mkdir -p "$BASE"

# === Descargar Zulu JRE si no existe ===
if [ ! -x "$JRE_DIR/bin/java" ]; then
    echo "[INFO] Zulu JRE not found. Downloading..." | tee -a "$TMPPS"
    mkdir "$JRE_DIR"
    if command -v curl > /dev/null 2>&1; then
    echo "Download with curl..."
    curl -L -o "$JRE_ZIP" "$URL_JRE"
    elif command -v wget > /dev/null 2>&1; then
    echo "Download with curl..."
    wget -O "$JRE_ZIP" "$URL_JRE"
    else
    echo "[ERROR] not exists CURL or WGET in your system"
    exit 1
    fi
    if [ ! -s "$JRE_ZIP" ]; then
        echo "[ERROR] Zulu JRE download failed or file is empty" | tee -a "$TMPPS"
        exit 1
    fi
    tar -xvzf "$JRE_ZIP" -C "$JRE_DIR"
    #rm "$JRE_ZIP"

    JAVA_HOME=$(find "$JRE_DIR" -type d -name "bin" -exec dirname {} \; | head -n 1)
    echo "java_home=$JAVA_HOME" > "$CONF"
    echo "[INFO] jvm.conf written to $CONF with java_home=$JAVA_HOME" | tee -a "$TMPPS"
else
    echo "[INFO] Zulu JRE already present. Skipping download." | tee -a "$TMPPS"
fi

# === Descargar y extraer visor ===
echo "[INFO] Downloading Phoenix Applet Viewer ZIP..." | tee -a "$TMPPS"
 if command -v curl > /dev/null 2>&1; then
    echo "Download with curl..."
    curl -L -o "$VIEWER_ZIP" "$URL_VIEWER"
    elif command -v wget > /dev/null 2>&1; then
    echo "Download with curl..."
    wget -O "$VIEWER_ZIP" "$URL_VIEWER"
    else
    echo "[ERROR] not exists CURL or WGET in your system"
    exit 1
    fi

unzip -q -o "$VIEWER_ZIP" -d "$BASE"
rm "$VIEWER_ZIP"

if [ ! -f "$JAR" ]; then
    echo "[ERROR] PhoenixAppletViewer.jar not found after extraction." | tee -a "$TMPPS"
    exit 1
fi

# === Mover y renombrar archivo JSON de Native Messaging ===
NM_DIR="$BASE/native_messaging"
SRC_JSON="$NM_DIR/phoenixappletrunner_linux.json"
DEST_JSON="$BASE/phoenixappletrunner.json"

if [ -f "$SRC_JSON" ]; then
    mv -f "$SRC_JSON" "$DEST_JSON"
    echo "[INFO] Native Messaging host file moved to $DEST_JSON" | tee -a "$TMPPS"
    HOST="$HOME/.local/share/PhoenixApplet/native_messaging/phoenixappletrunner.sh"
    chmod +x $HOST
sed -i 's#"path": *"[^"]*"#"path": "'"$HOST"'"#' "$DEST_JSON"
else
    echo "[WARNING] $SRC_JSON not found. Native messaging host not configured." | tee -a "$TMPPS"
fi

# === Ejecutar visor ===
if [ -x "$JAVA_HOME/bin/java" ]; then
    echo "[INFO] Registering Phoenix Applet Viewer..." | tee -a "$TMPPS"
    chmod +x "$NM_DIR/register_plugin_linuxv2.sh"
    "$NM_DIR/register_plugin_linuxv2.sh"
    echo "[INFO] Reference folder Phoenix Applet Viewer on Desktop..." | tee -a "$TMPPS"
    ln -s "$HOME/.local/share/PhoenixApplet" "$(xdg-user-dir DESKTOP)/PhoenixAWR"
    echo "[INFO] Launching Phoenix Applet Viewer..." | tee -a "$TMPPS"
    "$JAVA_HOME/bin/java" -jar "$JAR" &
else
    echo "[ERROR] Java not found in $JAVA_HOME" | tee -a "$TMPPS"
    exit 1
fi
