#!/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

if [ -r "$XRES_FILE" ]; then
  eval "$(awk '
    $0 !~ /^[[:space:]]*[!#]/ {
      idx = index($0, ":")
      if (idx <= 0) next
      k = substr($0, 1, idx - 1)
      v = substr($0, idx + 1)
      gsub(/^[[:space:]]+|[[:space:]]+$/, "", k)
      gsub(/^[[:space:]"'\'']+|[[:space:]"'\'']+$/, "", v)
      if (k == "theme.bgMain" || k == "theme.bgOverlay" || k == "theme.bgHeader" ||
          k == "theme.fgMain" || k == "theme.fgBright" || k == "theme.fgMuted" ||
          k == "theme.accent" || k == "theme.selBg" || k == "theme.border" ||
          k == "theme.danger" || k == "theme.dangerBg" || k == "theme.fontFace" ||
          k == "theme.baseFontSize") {
        sub(/^theme\./, "", k)
        gsub(/[^A-Za-z0-9_]/, "_", k)
        vals[k] = v
      }
    }
    END {
      for (k in vals) {
        s = vals[k]
        gsub(/'\''/, "'\''\\'\'''\''", s)
        printf "X_%s='\''%s'\''\n", k, s
      }
    }
  ' "$XRES_FILE")"
fi

BG_MAIN="${X_bgMain:-#0f1419}"
BG_OVERLAY="${X_bgOverlay:-${X_bgHeader:-$BG_MAIN}}"
FG_MAIN="${X_fgMain:-#e6e1cf}"
FG_BRIGHT="${X_fgBright:-$FG_MAIN}"
FG_MUTED="${X_fgMuted:-#7b8898}"
ACCENT="${X_accent:-${X_selBg:-#59c2ff}}"
BORDER="${X_border:-#283444}"
DANGER="${X_danger:-#f07178}"
DANGER_BG="${X_dangerBg:-#4c1d24}"
FONT_FACE="${X_fontFace:-Fira Code}"

BASE_SIZE="${X_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-center
    offset = (0, 96)
    notification_limit = 5
    progress_bar = true
    progress_bar_height = 18
    progress_bar_frame_width = 2
    progress_bar_min_width = 560
    progress_bar_max_width = 660
    padding = 18
    horizontal_padding = 22
    text_icon_padding = 16
    frame_width = 3
    frame_color = "$ACCENT"
    separator_color = "$BORDER"
    corner_radius = 0
    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

[osd]
    appname = "geminux-osd"
    alignment = center
    background = "$BG_OVERLAY"
    foreground = "$FG_BRIGHT"
    highlight = "$ACCENT"
    frame_color = "$ACCENT"
    history_ignore = yes

[osd_single]
    appname = "geminux-osd"
    body = ""
    format = "<b>%s</b>"
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" >/dev/null 2>&1 || {
      killall -q dunst 2>/dev/null || true
      DISPLAY="${DISPLAY:-:0}" dunst -config "$OUT_CONF" >/dev/null 2>&1 </dev/null &
    }
  fi
fi
