#!/bin/sh
# cs CLI launcher (generated by the CodeS desktop app).
# Runs the bundled cs command using the CodeS runtime.
# No separate Node.js install is required.

# Resolve this script's real location; PATH exposure installs a symlink to it
# (readlink -f is not portable to macOS, hence the loop).
SOURCE=$0
while [ -h "$SOURCE" ]; do
  DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
  SOURCE=$(readlink "$SOURCE")
  case $SOURCE in
    /*) ;;
    *) SOURCE="$DIR/$SOURCE" ;;
  esac
done
BIN_DIR=$(cd -P "$(dirname "$SOURCE")" >/dev/null 2>&1 && pwd)
APP_DIR=$(dirname "$BIN_DIR")

CLI_ENTRY="$APP_DIR/dist/cli.js"
# The CodeS executable sits at a fixed location relative to the app directory:
# macOS <bundle>/Contents/Resources/app -> ../../MacOS/code-study;
# Linux <install>/resources/app -> ../../code-study.
for CANDIDATE in "$APP_DIR/../../MacOS/code-study" "$APP_DIR/../../code-study"; do
  if [ -x "$CANDIDATE" ]; then
    export ELECTRON_RUN_AS_NODE=1
    exec "$CANDIDATE" "$CLI_ENTRY" "$@"
  fi
done
echo "cs: could not find the CodeS runtime next to $APP_DIR" >&2
exit 1
