#!/bin/bash

set -e

find . -name testing.out -delete
find . -name build_testing.out -delete
find . -name tallies_testing.out -delete

# conda activate openmc-env

BACK_DIR=${PWD}

OPENMC_BIN=${CONDA_PREFIX}/bin/openmc
PYTHON_BIN=${CONDA_PREFIX}/bin/python
OPENMC_CROSS_SECTIONS=/opt/xdata/endfb-vii.1-hdf5/cross_sections.xml
export OPENMC_CROSS_SECTIONS

for dir in AP1000/*/
do
    cd "$dir"
    ${PYTHON_BIN} *.py | tee testing.out

    cd ${BACK_DIR}
done

for dir in ARC700/*/
do
    cd "$dir"
    ${OPENMC_BIN} problem | tee testing.out

    cd ${BACK_DIR}
done

cd BEAVRS
./make_beavrs.py --optimal
cd ${BACK_DIR}

cd BWR
rm -f *.xml *.h5
jupyter nbconvert --execute BWR.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd Depletion
rm -f *.h5 [d-z]*.xml tallies.out
jupyter nbconvert --execute depletion.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd examples/hexagon
jupyter nbconvert --execute hexagonal-lattice.ipynb --inplace | tee testing.out
cd ../post_process
jupyter nbconvert --execute post-processing.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd RBMK
jupyter nbconvert --execute RBMK.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

#python3 Rep_Na_3_55.py

cd SFR
jupyter nbconvert --execute SFR.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd stress-test/benchmark-1
${OPENMC_BIN} | tee testing.out
cd ../benchmark-2/case1_1

cd ../case1_1
${OPENMC_BIN} | tee testing.out
cd ../case1_2
${OPENMC_BIN} | tee testing.out
cd ../case1_3
${OPENMC_BIN} | tee testing.out
cd ../case2_1
${OPENMC_BIN} | tee testing.out
cd ../case2_2
${OPENMC_BIN} | tee testing.out
cd ../case2_3
${OPENMC_BIN} | tee testing.out

cd ../../lattice-computations
jupyter nbconvert --execute BEAVRS.ipynb --inplace | tee testing.out

cd ../mc-performance
${PYTHON_BIN} build.py | tee build_testing.out
${PYTHON_BIN} generate_tallies.py | tee tallies_testing.out
${OPENMC_BIN}

#### TO DO: Add stress-test testing
cd ${BACK_DIR}

cd TRIGA
jupyter nbconvert --execute TRIGA.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd VHTR
jupyter nbconvert --execute VHTR.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

cd VVER
jupyter nbconvert --execute VVER.ipynb --inplace | tee testing.out
cd ${BACK_DIR}

for dir in VVER/*/
do
    cd "$dir"
    ${OPENMC_BIN} problem | tee testing.out

    cd ${BACK_DIR}
done

cd WBN1
for file in *.ipynb
do
    filename=$(basename -- "$file")
    filename="${filename%.*}"
    jupyter nbconvert --execute ${file} --inplace | tee ${filename}_testing.out
done
cd ${BACK_DIR}


printf "ALL TESTS COMPLETED SUCCESSFULLY!\n"

