#!/usr/bin/env bash

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly ROOT
readonly IMAGE="hugo-chiri-sec-02"
readonly RESULTS="${ROOT}/results"

cd "${ROOT}"
docker build --tag "${IMAGE}" .
if [[ "${RESULTS}" != "${ROOT}/results" ]]; then
  echo "refusing to clear an unexpected results directory" >&2
  exit 65
fi
rm -rf "${RESULTS}"
mkdir -p "${RESULTS}"

docker run --rm \
  --cpus=2 \
  --memory=512m \
  --network=none \
  --volume "${RESULTS}:/experiment/results" \
  "${IMAGE}"
docker run --rm \
  --cpus=2 \
  --memory=512m \
  --network=none \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint php \
  "${IMAGE}" verify-profile.php
docker run --rm \
  --cpus=2 \
  --memory=512m \
  --network=none \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint sh \
  "${IMAGE}" -lc \
  'php decision-record.php && php verify-decisions.php'
docker run --rm \
  --cpus=2 \
  --memory=512m \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint php \
  "${IMAGE}" source-evidence.php
docker run --rm \
  --cpus=2 \
  --memory=512m \
  --network=none \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint sh \
  "${IMAGE}" -lc \
  'php source-manifest.php && php mutate.php && php verify-results.php'
