#!/usr/bin/env bash
set -euo pipefail

export SCRIPT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
source "${SCRIPT_DIR}/src/main.sh"

sha256() {
   if command_exists sha256sum; then
      sha256sum
   elif command_exists shasum; then
      shasum -a 256
   elif command_exists openssl; then
      openssl dgst -sha256
   else
      echoerr "Unable to calculate sha256!"
      exit 43
   fi
}

header() {
   echo "$*"
   echo
}

cd "$SCRIPT_DIR"

header "git pull"
git pull

version="$(grep VERSION "${SCRIPT_DIR}/navi" | grep -Eo '[0-9\.]+')"
tag="v${version}"
tar="https://github.com/denisidoro/navi/archive/${tag}.tar.gz"
formula="/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/navi.rb"

header "Attempting to release version ${tag}..."
read -p "Press enter to continue"

header "git tag"
git tag -a "$tag" -m "$tag"

header "git push"
git push --follow-tags

header "rm formula"
rm "$formula" 2>/dev/null || true

header "tar sha256"
sha="$(curl -sL "$tar" | sha256 | awk '{print $1}')"

header "output"
echo "  url \"${tar}\""
echo "  sha256 \"${sha}\""