#!/bin/sh
set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
APP_EXECUTABLE="${SCRIPT_DIR}/Paseo"

if [ -d "${SCRIPT_DIR}/../Resources" ]; then
  RESOURCES_DIR="${SCRIPT_DIR}/../Resources"
elif [ -d "${SCRIPT_DIR}/resources" ]; then
  RESOURCES_DIR="${SCRIPT_DIR}/resources"
else
  echo "Unable to locate Paseo resources directory next to ${SCRIPT_DIR}" >&2
  exit 1
fi

CLI_ENTRY="${RESOURCES_DIR}/app.asar/node_modules/@getpaseo/cli/dist/index.js"

if [ ! -x "${APP_EXECUTABLE}" ]; then
  echo "Bundled Paseo executable not found at ${APP_EXECUTABLE}" >&2
  exit 1
fi

if [ ! -f "${CLI_ENTRY}" ]; then
  echo "Bundled Paseo CLI entrypoint not found at ${CLI_ENTRY}" >&2
  exit 1
fi

exec env ELECTRON_RUN_AS_NODE=1 "${APP_EXECUTABLE}" "${CLI_ENTRY}" "$@"
