#!/usr/bin/env bash
#
# Build the ingest_pipeline_base image with a specific version
# Then tag it with an environment
#
# Example: ./build_and_push_py3_base_image.sh prod 17

set -exuo pipefail

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

if [[ ! -v ZOOX_WORKSPACE_ROOT ]]; then
  echo "$0 must be run from within a ZOOX_WORKSPACE context"
  echo "   ZOOX_WORKSPACE_ROOT environment variable not set"
  exit 1
fi

environ="$1"
version="$2"
image_name_version="ingest.docker.zooxlabs.com/ingest_pipeline_py3_base:$version"
image_name_environ="ingest.docker.zooxlabs.com/ingest_pipeline_py3_base:$environ"
dir="ingest_pipeline_py3_base"

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

sh _get_files_for_base.sh $dir

function cleanup {
  sh _cleanup_for_base.sh $dir
}
trap cleanup EXIT

(
  docker build $dir --tag "$image_name_version" --tag "$image_name_environ"
  docker push "$image_name_version"
  docker push "$image_name_environ"
  ${ZOOX_WORKSPACE_ROOT}/docker_images/bazel_package/create.sh \
    ingest.docker.zooxlabs.com "$dir" "$version" "$dir"

)

echo Success!
