#!/bin/bash -eu

echo ".: BEGIN $0 rev ${BITBUCKET_COMMIT:-undefined} :."

# export AUTH0_CLIENT_ID_FOR_IDENTITY=$(aws ssm get-parameters --names "/auth0/identity-service/client-id" --query "Parameters[0].Value" | tr -d '"')
[[ ! -z ${AUTH0_CLIENT_ID_FOR_IDENTITY:+true} ]] || {
  echo "ERROR: got empty or unset trying to set AUTH0_CLIENT_ID_FOR_IDENTITY from AWS SSM Parameter Store"
  exit 2
}

# export AUTH0_CLIENT_SECRET_FOR_IDENTITY=$(aws secretsmanager get-secret-value --secret-id identity-service-client-secret --query "SecretString" | tr -d '"')
[[ ! -z ${AUTH0_CLIENT_SECRET_FOR_IDENTITY:+true} ]] || {
  echo "ERROR: got empty or unset trying to set AUTH0_CLIENT_SECRET_FOR_IDENTITY from AWS SSM Parameter Store"
  exit 3
}

#[[ ! -z ${MYSQL_PASS:+true} ]] || {
#  export MYSQL_PASS=$(credstash get mysql_pass_identity_server)
  [[ ! -z ${MYSQL_PASS:+true} ]] || {
    echo "ERROR: got empty or unset trying to set MYSQL_PASS from credstash"
    exit 5
  }
#}

# export RSA_PRIVATE_KEY=$(credstash get rsa_key_identity_server)
[[ ! -z ${RSA_PRIVATE_KEY:+true} ]] || {
  echo "ERROR: got empty or unset trying to set RSA_PRIVATE_KEY from credstash"
  exit 1
}

# [[ ! -z ${HUBSPOT_LOGIN:+true} ]] || {
#  export HUBSPOT_LOGIN=$(credstash get hubspot_login)
  [[ ! -z ${HUBSPOT_LOGIN:+true} ]] || {
    echo "WARN: failed to get HUBSPOT_LOGIN from credstash"
  }
# }

exec "$@"
