#!/bin/bash

root_path="data/ingest/docker_images"
dest_path="ingest.docker.zooxlabs.com/${root_path}"
image="$1"
prod_image="${dest_path}/${image}:prod"
local_image="bazel/${root_path}:${image}"

sh "${ZOOX_WORKSPACE_ROOT}/data/ingest/docker_images/autoupdate/check_not_volatile.sh" "${image}"
check_res=$?
if [ $check_res -ne 0 ]; then
  echo "Volatile: ${image}"
  exit 0
fi

# Building locally & getting last layer digest
bazel_run "//${root_path}:${image}" -- --norun > /dev/null
retVal=$?
if [ $retVal -ne 0 ]; then
    echo "Error: $image"
    exit $retVal
fi
cur_layer=$(docker inspect $local_image |
    python "${ZOOX_WORKSPACE_ROOT}/data/ingest/docker_images/autoupdate/last_layer_from_inspect_output.py")

# Pulling prod image & getting last layer digest
docker pull $prod_image > /dev/null
prod_layer=$(docker inspect $prod_image |
    python "${ZOOX_WORKSPACE_ROOT}/data/ingest/docker_images/autoupdate/last_layer_from_inspect_output.py")

if [ "$cur_layer" != "$prod_layer" ]; then
  echo "Changed: $image"
else
  echo "Not changed: $image"
fi
