#!/bin/bash
# Mode écran fermé : active/désactive la veille quand l'écran est fermé,
# avec surveillant de batterie (alerte à 15 %, extinction propre à 10 %).
# (c) TB INFORMATIQUE — Thierry Bonsigne — 06-42-29-28-18
RES="$(cd "$(dirname "$0")/../Resources" && pwd)"
ICON="$RES/dialog-icon.png"
WATCHER="$RES/surveilleur-batterie.sh"
PIDFILE="/tmp/mode-ecran-ferme-surveilleur.pid"
FLAG15="/tmp/mode-ecran-ferme-alerte15.flag"

etat_mode() {
  /usr/bin/pmset -g custom 2>/dev/null | /usr/bin/awk '$1 == "disablesleep" {print $2; exit}'
}

demarrer_surveillant() {
  local old
  if [ -f "$PIDFILE" ]; then
    old="$(cat "$PIDFILE" 2>/dev/null)"
    if [ -n "$old" ] && kill -0 "$old" 2>/dev/null; then
      return 0
    fi
  fi
  /usr/bin/nohup /bin/bash "$WATCHER" >/dev/null 2>&1 &
  echo $! > "$PIDFILE"
}

arreter_surveillant() {
  local old
  if [ -f "$PIDFILE" ]; then
    old="$(cat "$PIDFILE" 2>/dev/null)"
    [ -n "$old" ] && kill "$old" 2>/dev/null
    rm -f "$PIDFILE"
  fi
  rm -f "$FLAG15"
}

# Écran d'accueil TB INFORMATIQUE (visible à chaque ouverture)
/usr/bin/osascript -e 'display dialog "© TB INFORMATIQUE" & return & "Thierry Bonsigne" & return & "Tél : 06-42-29-28-18" with title "Mode écran fermé — TB INFORMATIQUE" buttons {"Continuer"} default button "Continuer" with icon note' >/dev/null

# AppleScript écrit dans un fichier temporaire (compatibilité bash 3.2 de macOS)
OSA_FILE="$(mktemp -t modeecranferme)"
cat > "$OSA_FILE" <<'OSA'
on run argv
	set icone to (POSIX file (item 1 of argv))
	set currentState to do shell script "/usr/bin/pmset -g custom | /usr/bin/awk '$1 == \"disablesleep\" {print $2; exit}'"
	if currentState is "1" then
		set statusText to "État actuel : ACTIVÉ"
	else
		set statusText to "État actuel : DÉSACTIVÉ"
	end if
	set dialogResult to display dialog statusText & return & return & "Que veux-tu faire ?" buttons {"Annuler", "Désactiver", "Activer"} default button "Annuler" with title "Mode écran fermé — TB INFORMATIQUE" with icon icone
	return button returned of dialogResult
end run
OSA
choice="$(/usr/bin/osascript "$OSA_FILE" "$ICON")"
rm -f "$OSA_FILE" || true

case "$choice" in
  Activer)
    /usr/bin/osascript -e 'do shell script "/usr/bin/pmset -a disablesleep 1" with administrator privileges' && {
      demarrer_surveillant
      /usr/bin/osascript -e "display dialog \"Mode ACTIVÉ. Le surveillant de batterie veille : alerte à 15 %, extinction propre à 10 % sur batterie. Pense à le désactiver avant de ranger ton Mac dans un sac.\" buttons {\"OK\"} default button \"OK\" with title \"Mode écran fermé — TB INFORMATIQUE\" with icon file (POSIX file \"$ICON\")"
    } ;;
  Désactiver)
    /usr/bin/osascript -e 'do shell script "/usr/bin/pmset -a disablesleep 0" with administrator privileges' && {
      arreter_surveillant
      /usr/bin/osascript -e "display dialog \"Mode DÉSACTIVÉ. La veille normale est rétablie et le surveillant de batterie est arrêté.\" buttons {\"OK\"} default button \"OK\" with title \"Mode écran fermé — TB INFORMATIQUE\" with icon file (POSIX file \"$ICON\")"
    } ;;
  *)
    # Annulé : si le mode est déjà actif, on s'assure que le surveillant tourne.
    if [ "$(etat_mode)" = "1" ]; then
      demarrer_surveillant
    fi ;;
esac
