#!/bin/bash -ex
#
# script/brew_tap_update
# mas
#
# Updates mas custom tap formula:
# https://github.com/mas-cli/homebrew-tap/blob/main/Formula/mas.rb
#

function usage {
  echo "Usage: brew_tap_update [-d] v0.0 [sha1_hash]"
  echo "  -d option enables dry run mode"
  echo "  version will be inferred using version script if not provided"
  echo "  sha will be inferred from the current commit if not provided"
  exit 1
}

# Max 3 arguments
if [[ $# -gt 3 ]]; then
  usage 1>&2
fi

# arg 1 - version tag
if [[ -n "${1}" ]]; then
  MAS_VERSION="${1}"
else
  MAS_VERSION="v$(script/version)"
fi

echo "MAS_VERSION: ${MAS_VERSION}"

# arg 2 - revision (commit hash)
if [[ -n "${2}" ]]; then
  REVISION="${2}"
else
  # Derive revision from version. Fails if MAS_VERSION is not a tag.
  REVISION=$(git rev-parse "${MAS_VERSION}")
fi

echo "REVISION: ${REVISION}"

# Build in mas project
script/bottle
