#!/bin/bash

# Increase HEAP size to 8GB
export NODE_OPTIONS="--max-old-space-size=8192"

if [ $# -eq 0 ]
  then
    if [ -n "$PLUTO_DEBUG" ]; then
      node --debug index.js
    else
      #Handle preprod and staging cases
      if [ "$NODE_APP_INSTANCE" = "preprod" ]; then
        CURR_ENV=$NODE_APP_INSTANCE
      elif [ "$NODE_ENV" = "staging" ]; then
        CURR_ENV=$PLUTO_APP_ENV_$PLUTO_K8S_NAMESPACE
      else
        CURR_ENV=$NODE_ENV;
      fi

      if [ "$CURR_ENV" != "production" ]; then
        # Start app with Sealights
        /src/sealights/node_modules/.bin/slnodejs run \
          --tokenfile /src/sealights/sltoken.txt \
          --buildsessionidfile  /src/sealights/buildSessionId \
          --labid $PLUTO_APP_HOSTNAME \
          --workspacepath "/src/" \
          --useinitialcolor true \
          --useslnode2 -- "/src/index.js"
      else
        node index.js
      fi
    fi
  else
    case "$1" in
      test)
        npm test
        ;;
      *)
        npm run $1
    esac
fi

exit $?
