#!/bin/bash
PACKAGE_NAME="fourq"
PACKAGE_BIN_NAME="fourq"

NPM_BIN_DIR=$(npm_config_yes=true npx which ${PACKAGE_BIN_NAME})
if [ -z "${NPM_BIN_DIR}" ]; then
  NPM_BIN_DIR=$(npm_config_yes=true npx which npm)
  NPM_DIR=${NPM_BIN_DIR::${#NPM_BIN_DIR}-8}
  PACKAGE_DIR="$NODE_MOULES_DIR/${PACKAGE_NAME}"
  if [ ! -d "${PACKAGE_DIR}" ]; then
    echo "ERROR: unable to locate command '${PACKAGE_BIN_NAME}' (package_name: ${PACKAGE_NAME})"
    exit 1
  fi
  NODE_MOULES_DIR="$NPM_DIR/lib/node_modules"
else
  CUTOFF=$(echo 19 + ${#PACKAGE_BIN_NAME} | bc)
  NPM_DIR=${NPM_BIN_DIR::${#NPM_BIN_DIR}-${CUTOFF}}
  NODE_MOULES_DIR="$NPM_DIR/node_modules"
  PACKAGE_DIR="$NODE_MOULES_DIR/${PACKAGE_NAME}"
fi

export NODE_OPTIONS=--max_old_space_size=262144
node --enable-source-maps --max-old-space-size=262144 "$PACKAGE_DIR/main.js" "$@"
