#!/usr/bin/env bash

set -euo pipefail

script_root="$( (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 "$script_root/../../Dockerfile.dockerignore" $root_dockerignore
# append ts proto dependencies to docker ignore
$git_root/spectrum/scripts/ts_proto_whitelist.py >> $root_dockerignore

# run proto_ui_gen
source $script_root/../proto_ui_gen/run.sh

pushd $git_root
(
  set -x
  docker build -f "$script_root/../../Dockerfile" -t $1 ./
)
popd
