summaryrefslogtreecommitdiff
path: root/notify-replace
blob: b083565b34b11e1397e042fb1acb901d28e5bbe3 (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
#!/bin/sh -u
# Sends, remembers, and replaces notifications based on program name that's
# passed as first argument to script. Uses notify-send.
# Non-POSIX features used: flock(1)
# TODO: consider replacing flock(1) with a POSIX-complaint alternative

config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/notify-replace"
[ -d "$config_dir" ] || mkdir -p "$config_dir"

if [ -z "${1:-}" ]; then
	# TODO: check if there's a better way to acquire name of currently
	# executing script
	# TODO: get rid of subshell
	echo "$(basenme "$0"): Need to provide notification id filename as first \
	argument" 1>&2
	exit 1
fi
lockdir="${config_dir}/notify-replace.lock"
notification_file="${config_dir}/${1}"
shift

# Exit if lock is in use.  A lock is used to prevent multiple invocations
# of script from replacing notification id at the same time
exec 9>"$lockdir"
flock -n 9 || exit 1

if [ -s "$notification_file" ]; then
	# replace previous notification
	nid="$(notify-send -pr "$(cat "$notification_file")" "$@")"
else 
	# send new notification
	nid="$(notify-send -p "$@")"
fi
# Remember ID of sent notification.
printf '%s' "$nid" >"$notification_file"