#!/usr/bin/env bash
#
# Installs Nuages on Linux: copies the app files into place, installs an icon,
# creates a .desktop launcher (menu "shortcut"), and registers it as the handler
# for the nuages:// URL scheme used by the SoundCloud login callback.
#
# NOTE: .NET MAUI does not currently produce a Linux desktop build, so there is
# no Nuages Linux binary yet. This script is ready for one: point it at a folder
# containing an executable named "Nuages" (override with --exe) and it does the
# rest. It also works for any self-contained Linux publish placed in that folder.
#
# Usage:
#   ./install-linux.sh                 # per-user install (no root)
#   ./install-linux.sh --system        # /opt + system-wide launcher (uses sudo)
#   ./install-linux.sh --source DIR --exe Nuages
#
set -euo pipefail

APP_NAME="Nuages"
EXE_NAME="Nuages"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE="$SCRIPT_DIR"
MODE="user"

while [[ $# -gt 0 ]]; do
    case "$1" in
        --system) MODE="system"; shift ;;
        --source) SOURCE="$2"; shift 2 ;;
        --exe)    EXE_NAME="$2"; shift 2 ;;
        *) echo "Unknown option: $1" >&2; exit 1 ;;
    esac
done
SOURCE="$(cd "$SOURCE" && pwd)"

if [[ ! -f "$SOURCE/$EXE_NAME" ]]; then
    echo "Could not find executable '$EXE_NAME' in '$SOURCE'." >&2
    echo "Pass --source <folder> and/or --exe <name>." >&2
    exit 1
fi

# Resolve install locations for the chosen mode.
if [[ "$MODE" == "system" ]]; then
    SUDO="sudo"; [[ "$(id -u)" -eq 0 ]] && SUDO=""
    INSTALL_DIR="/opt/nuages"
    DESKTOP_DIR="/usr/share/applications"
    ICON_DIR="/usr/share/icons/hicolor/512x512/apps"
    BIN_LINK="/usr/local/bin/nuages"
else
    SUDO=""
    INSTALL_DIR="$HOME/.local/share/nuages"
    DESKTOP_DIR="$HOME/.local/share/applications"
    ICON_DIR="$HOME/.local/share/icons/hicolor/512x512/apps"
    BIN_LINK="$HOME/.local/bin/nuages"
fi

echo "Installing $APP_NAME ($MODE)"
echo "  from : $SOURCE"
echo "  into : $INSTALL_DIR"

# 1. Copy app files.
$SUDO mkdir -p "$INSTALL_DIR"
$SUDO cp -R "$SOURCE/." "$INSTALL_DIR/"
$SUDO chmod +x "$INSTALL_DIR/$EXE_NAME"
TARGET_EXE="$INSTALL_DIR/$EXE_NAME"

# 2. Symlink on PATH (so `nuages` works from a terminal too).
$SUDO mkdir -p "$(dirname "$BIN_LINK")"
$SUDO ln -sf "$TARGET_EXE" "$BIN_LINK"

# 3. Icon. Prefer an appicon.png shipped alongside the app; fall back gracefully.
$SUDO mkdir -p "$ICON_DIR"
ICON_SRC=""
for c in "$SOURCE/appicon.png" "$SOURCE/Resources/AppIcon/appicon.png" "$SCRIPT_DIR/appicon.png"; do
    [[ -f "$c" ]] && { ICON_SRC="$c"; break; }
done
ICON_NAME="applications-multimedia"   # generic themed fallback
if [[ -n "$ICON_SRC" ]]; then
    $SUDO cp "$ICON_SRC" "$ICON_DIR/nuages.png"
    ICON_NAME="nuages"
    echo "  + icon installed"
fi

# 4. .desktop launcher, declaring the nuages:// scheme handler.
DESKTOP_FILE="$DESKTOP_DIR/nuages.desktop"
$SUDO mkdir -p "$DESKTOP_DIR"
$SUDO tee "$DESKTOP_FILE" >/dev/null <<EOF
[Desktop Entry]
Type=Application
Name=Nuages
GenericName=Music Player
Comment=A native SoundCloud client
Exec=$TARGET_EXE %u
Icon=$ICON_NAME
Terminal=false
Categories=AudioVideo;Audio;Player;Network;
MimeType=x-scheme-handler/nuages;
StartupNotify=true
StartupWMClass=Nuages
EOF
$SUDO chmod 644 "$DESKTOP_FILE"
echo "  + desktop launcher created"

# 5. Register handlers / refresh caches (best effort — tools vary by distro).
if command -v update-desktop-database >/dev/null 2>&1; then
    $SUDO update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
fi
if command -v gtk-update-icon-cache >/dev/null 2>&1 && [[ "$ICON_NAME" == "nuages" ]]; then
    $SUDO gtk-update-icon-cache -f -t "$(dirname "$(dirname "$(dirname "$ICON_DIR")")")" 2>/dev/null || true
fi
if [[ "$MODE" == "user" ]] && command -v xdg-mime >/dev/null 2>&1; then
    xdg-mime default nuages.desktop x-scheme-handler/nuages 2>/dev/null || true
    echo "  + nuages:// scheme handler registered"
fi

echo ""
echo "Done. Find Nuages in your application menu, or run 'nuages' from a terminal."
[[ "$MODE" == "user" && ":$PATH:" != *":$(dirname "$BIN_LINK"):"* ]] && \
    echo "Note: $(dirname "$BIN_LINK") is not on your PATH; add it to use the 'nuages' command."
