#!/usr/bin/env bash
set -euo pipefail

# todo: support container-build time module building
#
# deploy.sh <repo> <ssh-password>

ecr_repo="069289935426.dkr.ecr.us-east-1.amazonaws.com"
docker_tmp_dir="/tmp/docker-$1/"
# todo: allow for versioning?
container_tag="$ecr_repo/$1"

mkdir "$docker_tmp_dir"
cat > "$docker_tmp_dir/Dockerfile" <<EOF
  FROM $ecr_repo/$1
  WORKDIR /home/zoox/$1
  RUN sudo apt-get install apt-transport-https time
  RUN eval "\$(ssh-agent -s)" && \
       sshpass -p$2 ssh-add \$HOME/.ssh/github_password && \
       git remote update && \
       git checkout -B master origin/master
  RUN /home/zoox/driving/scripts/setup/setup.sh -f
  RUN /usr/bin/bazel build ...
EOF

docker build --force-rm --no-cache --tag "$container_tag" "$docker_tmp_dir"
eval $(aws ecr get-login --region=us-east-1)
docker push "$container_tag"

rm --recursive --force "$docker_tmp_dir"
