#!/usr/bin/env bash
#
# Build and push one of the ingest jobs

set -euo pipefail

function usage() {
  echo "Usage: $0 <image> <environ> <version>"
}

for arg in "$@"; do
  if [ "$arg" == --help ] || [ "$arg" == -h ]; then
    usage
    exit 0
  fi
done
if [ $# -ne 3 ]; then
  usage >&2
  exit 1
fi
for arg in "$@"; do
  if [ -z "$arg" ]; then
    usage >&2
    exit 1
  fi
done

image="$1"
environ="$2"
version="$3"

# Change directories to the top of the workspace
builtin cd "$(dirname "${BASH_SOURCE[0]}")"
builtin cd "$(bazel info workspace 2>/dev/null)"

# Pull base images
data/ingest/docker_images/ingest_pipeline_base/pull.sh >/dev/null
data/ingest/docker_images/ingest_pipeline_py3_base/pull.sh >/dev/null
data/ingest/docker_images/ingest_pipeline_py3_nodejs_base/pull.sh >/dev/null

bazel run \
  --define="${image}_version_tag=$version" \
  --define="${image}_environ_tag=$environ" \
  //data/ingest/docker_images:deploy_$image
