#!/bin/sh
# dev [command...]: run a command in the fork's development image (scripts/dev.Dockerfile), in hax/, as you.
# With no command: make tests. Examples:
#   ./dev                       build hax/build/hax for this machine and run every test
#   ./dev make lint             clang-format, the style script, clang-tidy
#   BUILD_DIR=build-asan ./dev  the tests under AddressSanitizer; build-tsan for ThreadSanitizer
#   ./dev scripts/check.sh test e2e/repl_steer
#   ./dev sh                    a shell there (tmux, fzf and less are in the image)
# The image is built on first use; `docker rmi hax-dev` makes the next run build it again.
set -e
H=$(cd "$(dirname "$0")" && pwd)
docker image inspect hax-dev >/dev/null 2>&1 || {
  ctx=$(mktemp -d)
  cp "$H/hax/scripts/install_deps.sh" "$ctx/"
  docker build -q -t hax-dev -f "$H/scripts/dev.Dockerfile" "$ctx" >&2
  rm -rf "$ctx"
}
[ $# -gt 0 ] || set -- make tests
# ThreadSanitizer needs a fixed address space layout, and Docker's default seccomp profile refuses the call
# that sets one.
tsan=
if [ "$BUILD_DIR" = build-tsan ]; then
  tsan="--security-opt seccomp=unconfined"
  set -- setarch -R "$@"
fi
tty=; [ -t 0 ] && [ -t 1 ] && tty=-t
exec docker run --rm --init -i $tty $tsan --memory=8g -u "$(id -u):$(id -g)" -e HOME=/tmp -e TERM="${TERM:-xterm-256color}" \
  -e BUILD_DIR -e HAX_PROVIDER -e HAX_MOCK_SCRIPT \
  -v "$H/hax:$H/hax" -w "$H/hax" hax-dev "$@"
