#!/bin/bash

# Get the full tag of latest ESP image for use in terraform.
# This is required as the gcloud_build_image script tags its built image with
# the version number.
# Optional argument ./esp_full_version <ESP_TAG>
# which is equivalent to setting the -v flag of gcloud_build_image.

set -e

BASE_IMAGE_NAME="gcr.io/endpoints-release/endpoints-runtime-serverless"

function error_exit() {
  # ${BASH_SOURCE[1]} is the file name of the caller.
  echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${1:-Unknown Error.} (exit ${2:-1})" 1>&2
  exit ${2:-1}
}

# Use 1st command line arg as ESP_TAG, default to "2" as in gcloud_build_image
ESP_TAG=${1:-"2"}

ALL_TAGS=$(gcloud container images list-tags "${BASE_IMAGE_NAME}" \
    --filter="tags~^${ESP_TAG}$" \
    --format="value(tags)")
IFS=',' read -ra TAGS_ARRAY <<< "${ALL_TAGS}"

if [ ${#TAGS_ARRAY[@]} -eq 0 ]; then
error_exit "Did not find ESP version: ${ESP_TAG}"
fi;

# Find the tag with the longest length.
ESP_FULL_VERSION=""
for tag in "${TAGS_ARRAY[@]}"; do
    if [[ ! "${tag}" =~ ^[0-9] ]]; then
        continue
    fi
    if [ ${#tag} -gt ${#ESP_FULL_VERSION} ]; then
        ESP_FULL_VERSION=${tag}
    fi
done

# Produce JSON object containing esp_full_version
cat <<END
{"esp_full_version": "${ESP_FULL_VERSION}"}
END
