#!/bin/bash
# Glue CLI wrapper - uses Electron's bundled Node.js

# Get the actual location of this script (following symlinks)
# This is important because Homebrew creates a symlink in /opt/homebrew/bin
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do
  DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# Navigate to app structure
APP_RESOURCES="$SCRIPT_DIR/../Resources"
ELECTRON_BIN="$SCRIPT_DIR/Glue"
CLI_ENTRY="$APP_RESOURCES/cli/dist/cli.js"

# Check if Electron binary exists
if [ ! -f "$ELECTRON_BIN" ]; then
  echo "Error: Electron binary not found at $ELECTRON_BIN"
  exit 1
fi

# Check if CLI entry exists
if [ ! -f "$CLI_ENTRY" ]; then
  echo "Error: CLI entry point not found at $CLI_ENTRY"
  exit 1
fi

# Run Electron as Node.js using ELECTRON_RUN_AS_NODE
# This makes Electron run the script with its embedded Node.js runtime
export ELECTRON_RUN_AS_NODE=1
exec "$ELECTRON_BIN" "$CLI_ENTRY" "$@"
