#!/bin/bash
## Usage
if [ $# -ne 2 ]; then
    echo 'Invalid args. You must specify a language for the AMIP reader: <fortran | python>'
    echo 'Usage <lang> <ifs_nproc>'
    echo 'Try ./run_example.sh fortran 4'
    exit 1
fi

# - Define number of processes to run each executable
nproc_exe1=$2
nproc_exe2=1 # AMIP is always one process

host=`uname -n`
user=`whoami`

## - Define paths
srcdir=`pwd`
datadir=$srcdir/data


exe1='python3 IFS_mock.py'
source_exe1=$srcdir/IFS_mock.py

if [ $1 = fortran ]; then
    exe2=amip-forcing
    source_exe2=$srcdir/../sources/amip-forcing/bin/amip-forcing
elif [ $1 = python ]; then
    exe2='python3 amip_forcing.py'
    source_exe2=$srcdir/../sources/amip-forcing/src/python/*.py
else
    echo 'Invalid args. You must specify a language for the AMIP reader: <fortran | python>'
    exit 1
fi

# - Define rundir
rundir=${srcdir}/work_${1}_${nproc_exe1}_${nproc_exe2}


echo '*****************************************************************'
echo '*** '$casename' : '$run
echo ''
echo 'Rundir       :' $rundir
echo 'Architecture :' $arch
echo 'Host         : '$host
echo 'User         : '$user
echo ''
echo $exe1' runs on '$nproc_exe1 'processes'
echo $exe2' runs on '$nproc_exe2 'processes'
echo ''
######################################################################
### 1. Create rundir and copy everything needed
#
\rm -fr $rundir
mkdir -p $rundir
ln -sf -f $datadir/namelist.amip  $rundir/.
ln -sf $datadir/ICMGGa22e+000000  $rundir/.
ln -sf $datadir/*.sh  $rundir/.
ln -sf $datadir/*.txt  $rundir/.
ln -sf $datadir/namcouple  $rundir/.
ln -sf $datadir/*.nc  $rundir/.
cp -f $source_exe1 $rundir/.
cp -f $source_exe2 $rundir/.
cd $rundir
######################################################################
### 2. Definition of mpirun command and batch script
echo 'Executing the model using mpirun'
mpirun -np $nproc_exe1 $exe1 : -np $nproc_exe2 $exe2

echo $casename 'is executed or submitted to queue.'
echo 'Results are found in rundir : '$rundir 
#
######################################################################
