#!/usr/bin/env bash

echo "*******************************************"
echo "              Start post_push              "
echo "*******************************************"
echo

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"

# shellcheck source=./_common
. "$DIR/_common"

echoEnvVars

function postPush() {
  local php_version=$1
  local php_variant=$2
  local image_name

  checkForVersionVariant "$php_version" "$php_variant"

  image_name=$(getImageName "$php_version" "$php_variant")

  echo "Post-pusing for image: $image_name"

  # Is this a release?
  if isRelease; then
    # Set the short tags
    local short_tag="$DOCKER_REPO:$php_version-$php_variant"
    echo Pushing short tag: "$short_tag"
    docker tag "$image_name" "$short_tag"
    docker push "$short_tag"

    # Is this the latest PHP version/variant?
    if isLatest; then
      local latest_tag="$DOCKER_REPO:latest"
      echo Pushing latest tag: "$latest_tag"
      docker tag "$image_name" "$latest_tag"
      docker push "$latest_tag"
    fi
  fi
}

function isRelease() {
  local semver_regex="^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$"
  [[ $SOURCE_BRANCH =~ $semver_regex ]]
}

function isLatest() {
  local latest_version=7.3
  local latest_variant=cli
  [ "$php_version" == "$latest_version" ] && [ "$php_variant" == "$latest_variant" ]
}

loopVersionsVariants postPush

echo
echo "************************************************"
echo "                 End post_push"
echo "************************************************"
echo

exit 0
