#!/bin/sh
# Synchronize dunst notification colors and font with active .Xresources/.xth theme.
set -eu

HOME_DIR="${HOME:-/home/user}"
XRES_FILE="${GEMINUX_XRESOURCES:-$HOME_DIR/.Xresources}"
if [ ! -r "$XRES_FILE" ] && [ -r /etc/skel/.Xresources ]; then
  XRES_FILE="/etc/skel/.Xresources"
fi

get_xres() {
  key="$1"
  def="$2"
  if [ -r "$XRES_FILE" ]; then
    val="$(awk -F: -v k="$key" '
      $0 !~ /^[[:space:]]*[!#]/ {
        kpart = $1
        gsub(/^[[:space:]]+|[[:space:]]+$/, "", kpart)
        if (kpart == k) {
          sub(/^[^:]*:[[:space:]]*/, "", $0)
          gsub(/^[[:space:]"'\'']+|[[:space:]"'\'']+$/, "", $0)
          print $0
        }
      }
    ' "$XRES_FILE" | tail -n 1)"
    if [ -n "$val" ]; then
      printf '%s' "$val"
      return
    fi
  fi
  printf '%s' "$def"
}

BG_MAIN="$(get_xres "theme.bgMain" "#0f1419")"
BG_OVERLAY="$(get_xres "theme.bgOverlay" "$(get_xres "theme.bgHeader" "$BG_MAIN")")"
FG_MAIN="$(get_xres "theme.fgMain" "#e6e1cf")"
FG_BRIGHT="$(get_xres "theme.fgBright" "$FG_MAIN")"
FG_MUTED="$(get_xres "theme.fgMuted" "#7b8898")"
ACCENT="$(get_xres "theme.accent" "$(get_xres "theme.selBg" "#59c2ff")")"
BORDER="$(get_xres "theme.border" "#283444")"
DANGER="$(get_xres "theme.danger" "#f07178")"
DANGER_BG="$(get_xres "theme.dangerBg" "#4c1d24")"
FONT_FACE="$(get_xres "theme.fontFace" "Fira Code")"

BASE_SIZE="$(get_xres "theme.baseFontSize" "32")"
case "$BASE_SIZE" in
  ''|*[!0-9]*) BASE_SIZE=32 ;;
esac
PX_SIZE="$(awk -v b="$BASE_SIZE" 'BEGIN {
  px = int(b + 2)
  if (px < 30) px = 30
  if (px > 44) px = 44
  printf "%dpx", px
}')"

OUT_CONF="${GEMINUX_DUNSTRC:-$HOME_DIR/.config/dunst/dunstrc}"
mkdir -p "$(dirname "$OUT_CONF")"

cat > "$OUT_CONF.tmp" <<EOF
# Auto-generated by geminux-sync-dunst-theme from active .Xresources theme
[global]
    monitor = 0
    follow = mouse
    width = 720
    height = (0, 360)
    origin = top-right
    offset = (20, 88)
    notification_limit = 5
    progress_bar = true
    padding = 18
    horizontal_padding = 22
    text_icon_padding = 16
    frame_width = 3
    frame_color = "$ACCENT"
    separator_color = "$BORDER"
    corner_radius = 8
    idle_threshold = 120
    font = $FONT_FACE $PX_SIZE
    markup = full
    format = "<b>%s</b>\n%b"
    alignment = left
    vertical_alignment = center
    show_age_threshold = 60
    word_wrap = yes
    icon_position = left
    max_icon_size = 64
    dmenu = /usr/bin/dmenu -p dunst:
    mouse_left_click = do_action, close_current
    mouse_middle_click = close_current
    mouse_right_click = close_all

[urgency_low]
    background = "$BG_MAIN"
    foreground = "$FG_MUTED"
    frame_color = "$BORDER"
    timeout = 5

[urgency_normal]
    background = "$BG_OVERLAY"
    foreground = "$FG_MAIN"
    frame_color = "$ACCENT"
    timeout = 8

[urgency_critical]
    background = "$DANGER_BG"
    foreground = "$FG_BRIGHT"
    frame_color = "$DANGER"
    timeout = 0
EOF
mv -f "$OUT_CONF.tmp" "$OUT_CONF"

if [ "${1:-}" != "--no-restart" ] && command -v dunstctl >/dev/null 2>&1; then
  if pidof dunst >/dev/null 2>&1; then
    dunstctl reload "$OUT_CONF" 2>/dev/null || {
      killall -q dunst 2>/dev/null || true
      DISPLAY="${DISPLAY:-:0}" dunst -config "$OUT_CONF" >/dev/null 2>&1 </dev/null &
    }
  fi
fi
