Blame SOURCES/acpid.power.sh

e6bb42
#!/bin/sh
e6bb42
e6bb42
PATH=/sbin:/bin:/usr/bin
e6bb42
e6bb42
# Check session status using systemd
e6bb42
session_ids=$(systemd-loginctl list-sessions 2>/dev/null | awk '{print $1}')
e6bb42
for session in ${session_ids} ; do
e6bb42
	session_status=$(systemd-loginctl session-status ${session})
e6bb42
	echo "${session_status}" | grep -e '\(Active: yes\|State: active\)' &> /dev/null &&
e6bb42
		echo "${session_status}" | grep -e '\(gnome-settings-daemon\|kded4\|xfce4-power-manager\)' &> /dev/null && exit 0
e6bb42
done
e6bb42
e6bb42
# Get the ID of the first active X11 session: using ConsoleKit
e6bb42
uid_session=$(
e6bb42
ck-list-sessions 2>/dev/null | \
e6bb42
awk '
e6bb42
/^Session[0-9]+:$/ { uid = active = x11 = "" ; next }
e6bb42
{ gsub(/'\''/, "", $3) }
e6bb42
$1 == "unix-user" { uid = $3 }
e6bb42
$1 == "active" { active = $3 }
e6bb42
$1 == "x11-display" { x11 = $3 }
e6bb42
active == "TRUE" && x11 != "" {
e6bb42
	print uid
e6bb42
	exit
e6bb42
}')
e6bb42
e6bb42
# Check that there is a power manager, otherwise shut down.
e6bb42
[ "$uid_session" ] &&
e6bb42
ps axo uid,cmd | \
e6bb42
awk '
e6bb42
    $1 == '$uid_session' &&
e6bb42
	($2 ~ /gnome-power-manager/ || $2 ~ /kpowersave/ ||
e6bb42
	 $2 ~ /xfce4-power-manager/ || $2 ~ /\/usr\/libexec\/gnome-settings-daemon/ ||
e6bb42
	 $2 ~ /kded4/ || $3 ~ /guidance-power-manager/) \
e6bb42
		{ found = 1; exit }
e6bb42
    END { exit !found }
e6bb42
' ||
e6bb42
  shutdown -h now
e6bb42