summaryrefslogtreecommitdiffstats
path: root/core/modules/idleaction/data/opt/openslx/scripts/systemd-idleaction_init
blob: 2d4507c6e2dcb86cbcbbcb1601d05ce1243f2d70 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/bin/ash

# Called to calculate the next wakeup of this machine. Should be called whenever
# it might make sense to update this, e.g. right after boot, when entering/leaving
# standby, and when shutting down.

if [ "$1" = "test" ]; then
	shift
	TEST=1
	SCHEDULE="$@"
else
	SCHEDULE=
	TEST=
	. /opt/openslx/config
fi

# Figure out the next rtcwake
SCHEDULE="$SCHEDULE $SLX_WAKEUP_SCHEDULE"
if [ "$1" = "suspend" ]; then
	# If we're about to suspend, consider reboot and shutdown too - we
	# need to wake up to execute these
	SCHEDULE="$SCHEDULE $SLX_SHUTDOWN_SCHEDULE $SLX_REBOOT_SCHEDULE"
fi # suspend == true

SCHEDULE=$( echo ${SCHEDULE} ) # poor man's trim

NEXT=
if [ -n "$SCHEDULE" ]; then
	# Time-math in ash - yay
	TODAY=$(date +%Y-%m-%d)
	# Use tomorrow instead of adding 86400 seconds so it (hopefully) works when DST changes
	TOMORROW=$(date --date="+1 day" +%Y-%m-%d)
	NOW=$(date +%s)
	for t in $SCHEDULE; do
		HOUR=${t%%:*}
		MINUTE=${t##*:}
		[ -z "$HOUR" -o -z "$MINUTE" ] && continue
		[ "$HOUR" -lt 0 -o "$HOUR" -gt 23 ] && continue
		[ "$MINUTE" -lt 0 -o "$MINUTE" -gt 59 ] && continue
		# wake up 2 minutes early; for the shutdown/reboot times this is required
		# so the actual cronjob for the shutdown/reboot will run.
		# Otherwise, since the wakeup will take some seconds, the actual cronjob
		# will not trigger, since it lies a few seconds in the past.
		# For the scheduled wakeups this would't be needed, but two minutes
		# early shouldn't hurt anyone.
		TS=$(date --date "-2 minutes $TODAY $t" +%s)
		[ "$TS" -le "$NOW" ] && TS=$(date --date "-2 minutes $TOMORROW $t" +%s)
		if [ -z "$NEXT" ] || [ "$NEXT" -gt "$TS" ]; then
			NEXT="$TS"
		fi
	done
fi

if [ -n "$TEST" ]; then
	echo -n "Next wakeup: "
	date -d "@$NEXT"
	exit 0
fi

# Enable WOL again, in case it got disabled
/opt/openslx/scripts/cron-enable_wol

if [ -n "$NEXT" ]; then
	# Set -l and hope it does the right thing on DST
	rtcwake -l -m no -t "$NEXT"
else
	rtcwake -m disable
fi

exit 0