#!/usr/bin/env bash
# Interactive Desk — one-time installation script
# Run manually after Ubuntu Server 24.04 LTS minimal install.
# Bootstrap internet: Android USB tethering or USB WiFi dongle
set -euo pipefail

DESK_USER="${SUDO_USER:-$USER}"
BASE_URL="${BASE_URL:-https://j2mef.zereb.ru/desktop-kiosk/}"

if [ "$DESK_USER" != "desk" ]; then
    echo "WARNING: User is '$DESK_USER', not 'desk'."
    echo "The niri config hardcodes paths for /home/desk/jar/ and /home/desk."
    echo "Either symlink /home/desk -> /home/$DESK_USER or edit config.kdl after install."
    echo ""
fi

echo "=== Interactive Desk Setup ==="
echo "User: $DESK_USER"

# --- Pre-flight: verify all remote files exist ---
echo ""
echo ">>> Checking all remote files are available..."
REMOTE_FILES=(
  "config.kdl"
  "desk-display.jar" "desk-map.jar" "desk-launcher.jar" "desk-voice.jar"
  "desk-voice.service"
  "docker-compose.yml"
)
MISSING=0
for f in "${REMOTE_FILES[@]}"; do
  if ! curl --head --silent --fail "$BASE_URL/$f" > /dev/null 2>&1; then
    echo "  MISSING: $BASE_URL/$f"
    MISSING=1
  else
    echo "  OK: $f"
  fi
done
if [ "$MISSING" -eq 1 ]; then
  echo "ERROR: One or more required files are missing on the server. Aborting."
  exit 1
fi

# --- System packages ---
echo ""
echo ">>> Installing system packages..."
sudo apt update
sudo apt install -y \
    docker.io docker-compose-v2 \
    pipewire-pulse wireplumber \
    vulkan-tools mesa-vulkan-drivers \
    openjdk-21-jre-headless \
    curl git wget \
    gcc clang libudev-dev libgbm-dev libxkbcommon-dev libegl1-mesa-dev \
    libwayland-dev libinput-dev libdbus-1-dev libsystemd-dev \
    libseat-dev libpipewire-0.3-dev libpango1.0-dev libdisplay-info-dev

# --- Docker group ---
echo ""
echo ">>> Adding user to docker group..."
sudo usermod -aG docker "$DESK_USER"

# --- Autologin on tty1 ---
echo ""
echo ">>> Configuring autologin on tty1..."
sudo mkdir -p /etc/systemd/system/getty@tty1.service.d
sudo tee /etc/systemd/system/getty@tty1.service.d/override.conf > /dev/null <<EOF
[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin $DESK_USER --noclear %I \$TERM
EOF

# --- Rust toolchain (for building niri) ---
echo ""
echo ">>> Installing Rust toolchain..."
sudo -u "$DESK_USER" bash -c "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y" 2>&1

# --- Build and install niri from source ---
if command -v niri &>/dev/null; then
  echo ""
  echo ">>> niri already installed ($(niri --version)), skipping build."
else
  echo ""
  echo ">>> Building niri from source (takes ~2-3 min)..."
  sudo -u "$DESK_USER" bash -c '
  export PATH="$HOME/.cargo/bin:$PATH"
  cd /tmp
  git clone --depth 1 --branch v26.04 https://github.com/niri-wm/niri.git
  cd niri
  cargo build --release
  ' 2>&1

  echo ">>> Installing niri binaries..."
  sudo cp /tmp/niri/target/release/niri /usr/local/bin/niri
  sudo cp /tmp/niri/resources/niri-session /usr/local/bin/niri-session
  sudo mkdir -p /usr/local/share/wayland-sessions
  sudo cp /tmp/niri/resources/niri.desktop /usr/local/share/wayland-sessions/niri.desktop
  sudo mkdir -p /etc/systemd/user /usr/local/share/xdg-desktop-portal
  sudo cp /tmp/niri/resources/niri.service /etc/systemd/user/niri.service
  sudo cp /tmp/niri/resources/niri-shutdown.target /etc/systemd/user/niri-shutdown.target
  sudo cp /tmp/niri/resources/niri-portals.conf /usr/local/share/xdg-desktop-portal/niri-portals.conf
  rm -rf /tmp/niri
fi

# --- Niri config ---
echo ""
echo ">>> Downloading Niri config..."
mkdir -p "$HOME/.config/niri"
curl -L -o "$HOME/.config/niri/config.kdl" "$BASE_URL/config.kdl"
echo "  IMPORTANT: Edit ~/.config/niri/config.kdl to set your display output name."
echo "  Run 'wlr-randr' after first boot to find it."

# --- Auto-launch niri from bashrc (runs on tty1 login, before systemd user services) ---
echo ""
echo ">>> Adding niri auto-launch to .bashrc..."
grep -q 'niri-session' "$HOME/.bashrc" 2>/dev/null || cat >> "$HOME/.bashrc" << 'BASHRC'

# Auto-launch niri Wayland compositor on tty1
if [ "$(tty)" = "/dev/tty1" ] && [ -z "$WAYLAND_DISPLAY" ] && [ -z "$DISPLAY" ]; then
    exec niri-session
fi
BASHRC

# --- Download fatJars and app service files ---
echo ""
echo ">>> Downloading fatJars and app services from $BASE_URL..."
FATJAR_DIR="$HOME/jar"
SYSTEMD_DIR="$HOME/.config/systemd/user"
mkdir -p "$FATJAR_DIR"
mkdir -p "$SYSTEMD_DIR"

for f in desk-display.jar desk-map.jar desk-launcher.jar desk-voice.jar; do
  echo "  Downloading $f..."
  curl -L -o "$FATJAR_DIR/$f" "$BASE_URL/$f"
done

echo "  Downloading desk-voice.service..."
curl -L -o "$SYSTEMD_DIR/desk-voice.service" "$BASE_URL/desk-voice.service"

# Download wrapper scripts for niri-spawned apps
for f in start-desk-display.sh start-desk-map.sh start-desk-launcher.sh; do
  echo "  Downloading $f..."
  curl -L -o "$FATJAR_DIR/$f" "$BASE_URL/$f"
done
chmod +x "$FATJAR_DIR"/start-desk-*.sh

# --- Download docker-compose.yml ---
echo ""
echo ">>> Downloading docker-compose.yml..."
curl -L -o "$HOME/docker-compose.yml" "$BASE_URL/docker-compose.yml"

# --- Download LLM models ---
echo ""
echo ">>> Downloading LLM models..."
MODELS_DIR="$HOME/models"
mkdir -p "$MODELS_DIR"

for url in \
  "https://huggingface.co/unsloth/Qwen3.5-4B-GGUF/resolve/main/Qwen3.5-4B-Q6_K.gguf?download=true" \
  "https://huggingface.co/unsloth/Qwen3.5-4B-GGUF/resolve/main/Qwen3.5-4B-Q8_0.gguf?download=true"; do
  filename=$(basename "$url" | sed 's/\?download=true//')
  if [ -f "$MODELS_DIR/$filename" ]; then
    echo "  $filename already exists, skipping."
  else
    echo "  Downloading $filename..."
    wget -O "$MODELS_DIR/$filename" "$url"
  fi
done

# --- Enable systemd user services ---
echo ""
echo ">>> Enabling systemd user services..."
sudo loginctl enable-linger "$DESK_USER"
DESK_UID=$(id -u "$DESK_USER")
sudo -u "$DESK_USER" XDG_RUNTIME_DIR="/run/user/$DESK_UID" systemctl --user daemon-reload
sudo -u "$DESK_USER" XDG_RUNTIME_DIR="/run/user/$DESK_UID" systemctl --user enable desk-voice.service

# --- Start Docker containers ---
echo ""
echo ">>> Starting Docker containers..."
sg docker -c "docker compose -f '$HOME/docker-compose.yml' up -d"

echo ""
echo "=== Installation complete ==="
echo ""
echo "Post-install steps:"
echo "  1. After reboot, edit ~/.config/niri/config.kdl — set output name from 'wlr-randr'"
echo "  2. Run 'sudo loginctl terminate-user $USER' at another tty to force niri to start (or reboot)"
echo "  3. The system will auto-login on tty1 → bashrc launches niri-session → niri spawns the apps."
echo ""
