summaryrefslogtreecommitdiff
path: root/magickindle/bin/start.sh
blob: 4b260ad44f6190c78cdb667386066cf7b9539fad (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/sh
# redirect stderr and stdout to file
exec >> /mnt/us/log 2>&1

# kill other instances of this script
me=$$
ps -ef | grep 'start.sh' | awk -v me=$me '$2 != me {print $2}' | xargs kill

echo "$(date) starting"

update_screensaver() {
	echo "$(date) downloading screensaver"

	#echo "$(date) enabling wifi"
	#lipc-set-prop com.lab126.cmd wirelessEnable 1
	for i in 1 2 3 4 5
	do
		curl "CHANGETHIS.png"  --output /mnt/us/screensaver.png --fail --silent --show-error && break
		sleep 10
	done
	# turning off wireless during ready to suspend causes issues with Kindle Wifi
	# - the Kindle forgets the gateway, the configuration gets desynced,
	# leading to the Kindle being connected but having no network
	#echo "$(date) disabling wifi"
	#lipc-set-prop com.lab126.cmd wirelessEnable 0

	echo "$(date) updating screen"
	eips -f -g /mnt/us/screensaver.png
}

LAST_UPDATE=0
lipc-wait-event -m com.lab126.powerd goingToScreenSaver,readyToSuspend | while read event
do
	echo "$(date) event $event"

	# throttle to 1/minute
	NOW=$(date +%s)
	if [ "$(($NOW - $LAST_UPDATE))" -ge 60 ]
	then
		# run in background so loop is not blocked
		update_screensaver &
	fi
	LAST_UPDATE=$NOW

	case "$event" in
		# event looks like "goingToScreenSaver 7" so use a regex
		readyToSuspend*)
			echo "$(date) scheduling wakeup"
			# must be called within 5s after readyToSuspend
			# 60s until screensaver + 29min sleep
			lipc-set-prop -i com.lab126.powerd rtcWakeup 1740
		;;
		goingToScreenSaver*)
		;;
	esac
	echo "$(date) waiting for next event"
done;