Blame SOURCES/acpid.power.sh

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