#!/usr/bin/env bash set -euo pipefail action="${1:-doctor}" if [[ $# -gt 0 ]]; then shift fi repo="." dry_run="false" json="false" passthrough=() while [[ $# -gt 0 ]]; do case "$1" in --repo) repo="${2:?--repo requires a path}" shift 2 ;; --dry-run) dry_run="true" shift ;; --json) json="true" shift ;; --) shift passthrough+=("$@") break ;; *) passthrough+=("$1") shift ;; esac done repo="$(cd "$repo" && pwd)" emit() { if [[ "$json" == "true" ]]; then printf '{"skill":"home-assistant-operator","action":"%s","repo":"%s","message":"%s"}\n' "$action" "$repo" "$1" else printf '%s\n' "$1" fi } run_cmd() { if [[ "$dry_run" == "true" ]]; then printf 'dry-run:' printf ' %q' "$@" printf '\n' return 0 fi (cd "$repo" && "$@") } require_file() { if [[ ! -f "$repo/$1" ]]; then emit "missing $1 in $repo" exit 1 fi } case "$action" in setup) require_file scripts/home-assistant-bootstrap.js run_cmd node "$repo/scripts/home-assistant-bootstrap.js" "${passthrough[@]}" ;; doctor) require_file scripts/home-assistant-verify.js run_cmd node "$repo/scripts/home-assistant-verify.js" "${passthrough[@]}" ;; audit) require_file scripts/home-assistant-inventory.js run_cmd node "$repo/scripts/home-assistant-inventory.js" "${passthrough[@]}" ;; sync) require_file scripts/sync-cursor-mcp.js run_cmd node "$repo/scripts/sync-cursor-mcp.js" "${passthrough[@]}" ;; *) emit "unknown action '$action'" exit 1 ;; esac