Blame SOURCES/acpid.power.sh

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