#!/bin/bash

export SAS_BASE_URL="hostNameURLGoesHere"
export SAS_USER="SASAdministratorsUserNameGoesHere"
export SAS_PASSWORD="passwordGoesHere"

export OAUTH_RESPONSE_JSON=$(curl --request POST \
  --url "$SAS_BASE_URL/SASLogon/oauth/token" \
  --header "Accept: application/json" \
  --header "Authorization: Basic c2FzLmVjOg==" \
  --header "Content-Type: application/x-www-form-urlencoded" \
  --data grant_type=password \
  --data username=$SAS_USER \
  --data password=$SAS_PASSWORD \
  --silent);

unset SAS_USER;
unset SAS_PASSWORD;

# Extract access token
export OAUTH_TOKEN=$(echo "$OAUTH_RESPONSE_JSON" | jq --raw-output '.access_token');

# Exit if token is empty
if [[ -z "$OAUTH_TOKEN" || "$OAUTH_TOKEN" == "null" ]]; then
  echo "(x) Failed to retrieve access token. Exiting.";
  exit 1;
fi


# Create DEFAULT_STORE domain
echo;
echo "Update DEFAULT_STORE domain - PUT $SAS_BASE_URL/credentials/domains/DEFAULT_STORE";
echo;
echo;
curl -X PUT "$SAS_BASE_URL/credentials/domains/DEFAULT_STORE" \
     --header 'Accept: application/vnd.sas.credential.domain+json' \
     --header "Authorization: Bearer $OAUTH_TOKEN" \
     --header 'Content-Type: application/vnd.sas.credential.domain+json' \
     --data '{"id": "DEFAULT_STORE","type": "password"}';

echo;
echo;
echo;
echo "Update DEFAULT_STORE credential - PUT $SAS_BASE_URL/credentials/domains/DEFAULT_STORE/clients/sas.svi-datahub";
echo;
echo;
# Add client credentials to DEFAULT_STORE
curl -X PUT "$SAS_BASE_URL/credentials/domains/DEFAULT_STORE/clients/sas.svi-datahub" \
     --header "Authorization: Bearer $OAUTH_TOKEN" \
     --header 'Accept: application/vnd.sas.credential+json' \
     --header 'Content-Type: application/vnd.sas.credential+json' \
     --data '{"domainId": "DEFAULT_STORE", "identityType": "client", "identityId": "sas.svi-datahub", "domainType": "password", "properties": {"userId": "${postgres.username}"}, "secrets": {"password": "JHtwb3N0Z3Jlcy5wYXNzd29yZH0="}}';
echo;
echo;
echo;
echo "Update ALERT_STORE domain - PUT $SAS_BASE_URL/credentials/domains/ALERT_STORE";
echo;
echo;
## Create ALERT_STORE domain
curl -X PUT "$SAS_BASE_URL/credentials/domains/ALERT_STORE" \
     --header 'Accept: application/vnd.sas.credential.domain+json' \
     --header "Authorization: Bearer $OAUTH_TOKEN" \
     --header 'Content-Type: application/vnd.sas.credential.domain+json' \
     --data '{"id": "ALERT_STORE","type": "password"}';
echo;
echo;
echo;
echo "Update ALERT_STORE credential - PUT $SAS_BASE_URL/credentials/domains/ALERT_STORE/clients/sas.svi-datahub";
echo;
echo;
# Add client credentials to ALERT_STORE
curl -X PUT "$SAS_BASE_URL/credentials/domains/ALERT_STORE/clients/sas.svi-datahub" \
     --header "Authorization: Bearer $OAUTH_TOKEN" \
     --header 'Accept: application/vnd.sas.credential+json' \
     --header 'Content-Type: application/vnd.sas.credential+json' \
     --data '{"domainId": "ALERT_STORE", "identityType": "client", "identityId": "sas.svi-datahub", "domainType": "password", "properties": {"userId": "${postgres.username}"}, "secrets": {"password": "JHtwb3N0Z3Jlcy5wYXNzd29yZH0="}}';
echo;
echo;