summaryrefslogtreecommitdiff
path: root/notify-replace
blob: c3bc92ba01d39103806b637a2cee2427955f2b3b (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
#!/bin/sh -e
# 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

xdg_config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
config_dir="$xdg_config_home/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
	program=$(basename $0)
	# TODO: shorten line
	echo "$program: 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"