#!/usr/bin/env zsh

set -eu

repo_root=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
sops_file="${repo_root}/.sops.yaml"

if [ ! -f "${sops_file}" ]; then
  exit 0
fi

if command -v yq >/dev/null 2>&1; then
  yq -r '.creation_rules[]?.path_regex | select(. != null)' "${sops_file}"
  exit 0
fi

if command -v python3 >/dev/null 2>&1; then
  python3 - "${sops_file}" <<'PY'
import sys

try:
    import yaml
except Exception as exc:
    raise SystemExit(f"python3 fallback requires PyYAML: {exc}")

with open(sys.argv[1], "r", encoding="utf-8") as handle:
    data = yaml.safe_load(handle) or {}

for rule in data.get("creation_rules") or []:
    regex = rule.get("path_regex")
    if regex:
        print(regex)
PY
  exit 0
fi

echo "Unable to read .sops.yaml path_regex values: install yq or python3 with PyYAML" >&2
exit 1
