#!/bin/bash
# Generate GitVersion prerelease (or release) semantic version information.
#
# Basic ideas:
#   The 'release goal version' should be specified in the code manually, via package.json.
#      This must be greater-than the last released version, otherwise this must fail.
#   Released versions are specified with (notionally static) version tags.
#   Everybody uses SemVer, preferably with pre-release (section 9) and build metadata (section 10).
#   Releases are built from release/<release-goal-version>
#   Tags are added in the form v<release-version> after the goal is achieved
#

log () {
  local log_level="$1"
  local msg="$2"
  if [[ -z "$msg" ]]; then
    return 0
  fi
  echo -e "${log_level}  $msg"
}

count_from () {
  # Finds the commit we want to count up to
  # to get the sequence number.
  # Options:
  #    a<MMP>, a manually tagged shared base thing. TODO: automate this
  #    last branch to a release?
  local next_version=$1
  if [ $(git tag -l "v$next_version") ]; then
    log ERROR "Release has already been tagged [v$next_version]"
    exit 1
  fi
  if [ $(git tag -l "a$next_version") ]; then
    echo "a$last_version"
    return
  fi
  local latest=$(git tag | sort -r --version-sort | head -n1)
  if [ $latest ]; then
    git merge-base $latest HEAD
  else
    log ERROR "Unable to find latest `v` tag"
    exit 2
  fi
}

if [ -f buildkite-scripts/utils/logging.sh ]; then
  source buildkite-scripts/utils/logging.sh
fi


# Derive information from git
BRANCH_NAME=${BUILDKITE_BRANCH:-$(git rev-parse --abbrev-ref HEAD)}
TAG_NAME=$(git tag --points-at HEAD)
log INFO "BRANCH_NAME=[$BRANCH_NAME], TAG_NAME=[$TAG_NAME]"
if [[ $TAG_NAME == v*.*.* ]]; then
  log INFO "Forcing to [$TAG_NAME]"
  BRANCH_NAME="${TAG_NAME}"
fi

# I'd rather this be 'fishfood' or something, but it needs to sort 
# before alpha lexicographically. 
PRE_RELEASE_TAG=aleph
case "$BRANCH_NAME" in
   master)
     PRE_RELEASE_TAG=beta
     ;;
   release/*)
     PRE_RELEASE_TAG=rc
     ;;
   feature*)
     PRE_RELEASE_TAG=alpha
     ;;
   v*)
     PRE_RELEASE_TAG=rc
     ;;
esac


# Package Information
MMP_VER=$(node -p "require('./package.json').version")
SHARED_BASE=$(count_from ${MMP_VER})

# Build Information
BUILD_NUMBER=${BUILDKITE_BUILD_NUMBER:-0000}
SEQUENCE_NUMBER=$(git rev-list ${SHARED_BASE}.. --count || ${BUILD_NUMBER})
log INFO "MMP_VER=[${MMP_VER}] SHARED_BASE=[${SHARED_BASE}] SEQUENCE_NUMBER=[${SEQUENCE_NUMBER}]"

SEM_VER="${MMP_VER}-${PRE_RELEASE_TAG}.${SEQUENCE_NUMBER}"
INFO="sha.${BUILDKITE_COMMIT:-$(git rev-parse --short HEAD)}"
INFO_VER="${SEM_VER}+${INFO}"

cat << EOF
{
  "semantic-version": "${SEM_VER}",
  "informational-version": "${INFO_VER}",
  "mmp-version": "${MMP_VER}"
}
EOF

if [ $BUILDKITE ]; then
  buildkite-agent meta-data set "semantic-version" "$SEM_VER"
  buildkite-agent meta-data set "informational-version" "${INFO_VER}"
  buildkite-agent meta-data set "mmp-version" "${MMP_VER}"
  buildkite-agent annotate "Version $SEM_VER " --style "info" --context "ctx-version"
fi
