summaryrefslogtreecommitdiff
path: root/bin/extra/saveself
blob: 8906a5fba1b4192f36d87106c5bdfa6c974cbaee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/sh

Threshold=5

GetBatteryCapacity()
{
	File="$(find /sys/class/power_supply \
		-maxdepth 1 \
		-type l \
		-name 'BAT*' |
		head -n 1)"
	cat "$File"/capacity
}

PrevCapacity="$(GetBatteryCapacity)"
[ "$PrevCapacity" ] || exit 1

while true
do
	sleep 5
	Capacity="$(GetBatteryCapacity)"
	if [ "$Capacity" -lt "$PrevCapacity" ] 
	then
		if [ "$Capacity" -le "$Threshold" ]
		then
			setsid slock
			HostName="$(hostname)"
			if [ "$HostName" = "spring" ]
			then
				doas /usr/sbin/zzz -Z
			elif [ "$HostName" = "winter" ] 
			then
				systemctl hibernate
			fi
		fi
		PrevCapacity="$Capacity"
	fi
done