mpc-notify

mpd/ncmpcc notifications with album covers
git clone https://git.pastanoggin.com/mpc-notify.git
Log | Files | Refs | README

notify-replace (1144B)


      1 #!/bin/sh -u
      2 # Sends, remembers, and replaces notifications based on program name that's
      3 # 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
      6 
      7 config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/notify-replace"
      8 [ -d "$config_dir" ] || mkdir -p "$config_dir"
      9 
     10 if [ -z "${1:-}" ]; then
     11 	# TODO: check if there's a better way to acquire name of currently
     12 	# executing script
     13 	# TODO: get rid of subshell
     14 	echo "$(basenme "$0"): Need to provide notification id filename as first \
     15 	argument" 1>&2
     16 	exit 1
     17 fi
     18 lockdir="${config_dir}/notify-replace.lock"
     19 notification_file="${config_dir}/${1}"
     20 shift
     21 
     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
     24 exec 9>"$lockdir"
     25 flock -n 9 || exit 1
     26 
     27 if [ -s "$notification_file" ]; then
     28 	# replace previous notification
     29 	nid="$(notify-send -pr "$(cat "$notification_file")" "$@")"
     30 else 
     31 	# send new notification
     32 	nid="$(notify-send -p "$@")"
     33 fi
     34 # Remember ID of sent notification.
     35 printf '%s' "$nid" >"$notification_file"