#!/usr/bin/env bash

set -euo pipefail

help() {
	echo "notify-send: send deduplicated notifications"
	echo "Usage: notify-once <name> [ARGS]"
	echo "  ARGS are arguments passed directly to notify-send"
}

if [ $# -lt 1 ]; then
	echo "Should get at least one argument, a name" >&2
	help
	exit 1
fi
APPNAME="$1"
shift

ID_PATH="/tmp/notify-state-$APPNAME"

if [ -e "$ID_PATH" ]; then
	# Exists, replace
	ID=$(cat "$ID_PATH")
	ID=$(notify-send -r "$ID" -p "$@")
else
	# Doesn't exist, create
	ID=$(notify-send -p "$@")
fi

# Store new ID
echo "$ID" >"$ID_PATH"
