#!/usr/bin/env bash

# Pre-realise every theme's Home Manager generation into the Nix store so
# future `nomarchy-theme-set` swaps are cache hits — no Stylix rebuild, no
# downloads. Run once after install (the installer wires this up) and again
# after adding or updating palettes.

set -e

if [ -f "/etc/nixos/flake.nix" ]; then
    REPO_DIR="/etc/nixos"
elif [ -f "/etc/nomarchy/flake.nix" ]; then
    REPO_DIR="/etc/nomarchy"
else
    echo "Error: Nomarchy flake repository not found in /etc/nixos or /etc/nomarchy." >&2
    exit 1
fi

echo "Pre-building every theme variant from $REPO_DIR#allThemeVariants..."
echo "This is a one-time cache warm-up. Subsequent theme switches will be instant."
nix build --no-link --print-out-paths \
    --extra-experimental-features "nix-command flakes" \
    "$REPO_DIR#allThemeVariants"

echo "Theme variants cached. Theme switches now avoid rebuilds."
