#!/bin/bash

VERSION=$1

rm -rf terraform-*.tar.?z go-modules-terraform*

echo "Downloading terraform-$VERSION..."

wget "https://github.com/hashicorp/terraform/archive/v$VERSION/terraform-$VERSION.tar.gz"

tar xf terraform-$VERSION.tar.gz
mkdir -p go-modules-terraform-$VERSION

cd terraform-$VERSION
  export GOPATH=$(realpath ../go-modules-terraform-$VERSION)
  go mod download
cd ..

cd go-modules-terraform-$VERSION/pkg/mod/cache/
  mv download go-modules-terraform-$VERSION
  tar cf ../../../../go-modules-terraform-$VERSION.tar go-modules-terraform-$VERSION
cd ../../../../

plzip -9 go-modules-terraform-$VERSION.tar

gunzip terraform-$VERSION.tar.gz
plzip -9 terraform-$VERSION.tar

rm -rf terraform-$VERSION
rm -rf go-modules-terraform-$VERSION
