#!/usr/bin/env bash
# TrainerDay Desktop installer for Linux
#
# Installs the latest AppImage to ~/Applications and adds TrainerDay to your
# app menu. No sudo required. Re-run anytime to update to the latest version.
#
# Quick install:
#   curl -fsSL https://download.trainerday.com/linux/install.sh | bash
#
# Or download and inspect first:
#   curl -fsSLO https://download.trainerday.com/linux/install.sh
#   bash install.sh

set -euo pipefail

APPIMAGE_URL="https://download.trainerday.com/linux/stable/TrainerDay-latest.AppImage"
INSTALL_DIR="$HOME/Applications"
APPIMAGE_PATH="$INSTALL_DIR/TrainerDay.AppImage"
DESKTOP_DIR="$HOME/.local/share/applications"
DESKTOP_FILE="$DESKTOP_DIR/trainerday.desktop"
ICON_DIR="$HOME/.local/share/icons"
ICON_PATH="$ICON_DIR/trainerday.png"

bold() { printf "\033[1m%s\033[0m\n" "$1"; }
info() { printf "  %s\n" "$1"; }
warn() { printf "\033[33m  %s\033[0m\n" "$1"; }
err()  { printf "\033[31m  %s\033[0m\n" "$1" >&2; }

[[ "$(uname -s)" == "Linux" ]] || { err "This installer is for Linux only."; exit 1; }
command -v curl >/dev/null || { err "curl is required but not installed."; exit 1; }

ARCH="$(uname -m)"
if [[ "$ARCH" != "x86_64" ]]; then
  warn "Detected CPU arch: $ARCH"
  warn "Only x86_64 builds are currently published — the AppImage will not run here."
  exit 1
fi

bold "Installing TrainerDay Desktop..."

mkdir -p "$INSTALL_DIR" "$DESKTOP_DIR" "$ICON_DIR"

info "Downloading latest AppImage to $APPIMAGE_PATH"
curl -fSL --progress-bar "$APPIMAGE_URL" -o "$APPIMAGE_PATH.tmp"
mv "$APPIMAGE_PATH.tmp" "$APPIMAGE_PATH"
chmod +x "$APPIMAGE_PATH"

info "Extracting application icon..."
EXTRACT_DIR="$(mktemp -d)"
trap 'rm -rf "$EXTRACT_DIR"' EXIT
( cd "$EXTRACT_DIR" && "$APPIMAGE_PATH" --appimage-extract 'usr/share/icons/hicolor/*/apps/*.png' >/dev/null 2>&1 || true )
ICON_SRC="$(find "$EXTRACT_DIR/squashfs-root" -type f -name '*.png' 2>/dev/null | sort -t/ -k7 -n -r | head -1)"
if [[ -n "${ICON_SRC:-}" && -f "$ICON_SRC" ]]; then
  cp "$ICON_SRC" "$ICON_PATH"
  ICON_VALUE="$ICON_PATH"
else
  warn "Couldn't extract icon — desktop entry will use a generic icon."
  ICON_VALUE="applications-other"
fi

# AppImage needs libfuse2 to mount itself. Ubuntu 24.04+ doesn't ship it by default.
# If FUSE is missing, fall back to --appimage-extract-and-run, which extracts the
# AppImage to /tmp on first launch (no FUSE needed, ~250MB cached after first run).
EXEC_FLAGS="--no-sandbox"
if ! ldconfig -p 2>/dev/null | grep -q 'libfuse.so.2'; then
  warn "libfuse2 not found — using --appimage-extract-and-run fallback."
  warn "For faster startup, install libfuse2:"
  if command -v apt-get >/dev/null; then
    if apt-cache show libfuse2t64 >/dev/null 2>&1; then
      warn "  sudo apt install libfuse2t64"
    else
      warn "  sudo apt install libfuse2"
    fi
  elif command -v dnf >/dev/null; then
    warn "  sudo dnf install fuse-libs"
  elif command -v pacman >/dev/null; then
    warn "  sudo pacman -S fuse2"
  fi
  EXEC_FLAGS="--appimage-extract-and-run --no-sandbox"
fi

info "Creating desktop entry at $DESKTOP_FILE"
cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Name=TrainerDay
Comment=TrainerDay desktop training app
Exec=$APPIMAGE_PATH $EXEC_FLAGS %U
Icon=$ICON_VALUE
Terminal=false
Type=Application
Categories=Sports;Utility;
StartupWMClass=TrainerDay
EOF

if command -v update-desktop-database >/dev/null; then
  update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
fi

bold ""
bold "Done. Launch TrainerDay from your app menu, or run:"
info "  $APPIMAGE_PATH $EXEC_FLAGS"
bold ""
info "Re-run this installer anytime to update to the latest version."
info "If you use an ANT+ USB stick, see https://download.trainerday.com/linux/ for setup."
