#!/usr/bin/env bash

set -euo pipefail

current_directory="$(pwd)"
terraform_directory="${ZOOX_WORKSPACE_ROOT}/infra/terraform"

cleanup() {
    # restore the working directory
    cd ${current_directory}
}

trap cleanup EXIT

function deploy() {
    terraform init
    list="$(terraform workspace list | grep production)"
    if [[ ${list} != *"production"* ]]; then
        terraform workspace new production
    fi
    terraform workspace select production
    terraform apply -auto-approve=false
}

cd ${terraform_directory}/hwsd_gateway
deploy

cd ${terraform_directory}/hwsd_test-2
deploy

cd ${terraform_directory}/hwsd_station-2
deploy

cd ${terraform_directory}/hwsd_component-2
deploy

# NOTE(INFRA-9161) - Script will error here due to resource not cleaned up
cd ${terraform_directory}/hwsd_run-2
deploy

cd ${current_directory}
