#!/bin/bash

function alter_suite() {
    # runs after install_suite in the installed suite dir

    # 1) ensure all all tasks sleep 0 (for speed)
    cat >> suite.rc <<EOF
[runtime]
    [[root]]
        script = /bin/true
EOF
    perl -pi -e "s/sleep \d+/sleep 0/" suite.rc

    # 2) cp stored reference logs to the installed suite dir
    if [[ -f $TEST_SOURCE_DIR/reflogs/$ORIG_SUITE_NAME ]]; then 
        # (the function is also used when generating reference logs)
        cp $TEST_SOURCE_DIR/reflogs/$ORIG_SUITE_NAME reference.log
    fi

    # 3) replace any example job host with localhost
    perl -pi -e "s/host = .*$/host = localhost/" suite.rc

    # 4) warn of expected task failures (else reference test aborts)
    # *** WARNING: ASSUMING FAILED TASKS ARE CLEANED UP BY THE SUITE.
    FAILS=$( grep -Po '[^\s]+(?=:fail)' suite.rc )
    FSTRG=""
    for FAIL in $FAILS; do
        FSTRG="${FAIL}.1, $FSTRG"
    done
    if [[ -n $FAILS ]]; then 
        cat >> suite.rc <<EOF
[cylc]
    [[reference test]]
        expected task failures = $FSTRG
EOF
    fi

    # 5) ensure cycling suites run only two cycles (for speed)
    # *** WARNING: ASSUMING ALL THE SUITES HAVE A 12-HOURLY CYCLE
    # (this is just for consistency - reference tests actually take
    # initial and final cycles from reference log, not the suite def).
    if grep -q "cycling mode = integer" suite.rc; then
      perl -pi -e "s/(initial cycle point\s*=)\s*.*/\1 1/g" suite.rc
      perl -pi -e "s/(final cycle point\s*=)\s*.*/\1 2/g" suite.rc
    else
      perl -pi -e "s/(initial cycle point\s*=)\s*.*/\1 20130808T00/g" suite.rc
      perl -pi -e "s/(final cycle point\s*=)\s*.*/\1 20130808T12/g" suite.rc
    fi
}
