#!/usr/bin/env bash

# Deploys docker images for:
# 1. calibration pipeline (ingest)
# 2. validation pipeline (ingest)
# 3. launcher script (ingest)

set -euo pipefail

version=""

if [ "$#" == 0 ]; then
    echo "No version was specified in the command-line"\
         "(version should be formatted as \"(a.b.c.d....)\")."
    echo "Setting the version (tag) automatically using date/time:"\
         "YY.mm.dd.HH.MM.SS (24-hour convention)."
    version=`date +"%Y.%m.%d.%H.%M.%S"`
    echo "Version is set to: $version."
else
    version="$1"
    echo "Pushing docker images for version: $version."
fi

# Build all in parallel to optimize build time.
bazel build --config=cuda \
//calibration/pipelines:calibration_pipeline_docker_push \
//calibration/pipelines/validation:ingest_validation_pipeline_docker_push \
//calibration/pipelines:calibration_pipeline_no_cameras_docker_push \
//calibration/pipelines/scripts:launcher_docker_push \
--define clams_image_tag="$version"

# Push all docker images
bazel run --config=cuda \
//calibration/pipelines:calibration_pipeline_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//calibration/pipelines/validation:ingest_validation_pipeline_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//calibration/pipelines:calibration_pipeline_no_cameras_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//calibration/pipelines/scripts:launcher_docker_push \
--define clams_image_tag="$version"

echo "Done. All images have the $version tag, except for the launcher image,"\
     "which has the \":latest\" tag, but encodes the version inside an"\
     "environment variable."
