#!/bin/bash

# omarchy:summary=Sync the generated Omarchy theme to T3 Code
# omarchy:hidden=true

set -euo pipefail

T3CODE_SOURCE_PATH="$HOME/.local/state/omarchy/current/theme/t3code.json"
T3CODE_CSS_SOURCE_PATH="$HOME/.local/state/omarchy/current/theme/t3code.css"
T3CODE_HOME="${T3CODE_HOME:-$HOME/.t3}"
T3CODE_STATE_DIR="$T3CODE_HOME/userdata"
# The filename is the theme id T3 Code shows and `t3 theme set` accepts, and
# it stays stable while the colors change underneath on every theme switch.
T3CODE_THEME_PATH="$T3CODE_STATE_DIR/themes/omarchy.json"

# Only follow an install that already exists. Creating the directory here would
# leave a stray T3 Code state dir on machines that do not run it.
[[ -d $T3CODE_STATE_DIR ]] || exit 0

# Theme setters release this lock before running app helpers, and a corners
# change runs this one on its own. Hold it through publication so a switch
# cannot swap the theme between reading its JSON and its CSS, and an older run
# cannot publish after a newer one.
exec {theme_lock}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-theme-set.lock"
flock "$theme_lock"

[[ -f $T3CODE_SOURCE_PATH ]] || exit 0
palette=$(<"$T3CODE_SOURCE_PATH")
chrome=""
[[ -f $T3CODE_CSS_SOURCE_PATH ]] && chrome=$(<"$T3CODE_CSS_SOURCE_PATH")

# A theme without an accent or a background leaves its placeholder unresolved.
# Publishing that would only make T3 Code reject the file, so skip instead and
# leave the previous palette in place.
if [[ $palette == *'{{'* || $chrome == *'{{'* ]]; then
  echo "Skipping T3 Code theme: the generated theme has unresolved colors." >&2
  exit 0
fi

# Corners follow Hyprland's live rounding, as the shell's do, so the theme, the
# corners toggle and the user's own looknfeel.lua all land the same way. Outside
# the session there is no instance signature, so reach the running Hyprland.
corner_radius() {
  local instance=() radius

  [[ -n ${HYPRLAND_INSTANCE_SIGNATURE:-} ]] || instance=(--instance 0)
  radius=$(hyprctl "${instance[@]}" -j getoption decoration:rounding 2>/dev/null | jq -r '.int // empty' 2>/dev/null || true)
  [[ $radius =~ ^[0-9]+$ ]] || radius=0
  echo "$radius"
}

css=""
if [[ -n $chrome ]]; then
  radius=$(corner_radius)
  css=":root { --omarchy-radius: ${radius}px; }"$'\n'
  # T3 Code rounds pills and badges with fixed sizes no variable reaches, so
  # square corners have to be forced everywhere.
  if (( radius == 0 )); then
    css+='*, *::before, *::after { border-radius: 0 !important; }'$'\n'
  fi
  css+=$chrome
fi

# T3 Code watches this file and retints every connected client on change, so
# the write has to be atomic: a half-written file would read as invalid. A
# theme that ships its own t3code.json may carry CSS of its own; it goes after
# Omarchy's chrome so its rules win.
mkdir -p "$(dirname "$T3CODE_THEME_PATH")"
tmp=$(mktemp "$T3CODE_THEME_PATH.XXXXXX")
trap 'rm -f "$tmp"' EXIT
jq --arg css "$css" '
  if $css == "" then .
  elif (.css // "") == "" then .css = $css
  else .css = $css + "\n" + .css
  end
' <<<"$palette" >"$tmp"
mv "$tmp" "$T3CODE_THEME_PATH"
