#!/usr/bin/env bash
#
# Get an existing old docker image and tag it
# with a given environment
#
set -exuo pipefail

if [[ -z "${1:-}" || "$1" == '--help' || "$1" == '-h' ]]; then
  echo "Usage: $0 IMAGE ENVIRONMENT OLD_VERSION"
  exit 1
fi

cd "$( (builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) )"

image="$1"; shift
environment="$1"; shift
target_version="$1"; shift
# Set the target name to the image name
target_name="${image}"

# Docker tag fragments
root_path="data/ingest/docker_images"
dest_path="ingest.docker.zooxlabs.com/${root_path}"

# Getting image with the desired version
docker pull "${dest_path}/${target_name}:${target_version}"

# Saying that its 'environ'
docker tag "${dest_path}/${target_name}:${target_version}" "${dest_path}/${target_name}:${environment}"

# Pushing to registry
docker push "${dest_path}/${target_name}:${environment}"

echo "Done!"
