#!/bin/bash

# Export URL values for API gateways in the following system environment variables:
#   ADMIN_GATEWAY_URL
#   MOBILE_GATEWAY_URL
#   ROBOT_GATEWAY_URL
# Maestro environments are documented at
# https://confluence.zooxlabs.com/display/Maestro/Maestro+Environments
#
# Usage:
#   $ source set_gateway_urls.sh <maestro_env>
#
# $1 - Maestro environment.
#
# Examples
#   $ source set_gateway_urls.sh maestro-dev
#
# Notes
# ECS Zip environment is not supported as it lacks a few APIs essential for user and
# pairing. Zip environment will be merged with Beta in 20Q2.

if [ "$1" ]
then
  if [[ $1 == maestro-* ]]
  then
    EKS_ENV=${1:8}
    echo "Setting URLs for EKS env: $EKS_ENV"
    export ADMIN_GATEWAY_URL="https://admin-gateway.$EKS_ENV.maestro.zooxlabs.com"
    export MOBILE_GATEWAY_URL="https://mobile-gateway.$EKS_ENV.maestro.zooxlabs.com"
    export ROBOT_GATEWAY_URL="https://robot-gateway.$EKS_ENV.maestro.zooxlabs.com"
    return 0
  else
    if [ "$1" = "ecs-dev" ] || [ "$1" = "ecs-alpha" ] || [ "$1" = "ecs-stage" ] || [ "$1" = "ecs-beta" ]
    then
      ECS_ENV=${1:4}
      echo "Setting URLs for ECS env: $ECS_ENV"
      export ADMIN_GATEWAY_URL="https://admin-gateway-$ECS_ENV.zooxlabs.com"
      export MOBILE_GATEWAY_URL="https://dispatch-client-api-$ECS_ENV.zoox.com"
      export ROBOT_GATEWAY_URL="https://robot-gateway-$ECS_ENV.zooxlabs.com"
      return 0
    else
      echo Maestro environment \'$1\' is not supported.
      echo It should be one of these values:
      echo "  ECS: ecs-dev, ecs-alpha, ecs-stage, ecs-beta"
      echo "  EKS: maestro-dev, maestro-staging, maestro-prod, maestro-<your_ephemeral_env>"
      return 1
    fi
  fi
else
  echo "Usage: source set_gateway_urls.sh <maestro_env>"
  echo "maestro_env should be one of these values:"
  echo "  ECS: ecs-dev, ecs-alpha, ecs-stage, ecs-beta"
  echo "  EKS: maestro-dev, maestro-staging, maestro-prod, maestro-<your_ephemeral_env>"
  return 1
fi
