#!/bin/bash

# omarchy:summary=Print OSC sequences for an Omarchy color theme
# omarchy:hidden=true

theme="${1:-$HOME/.config/omarchy/current/theme/colors.toml}"

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

awk -F= '
  function clean(value) {
    gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
    if (value ~ /^"/) {
      sub(/^"/, "", value)
      sub(/".*$/, "", value)
    }
    return value
  }

  {
    key = $1
    gsub(/^[[:space:]]+|[[:space:]]+$/, "", key)
    value = clean($2)
  }

  key == "foreground" { printf "\033]10;%s\007", value }
  key == "background" { printf "\033]11;%s\007", value }
  key == "cursor" { printf "\033]12;%s\007", value }
  key == "selection_background" { printf "\033]17;%s\007", value }
  key == "selection_foreground" { printf "\033]19;%s\007", value }
  key ~ /^color[0-9]+$/ {
    color_index = substr(key, 6) + 0
    if (color_index >= 0 && color_index <= 15) {
      printf "\033]4;%d;%s\007", color_index, value
    }
  }
' "$theme"
