#!/usr/bin/env bash
# TAGS=("cinderlib_tag travis_tag ember_destination" ... )
# DOCKER_FILE contains the docker file that is to be used to build these images
TRAVIS_REPO='embercsi/travis-ci'
# TODO: move everything to embercsi
# EMBER_REPO='embercsi/ember-csi'
export EMBER_REPO='akrog/ember-csi'

# Not a tag (merge to master, PR, or branch)
if [ "$TRAVIS_BRANCH" != "$TRAVIS_TAG" ]; then
  export DOCKER_FILE='Dockerfile'

  sanitized_branch="${TRAVIS_BRANCH//\//_}"
  travis_tag="${sanitized_branch}-PR${TRAVIS_PULL_REQUEST}"
  TAGS=("master;$TRAVIS_REPO:master-${travis_tag};$EMBER_REPO:master;unstable" \
        "latest;$TRAVIS_REPO:latest-${travis_tag};$EMBER_REPO:latest;unstable" )

else
  export DOCKER_FILE='Dockerfile-release'

  releases=`curl -s https://raw.githubusercontent.com/Akrog/cinderlib/master/hooks/rdo-releases`
  cinderlib_pinned_stable_version=`cat hooks/cinderlib-pinned-stable-version`

  while read -r release; do
      cinderlib_tag="${release}-cl_${cinderlib_pinned_stable_version}"
      release_tag="ember_${TRAVIS_TAG}-${cinderlib_tag}"
      TAGS[$i]="${cinderlib_tag};$TRAVIS_REPO:$release_tag;$EMBER_REPO:$release_tag;$release"
      i=$((i + 1))
  done <<< "$releases"
fi

export TAGS="${TAGS[@]}"
echo "set-tags returns $DOCKER_FILE and ${TAGS[@]}"
