#!/bin/sh
# codev — developer CLI shipped inside the desktop app (decision record 023).
# Resolves its real location (following the /usr/local/bin symlink), then runs
# the app's own Electron binary as Node against the bundled CLI entry.
SELF="$0"
while [ -L "$SELF" ]; do SELF="$(readlink "$SELF")"; done
PAYLOAD_DIR="$(cd "$(dirname "$SELF")/../.." && pwd)"
# macOS bundle layout: payload = Contents/Resources/app, binary in Contents/MacOS.
# Linux tarball layout: <root>/codev is a Conveyor wrapper whose AppArmor
# userns check aborts on restricted systems (Ubuntu 24.04 default) — a GUI
# sandbox concern that does not apply to ELECTRON_RUN_AS_NODE, so prefer the
# real binary <root>/codev.bin. Linux deb layout: <root>/codev IS the binary.
if [ -x "$PAYLOAD_DIR/../../MacOS/codev" ]; then
  APP_BINARY="$PAYLOAD_DIR/../../MacOS/codev"
elif [ -x "$PAYLOAD_DIR/../../codev.bin" ]; then
  APP_BINARY="$PAYLOAD_DIR/../../codev.bin"
elif [ -x "$PAYLOAD_DIR/../../codev" ]; then
  APP_BINARY="$PAYLOAD_DIR/../../codev"
else
  echo "codev: no app layout found around $PAYLOAD_DIR — this link does not point into an installed codev app. Reinstall the command-line tools from the app's settings." >&2
  exit 1
fi
# Name the engine resources root explicitly: on Linux the binary sits next to
# Electron's OWN resources/ directory, which must never be taken for the sfp
# tree shipped beside the bundle (#2134).
exec env ELECTRON_RUN_AS_NODE=1 SFP_RESOURCES_DIR="$PAYLOAD_DIR/out/main/resources" "$APP_BINARY" "$PAYLOAD_DIR/out/main/cli.cjs" "$@"
