#!/bin/sh
# desktop-shutdown — cross-DE shutdown

# Prefer desktop-session environment variables
DESKTOP="${XDG_CURRENT_DESKTOP:-$DESKTOP_SESSION:-}"

# Normalize to lowercase
DESKTOP_LOWER="$(printf '%s' "$DESKTOP" | tr '[:upper:]' '[:lower:]')"

# Commands for DEs
case "$DESKTOP_LOWER" in
  *gnome*|*ubuntu*)
    # GNOME / Ubuntu (gnome-session-quit may be available)
    if command -v gnome-session-quit >/dev/null 2>&1; then
      exec gnome-session-quit --power-off --force
    fi
    # fallback to systemctl
    exec systemctl poweroff
    ;;
  *xfce*|*xubuntu*)
    if command -v xfce4-session-logout >/dev/null 2>&1; then
      exec xfce4-session-logout --halt --fast
    fi
    exec systemctl poweroff
    ;;
  *budgie*|*ubuntu-budgie*)
    if command -v gnome-session-quit >/dev/null 2>&1; then
      exec gnome-session-quit --power-off --force
    fi
    exec systemctl poweroff
    ;;
  *)
    # Generic fallback: try gnome-session-quit, xfce4-session-logout, then systemctl
    if command -v gnome-session-quit >/dev/null 2>&1; then
      exec gnome-session-quit --power-off --force
    fi
    if command -v xfce4-session-logout >/dev/null 2>&1; then
      exec xfce4-session-logout --halt --fast
    fi
    exec systemctl poweroff
    ;;
esac
