#!/bin/bash

VERSION=$1

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

echo "Downloading opentofu-$VERSION..."

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

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

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

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

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

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

rm -rf opentofu-$VERSION
rm -rf go-modules-opentofu-$VERSION
