#!/usr/bin/env bash

# Deploys docker images for pre-processing pipeline and its launcher

set -euo pipefail

version=""
echo "Please only run this command from \`develop/clams\` or \`master\`"
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"
    if [[ "$version" =~ ^([0-9]*[.])*[0-9]*$ ]]; then
        echo "Pushing docker images for version: $version."
    else
        echo "Version should be formatted as \"integer.integer.....integer\")."
        exit 1
    fi
fi

# Build all in parallel to optimize build time.
bazel build --config=cuda \
//mapping/distributed_mapping/pipeline/integrated_clams_slam:integrated_clams_slam_pipeline_docker_push \
//mapping/distributed_mapping/pipeline/pre_processing:pre_processing_pipeline_docker_push \
//mapping/distributed_mapping/pipeline/all_mapping_products:all_mapping_products_pipeline_docker_push \
//mapping/distributed_mapping/pipeline/clams_slam:clams_slam_pipeline_docker_push \
//mapping/distributed_mapping/pipeline/scripts:pre_processing_launcher_docker_push \
//mapping/distributed_mapping/pipeline/scripts:integrated_clams_slam_launcher_docker_push \
--define clams_image_tag="$version"

# Push all docker images
bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/integrated_clams_slam:integrated_clams_slam_pipeline_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/pre_processing:pre_processing_pipeline_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/scripts:integrated_clams_slam_launcher_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/scripts:pre_processing_launcher_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/all_mapping_products:all_mapping_products_pipeline_docker_push \
--define clams_image_tag="$version"

bazel run --config=cuda \
//mapping/distributed_mapping/pipeline/clams_slam:clams_slam_pipeline_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."
