#!/bin/sh
# gremlin: runs the bundled engine with the Node runtime shipped beside it (INS-01).
set -e
self=$0
while [ -L "$self" ]; do
  target=$(readlink "$self")
  case $target in
    /*) self=$target ;;
    *) self=$(dirname "$self")/$target ;;
  esac
done
engine=$(cd "$(dirname "$self")/.." && pwd)
node=${GREMLIN_NODE:-}
if [ -z "$node" ]; then
  # Standalone CLI, macOS app bundle, Linux packages and AppImage, in that order.
  for candidate in "$engine/bin/gremlin-node" "$engine/../../MacOS/gremlin-node" "$engine/../../../bin/gremlin-node"; do
    if [ -x "$candidate" ]; then node=$candidate; break; fi
  done
fi
if [ -z "$node" ]; then
  echo "gremlin: the bundled Node runtime is missing; reinstall Gremlin or set GREMLIN_NODE." >&2
  exit 1
fi
exec "$node" "$engine/gremlin.mjs" "$@"
