#!/bin/bash

# Repalce 'sh run_solver' with 'sbatch run_solver'
find  -wholename "./*/*/run_all.sh" | xargs sed -i 's/sh run_solver.sh.*/sbatch run_solver.sh/'

# Repalce mpirun with srun
find  -wholename "./*/*/run_solver.sh" | xargs sed -i 's/mpirun -np \$processors/srun/'
find  -wholename "./*/*/run_solver.sh" | xargs sed -i '/^processors=[0-9]*/d'

# Adds module load to run_all.sh
find  -wholename "./*/*/run_all.sh" | xargs sed -i '3 i module load OpenFOAM/v2106-gimkl-2020a && source \$FOAM_BASH'

# Adds module load to run_mesh.sh
find  -wholename "./*/*/run_mesh.sh" | xargs sed -i '3 i module load OpenFOAM/v2106-gimkl-2020a && source \$FOAM_BASH'


# Add slurm header and module load
find  -name "run_solver.sh" | xargs sed -i '3 i \
\
export SLURM_EXPORT_ENV=ALL\
\

# replace decomposeParDict with slurm tasks
find . -name decomposeParDict | xargs  sed -i 's/numberOfSubdomains [0-9]*;/numberOfSubdomains ${SLURM_NTASKS};/g'
find . -name decomposeParDict | xargs  sed -i 's/n           ([0-9] [0-9] 1);/n           (4 2 1);/g'