#!/bin/bash

VERSION=$1

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

echo "Downloading grafanactl-$VERSION..."

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

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

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

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

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

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

rm -rf grafanactl-$VERSION
rm -rf go-modules-grafanactl-$VERSION
