#!/usr/bin/env bash
set -euo pipefail

SOURCE="${BASH_SOURCE[0]}"
while [ -L "$SOURCE" ]; do
  DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
  SOURCE="$(readlink "$SOURCE")"
  [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SOURCE")" && pwd)"
SMCCLAW_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
export SMCCLAW_INSTALL_PREFIX="${SMCCLAW_INSTALL_PREFIX:-$SMCCLAW_ROOT}"

NODE="${SMCCLAW_NODE:-$SMCCLAW_ROOT/runtime/node}"

if [ ! -x "$NODE" ]; then
  echo "smcclaw: Node.js runtime not found at $NODE" >&2
  echo "  Set SMCCLAW_NODE to point to a Node.js >= 22.12 binary." >&2
  exit 1
fi

export NODE_ENV="${NODE_ENV:-production}"

exec "$NODE" --disable-warning=ExperimentalWarning \
  "$SMCCLAW_ROOT/lib/smcclaw.mjs" "$@"
