#!/usr/bin/env bash
# prepublish_go 1.4.1
# Tidy up and run checks before publishing Go pkg

set -euo pipefail  # Fail immediately on any error
Gre='\e[1;32m' Red='\e[1;31m' Mag='\e[1;35m' Yel='\e[1;33m' Blu='\e[1;34m' Rst='\e[0m'

# ==== main
Prg=`head -1 go.mod | awk -F'/' '{print $NF}' | awk '{print $NF}'`
case "$OSTYPE" in
    "linux-gnu"* ) printf "==> Linux\n" ;;
    "darwin"* )    printf "==> macOS\n" ;;
    "msys"* )      printf "==> Windows with GitBASH\n" ;;
    * )            printf "==> Unknown OS '$OSTYPE'. Aborting.\n" && exit 1 ;;
esac

printf "==> See publishing info at https://go.dev/doc/modules/publishing (Not running list cmd to make mod fully available.)\n"
printf "==> And versioning info at https://go.dev/doc/modules/version-numbers\n"

echo "==> go mod tidy"
go mod tidy
echo "==> go fmt"
go fmt
echo "==> go test ./..."
go test ./...
echo "==> Installing/running staticcheck ./..."
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...

printf "\n==> grep replace go.mod:\n$Red$(grep replace go.mod)$Rst"
printf "\n==> Last 5 tag versions:\n"
git tag | sort -V | tail -5
CurrentTag=`git tag | sort -V | tail -1`
printf "\n==> To publish, do below one-liner, advancing Tag version:\n\n"
printf "    Tag=$CurrentTag && git add . && git commit -m \"x updates\" && git tag \$Tag && git push origin \$Tag && git push\n\n"

exit 0
