#!/usr/bin/env bash
# Build (unless --no-build), then run unit tests and the H2 example.
# Usage:
#   ./scripts/run.sh
#   ./scripts/run.sh --no-build
#   QCHEM_BUILD=../other-build ./scripts/run.sh --no-build

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
do_build=1
build_dir="${QCHEM_BUILD:-$ROOT/build}"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --no-build)
      do_build=0
      shift
      ;;
    -h|--help)
      echo "Usage: $0 [--no-build]"
      echo "  Builds Release in ./build (or \$QCHEM_BUILD), then runs qchem_tests and h2_sto3g."
      exit 0
      ;;
    *)
      echo "Unknown option: $1" >&2
      exit 1
      ;;
  esac
done

if [[ "$do_build" -eq 1 ]]; then
  "$ROOT/scripts/build.sh"
  build_dir="${ROOT}/build"
fi

for name in qchem_tests h2_sto3g; do
  exe="$build_dir/$name"
  if [[ ! -x "$exe" ]]; then
    echo "Missing executable: $exe" >&2
    echo "Run ./scripts/build.sh or set QCHEM_BUILD to your CMake build tree." >&2
    exit 1
  fi
done

echo "=== qchem_tests ==="
"$build_dir/qchem_tests"
echo "=== h2_sto3g ==="
"$build_dir/h2_sto3g"
