#!/bin/bash

set -euo pipefail

v=0.52.4

basedir="${0%/*}"
self="${0##*/}"

s=$(uname -s)
case "$s"
in
  Darwin) s=darwin ;;
  Linux) s=linux ;;
  *) echo "Unsupported system: $s" >/dev/stderr ;;
esac

m=$(uname -m)
case "$m"
in
  x86_64) m=amd64 ;;
  arm64) ;;
  *) echo "Unsupported machine: $m" >/dev/stderr ;;
esac

url=https://github.com/gruntwork-io/terragrunt/releases/download/v${v}/terragrunt_${s}_${m}
bin="$basedir/.tmp/$v/$self"

if [[ ! -x "$bin" ]]
then
  mkdir -p $(dirname "$bin")
  wget -O "$bin" "$url"
  chmod +x $bin
fi

exec "$bin" "${@}"