#!/usr/bin/env sh

set -e

# This script is a thin Docker wrapper around the existing `make` targets.
#
# To run one of those targets in a development container (such as `make test`),
# you would replace with `docker/make` (such as `docker/make test`).
#
# But first, you must run `docker/make ready` to prepare the container.
#
# The special `ready` pseudo-target is handled directly here,
# but all other targets are forwarded to `make` inside the container,
# with a build directory override so tha docker-based build files won't
# conflict with the build files being generated separately on the host.

if test "$*" = "ready"; then
  docker build --target dev --tag savi-dev .
	docker rm -f savi-dev || echo "the savi-dev container wasn't running"
	docker run --name savi-dev -v `pwd`:/opt/code -d --rm --memory 4g -p 8080 savi-dev tail -f /dev/null
	echo "the savi-dev container is ready!"
else
  # For some reason clang doesn't like it if we omit the "alpine" vendor
  # in the triple, where we'd otherwise use `[ARCH]-unknown-linux-musl`.
  # We also need to use `aarch64` instead of `arm64` here, if it applies...
  platform=$(docker exec savi-dev ./platform.sh | sed s/unknown/alpine/ | sed s/arm64/aarch64/)

  docker exec savi-dev make \
    BUILD=build-in-docker \
    MAKE_VAR_CACHE=.make-var-cache-in-docker \
    CLANG_TARGET_PLATFORM=$platform \
    "${@}"
fi
