#!/bin/sh
# install-system-polish.sh — NoSignal polish bundle. Three parts:
#
#   A. Menu cleanup + printing
#      - hides clutter .desktop entries (avahi browsers, V4L test tools,
#        foot terminal trio, pinentry dialogs) via per-user overrides
#      - enables cups.socket so printing works (stack is installed but off)
#   B. Web-app installer: nosignal-webapp-install / nosignal-webapp-remove
#   C. First-login welcome notification pointing at the discoverability keys
#
# Safe to re-run (idempotent). sudo only needed for cups.socket.
set -eu

SRC=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
BIN="$HOME/.local/bin"
APPS="$HOME/.local/share/applications"
HYPRUSER="$HOME/.config/caelestia/hypr-user.conf"

mkdir -p "$BIN" "$APPS"

# --- A1. hide menu clutter (user-level override shadows the system entry) ----
HIDDEN="avahi-discover bssh bvnc qv4l2 qvidcap foot footclient foot-server org.gnupg.pinentry-qt org.gnupg.pinentry-qt5 uuctl"
for id in $HIDDEN; do
  [ -f "/usr/share/applications/$id.desktop" ] || continue
  [ -f "$APPS/$id.desktop" ] && continue
  cat > "$APPS/$id.desktop" <<EOF
[Desktop Entry]
Type=Application
Name=Hidden by NoSignal
NoDisplay=true
Hidden=true
EOF
  echo ":: hidden $id"
done
update-desktop-database "$APPS" 2>/dev/null || true

# --- A2. printing: socket-activated CUPS (zero cost until first print) -------
if command -v systemctl >/dev/null 2>&1 && [ -e /usr/lib/systemd/system/cups.socket ]; then
  if systemctl is-enabled cups.socket >/dev/null 2>&1; then
    echo ":: cups.socket already enabled"
  else
    sudo systemctl enable --now cups.socket && echo ":: cups.socket enabled (printing works now)"
  fi
fi

# --- B. web-app installer -----------------------------------------------------
install -m 0755 "$SRC/nosignal-webapp-install" "$BIN/nosignal-webapp-install"
install -m 0755 "$SRC/nosignal-webapp-remove"  "$BIN/nosignal-webapp-remove"
echo ":: installed nosignal-webapp-install / nosignal-webapp-remove -> $BIN"

# --- C. first-login welcome ----------------------------------------------------
install -m 0755 "$SRC/nosignal-welcome" "$BIN/nosignal-welcome"
if [ -f "$HYPRUSER" ]; then
  if grep -q 'nosignal-welcome' "$HYPRUSER"; then
    echo ":: welcome exec-once already present"
  else
    printf '\n# NoSignal — one-time first-login welcome (stamps ~/.local/state/nosignal/welcomed)\nexec-once = ~/.local/bin/nosignal-welcome\n' >> "$HYPRUSER"
    echo ":: appended welcome exec-once -> $HYPRUSER"
  fi
else
  echo "NOTE: $HYPRUSER not found — add 'exec-once = ~/.local/bin/nosignal-welcome' manually."
fi

echo "Done. Try: nosignal-webapp-install \"YouTube\" https://youtube.com"
