#!/usr/bin/env bash

set -euo pipefail

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

tag="$1"
image_name="docker-registry.zooxlabs.com/argus:$tag"

cwd="$( (builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) )"
git_root="$(git rev-parse --show-toplevel)"

root_dockerignore="$git_root/.dockerignore"
root_dockerignore_backup="$root_dockerignore-backup"

mv $root_dockerignore $root_dockerignore_backup

clean_up () {
    ARG=$?
    mv $root_dockerignore_backup $root_dockerignore
    exit $ARG
}

trap clean_up EXIT

cp "$cwd/Dockerfile.dockerignore" $root_dockerignore
# append ts proto dependencies to docker ignore
$git_root/spectrum/scripts/ts_proto_whitelist.py >> $root_dockerignore

pushd $git_root
(
  set -x
  docker build -f "$cwd/Dockerfile" ./ --tag "$image_name"
  docker push "$image_name"
)
popd

echo Success!
