#!/bin/bash
# cards-card-repo-hook
# Pre-commit hook for Cards - bash wrapper delegates to compiled .mjs

# Skip if CARDS_SKIP_HOOK is set
if [ "$CARDS_SKIP_HOOK" = "1" ]; then
  exit 0
fi

# Log file path baked at installation time; env var takes precedence at runtime
export CARDS_GIT_CARD_REPO_HOOKS_LOG_FILE="${CARDS_GIT_CARD_REPO_HOOKS_LOG_FILE:-/home/node/.cards/edh/1b66cb99/workspace/.cards/logs/git-card-repo-hooks.log}"

# Only delegate when the compiled script is present
SCRIPT_DIR="$(dirname "$0")"
if [ -f "$SCRIPT_DIR/pre-commit.mjs" ]; then
  # Resolve Node.js: prefer the extension's bundled interpreter, fall back to PATH
  NODE_BIN=$(cat "$HOME/.cards/VSCODE_NODE" 2>/dev/null)
  if [ -n "$NODE_BIN" ] && [ -x "$NODE_BIN" ]; then
    exec "$NODE_BIN" "$SCRIPT_DIR/pre-commit.mjs"
  elif command -v node >/dev/null 2>&1; then
    exec node "$SCRIPT_DIR/pre-commit.mjs"
  fi
fi

# If no compiled script or no Node.js available, allow the commit (fail-open for DX)
exit 0
