#!/bin/bash

VERSION=$1

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

echo "Downloading shfmt-$VERSION..."

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

tar xf sh-$VERSION.tar.gz
mv sh-$VERSION shfmt-$VERSION
tar cf shfmt-$VERSION.tar shfmt-$VERSION
mkdir -p go-modules-shfmt-$VERSION

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

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

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

plzip -9 shfmt-$VERSION.tar

rm -f sh-$VERSION.tar.gz
rm -rf shfmt-$VERSION
rm -rf go-modules-shfmt-$VERSION
