#!/bin/sh
# Eye Saver – Linux installer & updater
# Usage: curl -fsSL https://eyesaver.webdevnc.com/install.sh | sh
set -e

INSTALL_DIR="$HOME/.local/share/EyeSaver"
DESKTOP_DIR="$HOME/.local/share/applications"
ICON_DIR="$HOME/.local/share/icons/hicolor/512x512/apps"
BIN_LINK="$HOME/.local/bin/eye_saver"
SYSTEMD_USER_DIR="$HOME/.config/systemd/user"
TAR_URL="https://eyesaver.webdevnc.com/EyeSaverDesktop_linux.tar.gz"
VERSION_URL="https://eyesaver.webdevnc.com/linux_version.txt"
TMP_ARCHIVE="/tmp/eye_saver_install_$$.tar.gz"
INSTALL_SH_URL="https://eyesaver.webdevnc.com/install.sh"

# ── helpers ────────────────────────────────────────────────────────────────
info()  { printf '\033[1;34m=> %s\033[0m\n' "$*"; }
ok()    { printf '\033[1;32m✔  %s\033[0m\n' "$*"; }
warn()  { printf '\033[1;33m⚠  %s\033[0m\n' "$*"; }

# ── check for curl ─────────────────────────────────────────────────────────
if ! command -v curl >/dev/null 2>&1; then
  warn "curl is required but not found. Install it with your package manager."
  exit 1
fi

# ── check latest version vs installed ─────────────────────────────────────
LATEST=$(curl -fsSL "$VERSION_URL" 2>/dev/null | tr -d '[:space:]') || true
INSTALLED=""
if [ -f "$INSTALL_DIR/.eye_saver_version" ]; then
  INSTALLED=$(cat "$INSTALL_DIR/.eye_saver_version" | tr -d '[:space:]')
fi

if [ -n "$INSTALLED" ] && [ "$INSTALLED" = "$LATEST" ]; then
  ok "Eye Saver $INSTALLED is already up to date."
  exit 0
fi

if [ -n "$INSTALLED" ] && [ -n "$LATEST" ]; then
  info "Updating Eye Saver $INSTALLED → $LATEST …"
else
  info "Installing Eye Saver $LATEST …"
fi

# ── download ───────────────────────────────────────────────────────────────
info "Downloading…"
curl -fsSL --progress-bar "$TAR_URL" -o "$TMP_ARCHIVE"

# ── extract ────────────────────────────────────────────────────────────────
info "Installing to $INSTALL_DIR …"
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
tar -xzf "$TMP_ARCHIVE" -C "$INSTALL_DIR"
rm -f "$TMP_ARCHIVE"

# Stamp the version file so the in-app updater can compare.
if [ -n "$LATEST" ]; then
  printf '%s' "$LATEST" > "$INSTALL_DIR/.eye_saver_version"
fi

# ── desktop integration ────────────────────────────────────────────────────
mkdir -p "$DESKTOP_DIR" "$ICON_DIR"

ICON_SRC="$INSTALL_DIR/share/icons/hicolor/512x512/apps/com.webdevnc.eye_saver_desktop.png"
if [ -f "$ICON_SRC" ]; then
  cp "$ICON_SRC" "$ICON_DIR/com.webdevnc.eye_saver_desktop.png"
fi

cat > "$DESKTOP_DIR/com.webdevnc.eye_saver_desktop.desktop" <<DESKTOP
[Desktop Entry]
Version=1.0
Name=Eye Saver
Comment=A screen break timer
Exec=$INSTALL_DIR/eye_saver_desktop
Icon=com.webdevnc.eye_saver_desktop
Terminal=false
Type=Application
Categories=Utility;
StartupNotify=false
DESKTOP

command -v update-desktop-database >/dev/null 2>&1 && \
  update-desktop-database "$DESKTOP_DIR" 2>/dev/null || true
command -v gtk-update-icon-cache   >/dev/null 2>&1 && \
  gtk-update-icon-cache -f -t "$HOME/.local/share/icons/hicolor" 2>/dev/null || true

# ── optional ~/.local/bin symlink ──────────────────────────────────────────
mkdir -p "$HOME/.local/bin"
ln -sf "$INSTALL_DIR/eye_saver_desktop" "$BIN_LINK"

# ── systemd user timer for automatic updates ───────────────────────────────
if command -v systemctl >/dev/null 2>&1 && systemctl --user status >/dev/null 2>&1; then
  info "Setting up automatic update timer (systemd user)…"

  mkdir -p "$SYSTEMD_USER_DIR"

  # Service: runs install.sh silently.
  cat > "$SYSTEMD_USER_DIR/eye-saver-update.service" <<SERVICE
[Unit]
Description=Eye Saver auto-updater

[Service]
Type=oneshot
ExecStart=/bin/sh -c 'curl -fsSL $INSTALL_SH_URL | sh'
StandardOutput=journal
StandardError=journal
SERVICE

  # Timer: runs daily.
  cat > "$SYSTEMD_USER_DIR/eye-saver-update.timer" <<TIMER
[Unit]
Description=Eye Saver daily update check

[Timer]
OnBootSec=2min
OnUnitActiveSec=24h
Persistent=true

[Install]
WantedBy=timers.target
TIMER

  systemctl --user daemon-reload
  systemctl --user enable --now eye-saver-update.timer 2>/dev/null && \
    ok "Auto-update timer enabled (runs daily)." || \
    warn "Could not enable systemd timer – you can update manually by re-running this script."
else
  warn "systemd user services not available – automatic updates not configured."
  warn "Re-run this script at any time to update Eye Saver."
fi

# ── done ───────────────────────────────────────────────────────────────────
ok "Eye Saver installed! Launch it from your application menu, or run: eye_saver"
