#!/usr/bin/env sh
# Wrapper so the analysis skills can call design_io from an INSTALLED plugin.
#
# Plugin skill commands run in the user's project cwd, but `skills._shared` and
# `insight_blueprint` live in the plugin. This wrapper runs the module from the
# plugin's own uv project (which self-provides both) and points it at the user's
# project `.insight/` via INSIGHT_BASE_DIR. See docs/adr/0006-plugin-execution-model.md.
set -e

proj="${CLAUDE_PROJECT_DIR:-$PWD}"
cd "${CLAUDE_PLUGIN_ROOT:?CLAUDE_PLUGIN_ROOT is not set (run via the installed plugin)}"

# Anchor the data base before it becomes a read/write root (defense in depth, ADR-0006 M1).
# proj must be an existing directory and the base must live at <proj>/.insight — reject
# anything else rather than mkdir/write under an attacker-influenced path.
[ -d "$proj" ] || { echo "design_io: project dir '$proj' does not exist" >&2; exit 1; }
export INSIGHT_BASE_DIR="$proj/.insight"
case "$INSIGHT_BASE_DIR" in
  */.insight) ;;
  *) echo "design_io: refusing base '$INSIGHT_BASE_DIR' (must end in /.insight)" >&2; exit 1;;
esac

# Persist the uv venv across plugin updates when a data dir is available.
if [ -n "${CLAUDE_PLUGIN_DATA:-}" ]; then
  export UV_PROJECT_ENVIRONMENT="$CLAUDE_PLUGIN_DATA/uv-venv"
fi

mkdir -p "$INSIGHT_BASE_DIR/designs" \
         "$INSIGHT_BASE_DIR/catalog/sources" \
         "$INSIGHT_BASE_DIR/catalog/knowledge"

exec uv run python -m skills._shared.design_io "$@"
