#!/bin/sh
#
# Copyright The OpenZipkin Authors
# SPDX-License-Identifier: Apache-2.0
#

set -ue

# This script echos a `MAJOR.MINOR.PATCH` version tag based on..
#  * arg1: XXXXX- prefix
#  * arg2: XXXXX-MAJOR.MINOR.PATCH git trigger tag
#
# The script exits 1 if the prefix doesn't match. On success, the tag is deleted if it exists.
#
# Note: In CI, `build-bin/git/login_git` must be called before invoking this.

trigger_tag_prefix=${1?required. Ex docker- to match docker-1.2.3}
trigger_tag=${2?trigger_tag is required. Ex ${trigger_tag_prefix}1.2.3}

# Checking sed output to determine success as exit code handling in sed or awk is awkward
version=$(echo "${trigger_tag}" | sed -En "s/^${trigger_tag_prefix}([0-9]+\.[0-9]+\.[0-9]+)$/\1/p")

if [ -z "$version" ]; then
  >&2 echo invalid trigger tag: ${trigger_tag}
  exit 1;
fi

# try to cleanup the trigger tag if it exists, but don't fail if it doesn't
git tag -d ${trigger_tag} 2>&- >&- || true
git push origin :${trigger_tag} 2>&- >&- || true

echo $version
