#!/bin/bash

set -e

ACCOUNT=$1

case "$ACCOUNT" in
        production)
            ENVIRONMENTS=(prod)
            ;;
        *)
            echo "Unknown account \"$ACCOUNT\", exiting"
            exit 1
esac

for ENVIRONMENT in "${ENVIRONMENTS[@]}"
do
    ROLE=`terraform output -state=./terraform/${ACCOUNT}/.terraform/terraform.tfstate ${ENVIRONMENT}-iam_role_arn`

    tcs sandstorm role-flags \
        --owner team-devtools \
        --name devtools-${ENVIRONMENT}-eb-service-role \
        --secret_key "devtools/eb-service-role/${ENVIRONMENT}/*" \
        --allowed_arn "${ROLE}" \
        --output ./sandstorm.d/${ACCOUNT}_${ENVIRONMENT}.json

done
