#!/bin/sh
# OpenClaw CLI — managed by ClawX
# Do not edit manually. Regenerated on ClawX updates.

# Resolve the real path of this script (follow symlinks)
SCRIPT="$0"
while [ -L "$SCRIPT" ]; do
  SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)"
  SCRIPT="$(readlink "$SCRIPT")"
  [ "${SCRIPT#/}" = "$SCRIPT" ] && SCRIPT="$SCRIPT_DIR/$SCRIPT"
done
SCRIPT_DIR="$(cd -P "$(dirname "$SCRIPT")" && pwd)"

if [ "$(uname)" = "Darwin" ]; then
  # macOS: <App>.app/Contents/Resources/cli/openclaw
  # SCRIPT_DIR = .../Contents/Resources/cli
  CONTENTS_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")"
  ELECTRON="$CONTENTS_DIR/MacOS/ClawX"
  CLI="$CONTENTS_DIR/Resources/openclaw/openclaw.mjs"
else
  # Linux: /opt/ClawX/resources/cli/openclaw
  # SCRIPT_DIR = .../resources/cli
  INSTALL_DIR="$(dirname "$(dirname "$SCRIPT_DIR")")"
  ELECTRON="$INSTALL_DIR/clawx"
  CLI="$INSTALL_DIR/resources/openclaw/openclaw.mjs"
fi

if [ ! -f "$ELECTRON" ]; then
  echo "Error: ClawX executable not found at $ELECTRON" >&2
  echo "Please reinstall ClawX or remove this script: $0" >&2
  exit 1
fi

case "$1" in
  update)
    echo "openclaw is managed by ClawX (bundled version)."
    echo ""
    echo "To update openclaw, update ClawX:"
    echo "  Open ClawX > Settings > Check for Updates"
    echo "  Or download the latest version from https://claw-x.com"
    echo ""
    ELECTRON_RUN_AS_NODE=1 "$ELECTRON" "$CLI" --version 2>/dev/null || true
    exit 0
    ;;
esac

export OPENCLAW_EMBEDDED_IN="ClawX"
ELECTRON_RUN_AS_NODE=1 exec "$ELECTRON" "$CLI" "$@"
