#!/usr/bin/env bash
set -euo pipefail

fn_exists() {
  # appended double quote is an ugly trick to make sure we do get a string -- if $1 is not a known command, type does not output anything
  [ `type -t $1`"" == 'function' ]
}

if ! fn_exists vault; then
   echo "Creating mock vault function"
    vault(){
        echo "This Vault command should not be called in docker. If it is, something has gone wrong."
        exit 1
    }
    export -f vault
fi


# @NOTE: due to PingID restrictions, running dev mode requires having the services running locally
source ./environments/common.sh
HOST="${SERVICE_HOST:-localhost}" # set SERVICE_HOST if you are developing on a remote machine
# Set as default, this can be manually modified for production
fall_back="http://${HOST}:5004"
export FLEETDASH_GATEWAY_API_ENDPOINT="${FLEETDASH_GATEWAY_API_ENDPOINT:-$fall_back}"
export CYPRESS_FLEETDASH_GATEWAY_API_ENDPOINT="${CYPRESS_FLEETDASH_GATEWAY_API_ENDPOINT:-$FLEETDASH_GATEWAY_API_ENDPOINT}"
fall_back="http://${HOST}:8050/v1/"
export VUE_APP_HEALTH_API="${VUE_APP_HEALTH_API:-$fall_back}"
fall_back="http://${HOST}:8007/v1/"
export VUE_APP_ALLOCATION_API_ENDPOINT="${VUE_APP_ALLOCATION_API_ENDPOINT:-$fall_back}"
export CYPRESS_JWT_TESTING_TOKEN=${CYPRESS_JWT_TESTING_TOKEN:-$(vault read -field=token secret/fleetdash/jwt_testing_token)}
export NODE_ENV=CYPRESS
