#!/usr/bin/env bash
set -euo pipefail

VERSION_URL="https://gitssy.nakildias.com/Nakildias/HarmonyApp/raw/master/version.txt"
INSTALL_DIR="${HOME}/Applications"
DESKTOP_DIR="${HOME}/.local/share/applications"
ICON_DIR="${HOME}/.local/share/icons/hicolor/256x256/apps"

echo "=== Harmony Desktop — Linux Installer ==="
echo ""

# ── 1. Install dependencies ──
echo "[1/5] Checking dependencies..."
install_pkg() {
  if command -v apt-get &>/dev/null; then
    sudo apt-get update -qq && sudo apt-get install -y -qq "$@"
  elif command -v dnf &>/dev/null; then
    sudo dnf install -y -q "$@"
  elif command -v pacman &>/dev/null; then
    sudo pacman -S --noconfirm --needed "$@"
  else
    echo "ERROR: Unsupported package manager. Install manually: $*"
    exit 1
  fi
}

MISSING=""
command -v wget &>/dev/null || MISSING="$MISSING wget"
command -v curl &>/dev/null || MISSING="$MISSING curl"
command -v fuse2-libs &>/dev/null || command -v fusermount &>/dev/null || {
  # AppImage needs FUSE
  if command -v apt-get &>/dev/null; then
    MISSING="$MISSING libfuse2"
  elif command -v dnf &>/dev/null; then
    MISSING="$MISSING fuse-libs"
  elif command -v pacman &>/dev/null; then
    MISSING="$MISSING fuse2"
  fi
}

if [ -n "$MISSING" ]; then
  echo "       Installing: $MISSING"
  install_pkg $MISSING
else
  echo "       All dependencies present."
fi

# ── 2. Fetch version.txt ──
echo "[2/5] Checking latest version..."
VERSION_TXT=$(curl -sL "$VERSION_URL")
VERSION=$(echo "$VERSION_TXT" | sed -n 's/^VERSION=(\(.*\))/\1/p' | head -1)
DOWNLOAD_URL=$(echo "$VERSION_TXT" | sed -n 's/^LINUX=(\(.*\))/\1/p' | head -1)

if [ -z "$VERSION" ]; then
  echo "ERROR: Could not determine latest version."
  echo "       Check your network and that $VERSION_URL is reachable."
  exit 1
fi
echo "       Latest version: $VERSION"

if [ -z "$DOWNLOAD_URL" ]; then
  echo "ERROR: No Linux download URL found in version.txt."
  exit 1
fi

# ── 4. Download ──
mkdir -p "$INSTALL_DIR"
DEST="$INSTALL_DIR/Harmony.AppImage"
echo "[3/5] Downloading Harmony $VERSION..."
wget -q --show-progress -O "$DEST" "$DOWNLOAD_URL"
chmod +x "$DEST"
echo "       Saved to $DEST"

# ── 5. Create desktop entry + icon ──
echo "[4/5] Creating desktop entry..."
mkdir -p "$DESKTOP_DIR" "$ICON_DIR"

# Extract icon from AppImage
"$DEST" --appimage-extract "*.png" 2>/dev/null || true
EXTRACTED_ICON=$(find squashfs-root -name "icon.png" -o -name "harmony.png" 2>/dev/null | head -1)
if [ -n "$EXTRACTED_ICON" ] && [ -f "$EXTRACTED_ICON" ]; then
  cp "$EXTRACTED_ICON" "$ICON_DIR/harmony.png"
else
  # Fallback: download from repo
  wget -q -O "$ICON_DIR/harmony.png" "https://gitssy.nakildias.com/Nakildias/HarmonyApp/raw/master/assets/icon.png" 2>/dev/null || true
fi
rm -rf squashfs-root 2>/dev/null || true

ICON_PATH="$ICON_DIR/harmony.png"
cat > "$DESKTOP_DIR/harmony.desktop" << DESKTOP
[Desktop Entry]
Name=Harmony
Comment=Harmony Desktop Client
Exec=$DEST
Icon=$ICON_PATH
Type=Application
Categories=Network;Chat;InstantMessaging;
StartupWMClass=Harmony
DESKTOP

update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true

echo "[5/5] Done!"
echo ""
echo "Harmony $VERSION installed successfully."
echo "Launch from your application menu or run:"
echo "  $DEST"
echo ""
