#!/usr/bin/env bash
# Build and run the exporter against the disc.
#
#   build-export              build only
#   build-export --run        build, export to ./export, then validate it
#
# The validate step is not optional politeness: `export` writes a tree and
# `check` is the only thing that says the tree is readable by anything other
# than the program that wrote it. A build that exports and does not check has
# not shown anything.
#
# Jobs are capped: this box runs two agent containers and a desktop, and an
# unbounded parallel build has crashed it. Do not raise this to "use all cores".
set -euo pipefail
cd "${PROJECT_DIR:-/work}"
export CARGO_BUILD_JOBS="${CARGO_BUILD_JOBS:-3}"
cargo build --release -p sylpheed-export
if [ "${1:-}" = "--run" ]; then
  shift
  disc="${SYLPHEED_DISC:?set SYLPHEED_DISC to the extracted disc root}"
  "$CARGO_TARGET_DIR/release/sylpheed-export" export --disc "$disc" --out export "$@"
  exec "$CARGO_TARGET_DIR/release/sylpheed-export" check --out export
fi
