#!/usr/bin/env bash
# Compatibility-safe cursor synchroniser for Sentinel Linux.
# It reads the selected icon theme and changes only the cursor theme.
# It deliberately never writes org.mate.interface icon-theme.
set -Eeuo pipefail

MAP=${SENTINEL_LINUX_ICON_MAP:-/usr/share/sentinel-linux/icon-variants.tsv}
ICON_ROOT=${SENTINEL_LINUX_ICON_ROOT:-/usr/share/icons}
STATE=/var/lib/sentinel-linux/theme-cursor-integration

die(){ printf 'ERROR: %s\n' "$*" >&2; exit 1; }
strip_quotes(){ local v=$1; v=${v#\'}; v=${v%\'}; printf '%s\n' "$v"; }

current(){
  printf 'icons=%s\n' "$(gsettings get org.mate.interface icon-theme)"
  printf 'cursor=%s\n' "$(gsettings get org.gnome.desktop.interface cursor-theme)"
}

cursor_for_current(){
  local icons cursor
  icons=$(strip_quotes "$(gsettings get org.mate.interface icon-theme)")
  cursor=$(awk -F '\t' -v icons="$icons" 'NR>1 && $2==icons {print $3; exit}' "$MAP")
  case "$icons" in
    SentinelOS-Dark) [[ -n "$cursor" ]] || cursor=SentinelOS-Cursor-Dark-Gold ;;
  esac
  [[ -n "$cursor" ]] || cursor=SentinelOS-Cursor-Light-Cyan
  printf '%s\n' "$cursor"
}

sync_current(){
  [[ ${EUID:-$(id -u)} -ne 0 || -n ${SENTINEL_LINUX_TEST_MODE:-} ]] || die 'run --sync-current as the desktop user, not root'
  local before after cursor
  before=$(gsettings get org.mate.interface icon-theme)
  cursor=$(cursor_for_current)
  [[ -s "$ICON_ROOT/$cursor/index.theme" ]] || die "cursor theme missing: $cursor"
  gsettings set org.gnome.desktop.interface cursor-theme "$cursor"
  after=$(gsettings get org.mate.interface icon-theme)
  [[ "$after" == "$before" ]] || die "icon theme changed unexpectedly: $before -> $after"
  printf 'cursor=%s\n' "$cursor"
  printf 'icon_theme_preserved=%s\n' "$after"
}

verify(){
  [[ -r "$MAP" ]] || die "icon mapping missing: $MAP"
  [[ -x /usr/bin/sentinel-linux-icon-selector || -n ${SENTINEL_LINUX_TEST_MODE:-} ]] || die 'icon selector missing'
  if grep -Eq '^[[:space:]]*gsettings[[:space:]]+set[[:space:]]+org\.mate\.interface[[:space:]]+icon-theme' "$0"; then
    die 'unsafe icon-theme writer detected'
  fi
  printf 'verify: ok\n'
  printf 'icon-theme writes: disabled\n'
}

apply_bindings(){
  [[ ${EUID:-$(id -u)} -eq 0 ]] || die '--apply-bindings requires root'
  [[ "${1:-}" == --confirm && "${2:-}" == APPLY_SENTINELOS_CURSOR_BINDINGS ]] ||
    die 'confirmation required: --confirm APPLY_SENTINELOS_CURSOR_BINDINGS'
  mkdir -p "$STATE"
  printf 'mode=cursor-only\nicon-theme-writes=false\n' > "$STATE/bindings-v3"
  chmod 0644 "$STATE/bindings-v3"
  printf 'bindings: cursor-only\n'
}

case "${1:-}" in
  --sync-current) [[ $# -eq 1 ]] || die 'unexpected arguments'; sync_current ;;
  --current) [[ $# -eq 1 ]] || die 'unexpected arguments'; current ;;
  --verify) [[ $# -eq 1 ]] || die 'unexpected arguments'; verify ;;
  --apply-bindings) shift; apply_bindings "$@" ;;
  -h|--help|'')
    cat <<'USAGE'
Usage:
  sentinelos-theme-cursor-integration --sync-current
  sentinelos-theme-cursor-integration --current
  sentinelos-theme-cursor-integration --verify
  sudo sentinelos-theme-cursor-integration --apply-bindings --confirm APPLY_SENTINELOS_CURSOR_BINDINGS

This compatibility command synchronises only the cursor. It never overwrites a
user-selected icon theme.
USAGE
    ;;
  *) die "unsupported option: $1" ;;
esac
