#!/bin/bash
set -e

# Push CI images
function push_ci_image(){

  echo "Pushing ${REPONAME}/${IMGNAME}:${IMGTAG} ..."
  docker push ${REPONAME}/${IMGNAME}:${IMGTAG}

}

# Push Release Images
function push_release_image(){

  if [ ! -z "${CIRCLE_TAG}" ];
  then
    # Push with different tags if tagged as a release
    # When github is tagged with a release, then CircleCI will
    # set the release tag in env CIRCLE_TAG
    echo "Pushing ${REPONAME}/${IMGNAME}:${CIRCLE_TAG} ..."
    docker tag ${IMAGEID} ${REPONAME}/${IMGNAME}:${CIRCLE_TAG}
    docker push ${REPONAME}/${IMGNAME}:${CIRCLE_TAG}
    echo "Pushing ${REPONAME}/${IMGNAME}:latest ..."
    docker tag ${IMAGEID} ${REPONAME}/${IMGNAME}:latest
    docker push ${REPONAME}/${IMGNAME}:latest
  fi;

}

BUILD_TYPE=$(echo $1 | cut -d "=" -f 2)
REPONAME=$(echo $2 | cut -d "=" -f 2)
IMGNAME=$(echo $3 | cut -d "=" -f 2)
IMGTAG=$(echo $4 | cut -d "=" -f 2)

# Check for Image Details
if [ -z "${REPONAME}" ] || [ -z "${IMGNAME}" ] || [ -z "${IMGTAG}" ]
then
  echo "Image details are missing. Nothing to push.";
  exit 1
fi

IMAGEID=$( docker images -q ${REPONAME}/${IMGNAME}:${IMGTAG} )

# Verify Docker Credentials
if [ ! -z "${DNAME}" ] && [ ! -z "${DPASS}" ];
then
  docker login -u "${DNAME}" -p "${DPASS}";
  if [ "${BUILD_TYPE}" == "ci" ]; then
    push_ci_image;
  elif [ "${BUILD_TYPE}" == "release" ]; then
    push_release_image;
  else
    echo "Invalid build type"; exit 1
  fi
else
  echo "No docker credentials provided. Skip uploading ${REPONAME}/${IMGNAME}:${IMGTAG} to docker hub";
fi
