2 # notify-replace: sends, remembers, and replaces notifications based on program
3 # name that's passed as first argument to script. Uses notify-send.
4 # Non-POSIX features used: flock(1)
5 # TODO: consider replacing flock(1) with a POSIX-complaint alternative
7 config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/notify-replace"
8 [ -d "$config_dir" ] || mkdir -p "$config_dir"
10 if [ -z "${1:-}" ]; then
11 # TODO: check if there's a better way to acquire name of currently
13 # TODO: get rid of subshell
14 echo "$(basenme "$0"): Need to provide notification id filename as first \
18 lockdir="${config_dir}/notify-replace.lock"
19 notification_file="${config_dir}/${1}"
22 # Exit if lock is in use. A lock is used to prevent multiple invocations
23 # of script from replacing notification id at the same time
27 if [ -s "$notification_file" ]; then
28 # replace previous notification
29 nid="$(notify-send -pr "$(cat "$notification_file")" "$@")"
31 # send new notification
32 nid="$(notify-send -p "$@")"
34 # Remember ID of sent notification.
35 printf '%s' "$nid" >"$notification_file"