#!/bin/sh
# SessionStart hook — announce plugin paths and surface in-flight runs.
#
# The plugin root is derived from this script's own location, so it works for marketplace
# installs and local checkouts alike, including paths containing spaces.
#
# No dependency install happens here: the scripts' one YAML dependency is vendored
# (scripts/lib/vendor/js-yaml.cjs, loaded via scripts/lib/yaml.js), so they run with plain
# Node in any environment the plugin is copied to — no npm, no network required.

PLUGIN_ROOT=$(cd "$(dirname "$0")/../.." && pwd)

echo "STACKONE_PLUGIN_ROOT=$PLUGIN_ROOT"
echo "STACKONE_SCRIPTS=$PLUGIN_ROOT/scripts"
echo 'Connector routing: any request to build, research, or validate a connector runs through the gated flow — /connector:full (or :research / :build / :validate for one phase). Never author connector YAML freestyle: without run.js init, scope, and the gates the output is unverified. When in doubt, invoke /connector:full explicitly.'

if [ -d src/configs ] && grep -q '@stackone/cli' package.json 2>/dev/null; then
    echo '🔌 Connector development environment detected'
    if [ -d .git ] && ! grep -qE '^\.connector-dev/?$' .gitignore 2>/dev/null; then
        # Keep run state (and anything credential-shaped under it) out of commits.
        if [ -s .gitignore ] && [ -n "$(tail -c1 .gitignore)" ]; then
            printf '\n' >>.gitignore
        fi
        echo '.connector-dev/' >>.gitignore
    fi
    node "$PLUGIN_ROOT/scripts/hooks/report-run-status.js" --mode session
fi

exit 0
