Blame


1 79226042 2023-12-31 boredpast #!/bin/sh -u
2 72ce537b 2025-07-10 noodle # notify-replace: sends, remembers, and replaces notifications based on program
3 72ce537b 2025-07-10 noodle # name that's passed as first argument to script. Uses notify-send.
4 9733f193 2023-06-05 shawtynoo # Non-POSIX features used: flock(1)
5 9733f193 2023-06-05 shawtynoo # TODO: consider replacing flock(1) with a POSIX-complaint alternative
6 9733f193 2023-06-05 shawtynoo
7 79226042 2023-12-31 boredpast config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/notify-replace"
8 9733f193 2023-06-05 shawtynoo [ -d "$config_dir" ] || mkdir -p "$config_dir"
9 9733f193 2023-06-05 shawtynoo
10 79226042 2023-12-31 boredpast if [ -z "${1:-}" ]; then
11 9733f193 2023-06-05 shawtynoo # TODO: check if there's a better way to acquire name of currently
12 9733f193 2023-06-05 shawtynoo # executing script
13 9733f193 2023-06-05 shawtynoo # TODO: get rid of subshell
14 79226042 2023-12-31 boredpast echo "$(basenme "$0"): Need to provide notification id filename as first \
15 79226042 2023-12-31 boredpast argument" 1>&2
16 9733f193 2023-06-05 shawtynoo exit 1
17 9733f193 2023-06-05 shawtynoo fi
18 79226042 2023-12-31 boredpast lockdir="${config_dir}/notify-replace.lock"
19 79226042 2023-12-31 boredpast notification_file="${config_dir}/${1}"
20 9733f193 2023-06-05 shawtynoo shift
21 9733f193 2023-06-05 shawtynoo
22 9733f193 2023-06-05 shawtynoo # Exit if lock is in use. A lock is used to prevent multiple invocations
23 9733f193 2023-06-05 shawtynoo # of script from replacing notification id at the same time
24 9733f193 2023-06-05 shawtynoo exec 9>"$lockdir"
25 9733f193 2023-06-05 shawtynoo flock -n 9 || exit 1
26 9733f193 2023-06-05 shawtynoo
27 9733f193 2023-06-05 shawtynoo if [ -s "$notification_file" ]; then
28 9733f193 2023-06-05 shawtynoo # replace previous notification
29 9733f193 2023-06-05 shawtynoo nid="$(notify-send -pr "$(cat "$notification_file")" "$@")"
30 9733f193 2023-06-05 shawtynoo else
31 9733f193 2023-06-05 shawtynoo # send new notification
32 9733f193 2023-06-05 shawtynoo nid="$(notify-send -p "$@")"
33 9733f193 2023-06-05 shawtynoo fi
34 9733f193 2023-06-05 shawtynoo # Remember ID of sent notification.
35 9733f193 2023-06-05 shawtynoo printf '%s' "$nid" >"$notification_file"