#!/bin/bash

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

colors_toml=~/.config/omarchy/current/theme/colors.toml

if [[ ! -f $colors_toml ]] || ! pgrep -x foot >/dev/null; then
  exit 0
fi

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

for foot_pid in $(pgrep -x foot); do
  for child_pid in $(pgrep -P "$foot_pid"); do
    tty=$(readlink "/proc/$child_pid/fd/1" 2>/dev/null)
    [[ $tty == /dev/pts/* ]] && printf '%b' "$foot_osc" >"$tty"
  done
done
