#!/bin/bash
# Play a notification sound, alternating between Chord.wav and Chord2.wav.
# Skips: do-not-disturb mode and low-urgency notifications.
# Usage: notify-sound <notification-id>   (called from mako's on-notify binding)

ID="${1:-}"

# Silence while do-not-disturb is active
if makoctl mode 2>/dev/null | grep -q dnd; then
    exit 0
fi

# Only play for normal/critical urgency
if [ -n "$ID" ]; then
    urgency=$(makoctl list 2>/dev/null | awk -v id="$ID" '
        $1 == "Notification" { if (seen) exit; if ($2 == id) seen = 1; next }
        seen && /^  Urgency:/ { print $2; exit }')
    if [ "$urgency" = "low" ]; then
        exit 0
    fi
fi

SOUND_DIR=~/.config/mako/scripts
STATE_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/mako-notify-sound"

# Alternate between the two sounds on every play
if [ -f "$STATE_FILE" ] && [ "$(cat "$STATE_FILE")" = "0" ]; then
    SOUND="$SOUND_DIR/Chord2.wav"
    echo "1" > "$STATE_FILE"
else
    SOUND="$SOUND_DIR/Chord.wav"
    echo "0" > "$STATE_FILE"
fi

if command -v paplay >/dev/null 2>&1; then
    paplay "$SOUND"
elif command -v pw-play >/dev/null 2>&1; then
    pw-play "$SOUND"
fi
