#!/bin/sh
# ANODE Dunst notification theme & DPI scaler (strict POSIX sh)
set -eu

: "${XDG_CONFIG_HOME:=$HOME/.config}"
XRES="${1:-$HOME/.Xresources}"
DUNST_DIR="$XDG_CONFIG_HOME/dunst"
DUNST_RC="$DUNST_DIR/dunstrc"

xres_get() {
  _k="$1"
  _def="$2"
  _val="$(awk -v key="$_k" '
    {
      s = $0
      gsub(/^[ \t\r]+|[ \t\r]+$/, "", s)
      if (s == "" || substr(s, 1, 1) == "!" || substr(s, 1, 1) == "#") next
      p = index(s, ":")
      if (p > 0) {
        k = substr(s, 1, p - 1)
        gsub(/^[ \t]+|[ \t]+$/, "", k)
        if (k == key) {
          v = substr(s, p + 1)
          gsub(/^[ \t]+|[ \t]+$/, "", v)
          print v
          exit
        }
      }
    }
  ' "$XRES" 2>/dev/null || true)"
  if [ -n "$_val" ]; then
    printf '%s\n' "$_val"
  else
    printf '%s\n' "$_def"
  fi
}

if [ ! -r "$XRES" ]; then
  exit 0
fi

FONT_FACE="$(xres_get theme.fontFace "Fira Code")"
SCALE="$(xres_get theme.scale "1.00")"
BASE_SZ="$(xres_get theme.baseFontSize "15")"

BG_OVERLAY="$(xres_get theme.bgOverlay "#192231")"
BG_URGENT="$(xres_get theme.dangerBg "#471d24")"
FG_BRIGHT="$(xres_get theme.fgBright "#ffffff")"
FG_MAIN="$(xres_get theme.fgMain "#e6e1cf")"
BORDER="$(xres_get theme.border "#283444")"
ACCENT="$(xres_get theme.accent "#59c2ff")"
WARNING="$(xres_get theme.warning "#ffb454")"
DANGER="$(xres_get theme.danger "#f07178")"

GEOM="$(awk -v s="$SCALE" -v bsz="$BASE_SZ" 'BEGIN {
  sc = s + 0.0
  if (sc < 0.50) sc = 0.50
  if (sc > 4.00) sc = 4.00
  w = int(320.0 * sc + 0.5); if (w < 220) w = 220
  hmax = int(160.0 * sc + 0.5); if (hmax < 80) hmax = 80
  ox = int(14.0 * sc + 0.5); if (ox < 8) ox = 8
  oy = int(38.0 * sc + 0.5); if (oy < 20) oy = 20
  pad = int(10.0 * sc + 0.5); if (pad < 6) pad = 6
  hpad = int(12.0 * sc + 0.5); if (hpad < 8) hpad = 8
  tpad = int(8.0 * sc + 0.5); if (tpad < 4) tpad = 4
  fw = int(2.0 * sc + 0.5); if (fw < 1) fw = 1
  rad = int(6.0 * sc + 0.5); if (rad < 3) rad = 3
  pbar_h = int(6.0 * sc + 0.5); if (pbar_h < 4) pbar_h = 4
  fpx = int((bsz + 0.0) + 0.5); if (fpx < 10) fpx = 10; if (fpx > 72) fpx = 72
  printf "%d %d %d %d %d %d %d %d %d %d %d\n", w, hmax, ox, oy, pad, hpad, tpad, fw, rad, pbar_h, fpx
}')"

set -- $GEOM
D_W="$1" D_H="$2" D_OX="$3" D_OY="$4" D_PAD="$5" D_HPAD="$6" D_TPAD="$7" D_FW="$8" D_RAD="$9"
shift 9
D_PBAR="$1" D_FPX="$2"

mkdir -p "$DUNST_DIR"
TMP_RC="$DUNST_RC.tmp.$$"

cat > "$TMP_RC" <<EOF
# Managed by anode-sync-dunst-theme (scale=$SCALE)
[global]
    monitor = 0
    follow = keyboard
    scale = 1
    width = $D_W
    height = (10, $D_H)
    origin = top-right
    offset = ($D_OX, $D_OY)
    notification_limit = 4
    progress_bar = true
    progress_bar_height = $D_PBAR
    progress_bar_frame_width = 1
    progress_bar_corner_radius = 4
    highlight = "$ACCENT"
    indicate_hidden = yes
    separator_height = 2
    padding = $D_PAD
    horizontal_padding = $D_HPAD
    text_icon_padding = $D_TPAD
    frame_width = $D_FW
    frame_color = "$BORDER"
    separator_color = frame
    corner_radius = $D_RAD
    font = $FONT_FACE ${D_FPX}px
    line_height = 2
    markup = full
    format = "<b>%s</b>\n%b"
    alignment = left
    vertical_alignment = center
    show_age_threshold = 60
    ellipsize = end
    word_wrap = yes
    stack_duplicates = true
    show_indicators = yes
    mouse_left_click = close_current
    mouse_middle_click = do_action, close_current
    mouse_right_click = close_all

[urgency_low]
    background = "$BG_OVERLAY"
    foreground = "$FG_MAIN"
    frame_color = "$BORDER"
    highlight = "$ACCENT"
    timeout = 4

[urgency_normal]
    background = "$BG_OVERLAY"
    foreground = "$FG_BRIGHT"
    frame_color = "$ACCENT"
    highlight = "$ACCENT"
    timeout = 6

[urgency_critical]
    background = "$BG_URGENT"
    foreground = "$FG_BRIGHT"
    frame_color = "$DANGER"
    highlight = "$WARNING"
    timeout = 0
EOF

mv -f "$TMP_RC" "$DUNST_RC"

if [ -n "${DISPLAY:-}" ] && [ "${ANODE_NO_SIGNAL:-0}" != "1" ]; then
  if command -v dunstctl >/dev/null 2>&1; then
    dunstctl reload >/dev/null 2>&1 || true
  fi
  _dpids="$( (ps -eo pid=,comm= 2>/dev/null || ps -ax -o pid=,comm= 2>/dev/null || true) | awk '
    { cmd=$2; sub(/^.*\//, "", cmd); if (cmd == "dunst") print $1 }
  ' )"
  if [ -n "${_dpids:-}" ]; then
    for _p in $_dpids; do
      kill -HUP "$_p" 2>/dev/null || true
    done
  fi
fi
