#!/usr/bin/env bash

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly ROOT
readonly IMAGE="hugo-chiri-lar-05"
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=1g \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" profile.mjs

docker run --rm \
  --cpus=2 \
  --memory=1g \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" lifecycle.mjs

docker run --rm \
  --cpus=2 \
  --memory=1g \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" verify.mjs

mkdir -p "${RESULTS}/environment"
docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  "${IMAGE}" sh -lc \
  'composer audit --locked --no-interaction > results/environment/composer-audit.txt 2>&1'

docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" source-evidence.mjs

docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" compatibility.mjs

docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" adoption-record.mjs

docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint node \
  "${IMAGE}" mutate.mjs

docker run --rm \
  --volume "${RESULTS}:/experiment/results" \
  --entrypoint sh \
  "${IMAGE}" -lc 'node source-manifest.mjs && node verify-results.mjs'
