set -xuve

HPCROOTDIR=%HPCROOTDIR%
EXPID=%EXPID%
CHUNK=%CHUNK%
SDATE=%SDATE%
MEMBER=%MEMBER%

# Load the modules
module purge
module load intel/2017.4 mkl/2017.4 python/3.6.1 netcdf/4.2 impi/2017.4

# setup pyOasis environment
source $HPCROOTDIR/python-amip-reader/sources/oasis3-mct/generated/python/init.sh

# Go to the source directory
launch_dir=$HPCROOTDIR/python-amip-reader/sources/ece3-toy-model
cd ${launch_dir}

# Check that inidata dir exists and is not empty, rise error and exit otherwise
inidata=${HPCROOTDIR}/${SDATE}/${MEMBER}/inidata
if [ ! -d ${inidata} ]; then
    echo "ERROR: ${inidata} does not exist"
    exit 1
fi
if [ ! -n "$(ls -A ${inidata})" ]; then
    echo "ERROR: ${inidata} is empty"
    exit 1
fi

# Create the member runtime directory if it is the first Chunk
runtime=${HPCROOTDIR}/${SDATE}/${MEMBER}/runtime
if [ ${CHUNK} -eq 1 ]; then
    # Remove old runtime if exists and create a new one
    if [ -d ${runtime} ]; then
        rm -rf ${runtime}
    fi
    mkdir -p ${runtime}
# If not the first Chunk, check that runtime exists. Print error and exit otherwise
else
    if [ ! -d ${runtime} ]; then
        echo "ERROR: ${runtime} does not exist"
        exit 1
    fi
fi

# Copy the initial data files from inidata to runtime
cp -r ${inidata}/* ${runtime}

# Go to the rundir
cd ${runtime}

# Define the number of processors for each component
COMP1_nproc=${MY_COMP1_NPROC}
COMP2_nproc=${MY_COMP2_NPROC}
COMP3_nproc=1 #${MY_COMP3_NPROC}

# Define the yaml configuration files for each component
COMP1_yaml=COMP1_toy_model.yaml
COMP2_yaml=COMP2_toy_model.yaml
COMP3_yaml=COMP3_toy_model.yaml

# Substitute the number of processors in the yaml files
sed -i "s/model_nproc: .*/model_nproc: ${COMP1_nproc}/" ${COMP1_yaml}
sed -i "s/model_nproc: .*/model_nproc: ${COMP2_nproc}/" ${COMP2_yaml}
sed -i "s/model_nproc: .*/model_nproc: ${COMP3_nproc}/" ${COMP3_yaml}

# Copy the scalability data for each component to runtime
COMP1_scalability=COMP1_scalability.csv
COMP2_scalability=COMP2_scalability.csv
COMP3_scalability=COMP3_scalability.csv

cp ${inidata}/ece3_scalability_data/${COMP1_scalability} ${runtime}
cp ${inidata}/ece3_scalability_data/${COMP2_scalability} ${runtime}
cp ${inidata}/ece3_scalability_data/${COMP3_scalability} ${runtime}

# Change the scalability data file to read from in the yaml files
sed -i "s/scalability: .*/scalability: ${COMP1_scalability}/" ${COMP1_yaml}
sed -i "s/scalability: .*/scalability: ${COMP2_scalability}/" ${COMP2_yaml}
sed -i "s/scalability: .*/scalability: ${COMP3_scalability}/" ${COMP3_yaml}

# Run the model (for now in sequential)
mpirun -np 1 python3 toy_model.py --yaml_file_path ${COMP1_yaml} \
: -np 1 python3 toy_model.py --yaml_file_path ${COMP2_yaml} \
: -np 1 python3 toy_model.py --yaml_file_path ${COMP3_yaml}

# Move the output files to the runtime/log/chunk directory
log_dir=${runtime}/log/$(printf "%03d" ${CHUNK})
mkdir -p ${log_dir}

# Move the output files to the log directory
mv timeline_* debug.* load_balancing_info.txt nout.000000 *.out* ${log_dir}