#!/bin/bash
# Install with: omarchy hook install theme-set contrib/omarchy/sup-theme
set -euo pipefail

# These overrides also let the hook be tested without changing the desktop.
theme_dir=${SUP_OMARCHY_THEME_DIR:-$HOME/.local/state/omarchy/current/theme}
themes_dir=${SUP_THEMES_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/sup/themes}
source_file=$theme_dir/sup.json

# A theme without a generated palette leaves the last accepted colors alone.
[[ -f $source_file && ! -L $source_file ]] || exit 0
mkdir -p -- "$themes_dir"
temporary=$(mktemp "$themes_dir/.omarchy.XXXXXX")
trap 'rm -f -- "$temporary"' EXIT
cp -- "$source_file" "$temporary"
chmod 644 "$temporary"
mv -f -- "$temporary" "$themes_dir/omarchy.json"

# This command only contacts an existing instance. A stopped app stays stopped.
# The next launch reads the file if no instance is running or predates reload.
sup reload-themes >/dev/null 2>&1 || true
