#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only
# Synchronizes dunst notification colors and font with the active NeoUI .xth/.Xresources theme.

XRES_FILE="/home/user/.Xresources"
if [ -e /tmp/bananarchy_duress_mode ] && [ -r /tmp/duress_home/.Xresources ]; then
  XRES_FILE="/tmp/duress_home/.Xresources"
elif [ ! -r "$XRES_FILE" ] && [ -r /usr/share/sxmo/appcfg/Xresources ]; then
  XRES_FILE="/usr/share/sxmo/appcfg/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" "")"
[ -z "$BG_MAIN" ] && BG_MAIN="$(get_xres "*background" "#1c1f26")"

BG_OVERLAY="$(get_xres "theme.bgOverlay" "")"
[ -z "$BG_OVERLAY" ] && BG_OVERLAY="$(get_xres "theme.bgHeader" "$BG_MAIN")"

FG_MAIN="$(get_xres "theme.fgMain" "")"
[ -z "$FG_MAIN" ] && FG_MAIN="$(get_xres "*foreground" "#eceff4")"

FG_BRIGHT="$(get_xres "theme.fgBright" "$FG_MAIN")"
FG_MUTED="$(get_xres "theme.fgMuted" "#9aa5b4")"

ACCENT="$(get_xres "theme.accent" "")"
[ -z "$ACCENT" ] && ACCENT="$(get_xres "theme.selBg" "#88c0d0")"

BORDER="$(get_xres "theme.border" "#4c566a")"
DANGER="$(get_xres "theme.danger" "#bf616a")"
DANGER_BG="$(get_xres "theme.dangerBg" "#4c2228")"

FONT_FACE="$(get_xres "theme.fontFace" "Fira Code")"
BASE_SIZE="$(get_xres "theme.baseFontSize" "13")"
case "$BASE_SIZE" in
  ''|*[!0-9]*) BASE_SIZE=13 ;;
esac
PT_SIZE="$(awk -v b="$BASE_SIZE" 'BEGIN {
  pt = b * 0.72
  if (pt < 8.0) pt = 8.0
  if (pt > 14.0) pt = 14.0
  printf "%.1f", pt
}')"

write_dunstrc() {
  target="$1"
  cat > "$target" <<EOF
# Auto-generated by bananarchy-sync-dunst-theme from active NeoUI theme
[global]
    width = 230
    height = 120
    offset = (5,26)
    padding = 4
    horizontal_padding = 6
    frame_width = 2
    frame_color = "$ACCENT"
    separator_color = "$BORDER"
    idle_threshold = 0
    font = $FONT_FACE $PT_SIZE
    markup = full
    max_icon_size = 24
    dmenu = sxmo_dmenu.sh -p dunst:
    follow = mouse
    mouse_left_click = do_action, close_current

[shortcuts]
    close_all = ctrl+shift+space

[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 = 10
EOF
}

write_dunstrc "/tmp/dunstrc.tmp"
mv -f "/tmp/dunstrc.tmp" "/tmp/dunstrc"

if [ ! -e /tmp/bananarchy_duress_mode ]; then
  mkdir -p /home/user/.config/dunst 2>/dev/null || true
  cp -f /tmp/dunstrc /home/user/.config/dunst/dunstrc 2>/dev/null || true
else
  mkdir -p /tmp/duress_home/.config/dunst 2>/dev/null || true
  cp -f /tmp/dunstrc /tmp/duress_home/.config/dunst/dunstrc 2>/dev/null || true
fi

if [ "${1:-}" != "--no-restart" ]; then
  if pidof -s dunst >/dev/null 2>&1; then
    pkill -x dunst >/dev/null 2>&1 || true
    sleep 0.08
    if ! pidof -s dunst >/dev/null 2>&1; then
      DISPLAY="${DISPLAY:-:0}" dunst -config /tmp/dunstrc >/dev/null 2>&1 </dev/null &
    fi
  fi
fi
exit 0
