#!/bin/sh
# Pilote le shell AuxiNux par D-Bus (bus de session). Utilisé par les raccourcis clavier de labwc.
usage() {
  cat <<EOF
Usage : auxinux-shellctl COMMANDE
  start-menu       ouvre ou ferme le menu Démarrer (touche Super)
  search           ouvre la recherche (Super+S)
  show-desktop     affiche le bureau / restaure les fenêtres (Super+D)
  files            ouvre le gestionnaire de fichiers (Super+E)
  terminal         ouvre un terminal (Ctrl+Alt+T)
  quick-settings   ouvre ou ferme les paramètres rapides (Super+A)
  notifications    ouvre ou ferme les notifications et le calendrier (Super+N)
  volume-up | volume-down | volume-mute
  brightness-up | brightness-down
  lock             verrouille la session (Super+L)
  screenshot       capture tout l'écran (Impr. écran)
  screenshot-area  capture une zone choisie à la souris (Super+Maj+S)
  settings [PAGE]  ouvre les Paramètres AuxiNux (Super+I)
  reload           relit la configuration du shell
  version          affiche la version du shell en cours
EOF
}
bus="${AUXINUX_APP_ID:-ca.auxinux.Shell}"
fallback=""
case ${1:-} in
  start-menu) method=ToggleStartMenu ;;
  search) method=OpenSearch ;;
  show-desktop) method=ShowDesktop ;;
  files) method=OpenFiles ;;
  terminal) method=OpenTerminal ;;
  quick-settings) method=ToggleQuickSettings ;;
  notifications) method=ToggleNotifications ;;
  # Si le shell ne répond pas, les touches multimédias et le verrouillage fonctionnent quand même.
  volume-up) method=VolumeUp; fallback="wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 5%+" ;;
  volume-down) method=VolumeDown; fallback="wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" ;;
  volume-mute) method=VolumeMute; fallback="wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle" ;;
  brightness-up) method=BrightnessUp ;;
  brightness-down) method=BrightnessDown ;;
  lock) method=Lock; fallback="loginctl lock-session" ;;
  screenshot) method=Screenshot ;;
  screenshot-area) method=ScreenshotArea ;;
  restart) method=Restart ;;
  settings)
    exec busctl --user call --timeout=2 "$bus" /ca/auxinux/Shell ca.auxinux.Shell1 OpenSettings s "${2:-}" ;;
  reload) method=Reload ;;
  version)
    exec busctl --user get-property "$bus" /ca/auxinux/Shell ca.auxinux.Shell1 Version ;;
  -h|--help) usage; exit 0 ;;
  *) usage >&2; exit 2 ;;
esac
if [ -n "$fallback" ]; then
  busctl --user call --timeout=2 "$bus" /ca/auxinux/Shell ca.auxinux.Shell1 "$method" 2>/dev/null || exec $fallback
else
  exec busctl --user call --timeout=2 "$bus" /ca/auxinux/Shell ca.auxinux.Shell1 "$method"
fi
