#!/usr/bin/env bash

# Run Helper script for running test buckets across different CI systems.
#
# The CI tests for rocket-chip are split up into a number of different buckets
# in order to parallelize the test suite. The buckets are each assigned a
# number, starting with 1. To edit the buckets, simply edit the main `case`
# statement below.

set -euo pipefail

print_usage() {
  echo "Usage: ${0} bucket-number"
}

if [[ $# -ne 1 ]]; then
  echo "Missing required argument: test bucket number" >&2
  print_usage
  exit 1
fi

regression_dir=$(cd "$(dirname "$0")" ; pwd -P)
rocketchip_dir=$(dirname "${regression_dir}")

if [[ ${TRAVIS:-false} == true ]]; then
  # If running on Travis CI, make sure that travis_wait is defined.
  . "${regression_dir}/travis_whereami.bash"
  . "${regression_dir}/travis_setup_env.bash"
  . "${regression_dir}/travis_jigger.bash"
  . "${regression_dir}/travis_wait.bash"
  set +u
  travis_setup_env
  set -u
else
  # If not running on Travis CI, Stub out the travis_wait to be a no-op, since
  # it's unnecessary and it has some undesirable side effects such as hiding
  # console output until the command finishes.
  travis_wait() {
    # Throw out first argument, which is the wait time in minutes.
    shift
    # Run the remaining arguments as a normal command.
    "$@"
  }
fi

# Always run from the root rocket-chip directory.
cd "${rocketchip_dir}"

# Test bucket definitions
bucket_number=$1
set -x
case "${bucket_number}" in
  1)
    # Temporarily disable this bucket, which is hitting OOM on Actions
    #travis_wait 100 make emulator-ndebug -C regression SUITE=UnittestSuite JVM_MEMORY=3G VERILATOR_THREADS=1
    #travis_wait 100 make emulator-regression-tests -C regression SUITE=UnittestSuite JVM_MEMORY=3G VERILATOR_THREADS=1
    ;;

  2)
    travis_wait 80 make emulator-ndebug -C regression SUITE=JtagDtmSuite JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-32 -C regression SUITE=JtagDtmSuite JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-64 -C regression SUITE=JtagDtmSuite JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-32 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=DebugTest JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-64 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=DebugTest JVM_MEMORY=3G
    ;;

  3)
    travis_wait 80 make emulator-ndebug -C regression SUITE=JtagDtmSuite JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-32 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=MemTest64 JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-64 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=MemTest64 JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-32 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=MemTest32 JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-64 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=MemTest32 JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    travis_wait 80 make emulator-jtag-dtm-tests-32 -C regression SUITE=JtagDtmSuite JTAG_DTM_TEST=MemTest8 JTAG_DTM_ENABLE_SBA=on JVM_MEMORY=3G
    ;;

  4)
    travis_wait 80 make emulator-ndebug -C regression SUITE=RocketSuiteB JVM_MEMORY=3G
    travis_wait 80 make emulator-regression-tests -C regression SUITE=RocketSuiteB JVM_MEMORY=3G
    ;;

  5)
    travis_wait 80 make emulator-ndebug -C regression SUITE=RocketSuiteA JVM_MEMORY=3G
    travis_wait 80 make emulator-regression-tests -C regression SUITE=RocketSuiteA JVM_MEMORY=3G
    ;;

  6)
    travis_wait 80 make emulator-ndebug -C regression SUITE=RocketSuiteC JVM_MEMORY=3G
    make emulator-regression-tests -C regression SUITE=RocketSuiteC JVM_MEMORY=3G
    ;;

  7)
    travis_wait 100 make emulator-ndebug -C regression SUITE=Miscellaneous JVM_MEMORY=3G
    ;;
  8)
    make scalatest -C regression SUITE=foo JVM_MEMORY=8G
    ;;
  9)
    make scalafix-check -C regression SUITE=foo JVM_MEMORY=8G
    ;;

  -h|--help)
    print_usage
    ;;

  *)
    echo "Error: Invalid bucket number: ${bucket_number}"
    exit 2;
esac
