~mil/sxmo-utils

sxmo-utils/scripts/notifications/sxmo_notificationmonitor.sh -rwxr-xr-x 1.8 KiB
d8b64fd6Nathaniel Barragan Fix looping behavior 2 days ago
                                                                                
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
72
73
74
#!/usr/bin/env sh
trap gracefulexit INT TERM

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

gracefulexit() {
	echo "Gracefully exiting $0"
	kill -9 0
}

handlenewnotiffile(){
	NOTIFFILE="$1"

	if [ "$(wc -l "$NOTIFFILE" | cut -d' ' -f1)" -lt 3 ]; then
		echo "Invalid notification file $NOTIFFILE found (<3 lines -- see notif spec in sxmo_notifwrite.sh), deleting!" >&2
		rm -f "$NOTIFFILE"
	else
		sxmo_hooks.sh notification "$NOTIFFILE" &
		NOTIFACTION="$(awk NR==1 "$NOTIFFILE")"
		NOTIFWATCHFILE="$(awk NR==2 "$NOTIFFILE")"
		NOTIFMSG="$(tail -n+3 "$NOTIFFILE" | cut -c1-70)"

		(
			dunstify --action="2,open" "$NOTIFMSG" | grep 2 && (
				setsid -f sh -c "$NOTIFACTION" &
				rm -f "$NOTIFFILE"
			)
		) &

		[ -e "$NOTIFWATCHFILE" ] && (
			inotifywait "$NOTIFWATCHFILE" && rm -f "$NOTIFFILE"
		) &
	fi
}

recreateexistingnotifs() {
	for NOTIF in "$NOTIFDIR"/*; do
		[ -f "$NOTIF" ] || continue
		handlenewnotiffile "$NOTIF"
	done
}

syncled() {
	sxmo_setled.sh green 0
	if [ "$(find "$NOTIFDIR"/ -type f | wc -l)" -gt 0 ]; then
		sleep 0.1
		sxmo_setled.sh green 1
	fi
}

monitorforaddordelnotifs() {
	while true; do
		if [ ! -e "$NOTIFDIR" ]; then
			mkdir -p "$NOTIFDIR" || sleep 10
		fi
		inotifywait -e create,attrib,moved_to,delete,delete_self,moved_from "$NOTIFDIR"/ | (
			INOTIFYOUTPUT="$(cat)"
			INOTIFYEVENTTYPE="$(echo "$INOTIFYOUTPUT" | cut -d" " -f2)"
			syncled
			if echo "$INOTIFYEVENTTYPE" | grep -E "CREATE|MOVED_TO|ATTRIB"; then
				NOTIFFILE="$NOTIFDIR/$(echo "$INOTIFYOUTPUT" | cut -d" " -f3)"
				handlenewnotiffile "$NOTIFFILE"
			fi
		) & wait
	done
}

pgrep -f "$(command -v sxmo_notificationmonitor.sh)" | grep -Ev "^${$}$" | xargs -r kill
rm -f "$NOTIFDIR"/incomingcall
recreateexistingnotifs
syncled
monitorforaddordelnotifs