#!/bin/bash

# omarchy:summary=Apply current Omarchy theme colors and background alpha to running Foot terminals
# omarchy:hidden=true

colors_toml=$HOME/.local/state/omarchy/current/theme/colors.toml
foot_ini=$HOME/.local/state/omarchy/current/theme/foot.ini

[[ -f $colors_toml ]] || exit 0

# Theme switches wait on this retint, and pgrep scans every process, so it runs
# once; each terminal's PTY comes from foot's own /dev/ptmx fdinfo (which works
# even when a direct child launched with -e redirects stdout, like agy, and for
# foot --server with multiple windows), falling back to child fds if needed.
foot_pids=$(pgrep -x foot) || exit 0

foot_osc=$(omarchy-theme-osc "$colors_toml")
[[ -n $foot_osc ]] || exit 0

# foot takes a background alpha as an urxvt-style [percent] prefix, so a theme
# going see-through or back, as with Glassy windows, reaches running terminals.
# The last alpha in the theme's foot.ini wins, as it does when foot reads it.
alpha=$(sed -n 's/^[[:space:]]*alpha[[:space:]]*=[[:space:]]*\([0-9.]*\).*/\1/p' "$foot_ini" 2>/dev/null | tail -n 1)
background=$(omarchy-theme-color --file "$colors_toml" background 2>/dev/null)
if [[ -n $background ]]; then
  percent=$(awk -v alpha="${alpha:-1}" 'BEGIN { printf "%d", alpha * 100 + 0.5 }')
  foot_osc+=$(printf '\033]11;[%d]%s\007' "$percent" "$background")
fi

declare -A seen_ttys=()
write_tty() {
  local tty="$1"
  [[ $tty == /dev/pts/* && -z ${seen_ttys[$tty]:-} ]] || return 0
  seen_ttys[$tty]=1
  { printf '%s' "$foot_osc" >"$tty"; } 2>/dev/null || true
}

for foot_pid in $foot_pids; do
  found=false
  for fdinfo in "/proc/$foot_pid/fdinfo"/*; do
    [[ -r $fdinfo ]] || continue
    {
      while IFS=$' \t:' read -r key value _; do
        if [[ $key == "tty-index" && $value =~ ^[0-9]+$ ]]; then
          write_tty "/dev/pts/$value"
          found=true
        fi
      done <"$fdinfo"
    } 2>/dev/null || true
  done
  [[ $found == true ]] && continue

  children="/proc/$foot_pid/task/$foot_pid/children"
  if [[ -e $children ]]; then
    { child_pids=$(<"$children"); } 2>/dev/null || continue
  else
    # A kernel built without CONFIG_PROC_CHILDREN keeps no such list.
    child_pids=$(pgrep -P "$foot_pid")
  fi
  for child_pid in $child_pids; do
    for fd in 1 0 2; do
      tty=$(readlink "/proc/$child_pid/fd/$fd" 2>/dev/null)
      if [[ $tty == /dev/pts/* ]]; then
        write_tty "$tty"
        break
      fi
    done
  done
done
