#!/bin/bash
# How to use this file TF_VAR_vmid_base=3550 TF_VAR_lxc_count=4 ./run.sh apply -auto-approve
set -e

cd "$(dirname "$0")"

# Forward all TF_VAR_* environment variables into the container
TF_VAR_ARGS=""
while IFS='=' read -r name _; do
  TF_VAR_ARGS="$TF_VAR_ARGS -e $name"
done < <(env | grep '^TF_VAR_')

case "${1:-plan}" in
  init|validate|plan|apply|destroy)
    docker compose run --rm $TF_VAR_ARGS terraform "$1" "${@:2}"
    ;;
  shell)
    docker compose run --rm $TF_VAR_ARGS terraform sh
    ;;
  *)
    echo "Usage: $0 [init|plan|apply|destroy|shell]"
    exit 1
    ;;
esac
