#!/bin/sh
# Need to execute prep.sh before
# to launch simulation

if [ $# -ne 6 ] ; then
    bash isntall_and_run3D.sh --help
    exit 1
fi

while (( $# > 0 )) ; do
    case $1 in
        -wave)       wave=$2 ;  shift ; shift ;;
        -nsimus)     nsimus=$2 ; shift ; shift ;;
        -njobs)      njobs=$2 ; shift ; shift ;;
        -name)       name=$2 ; shift ; shift ;;
        -group)      group=$2 ; shift ; shift ;;
        -pathrelan)  pathrelan=$2 ; shift ; shift ;;
        -h|-help|--help) echo Usage: $0 "[-wave wave_number] [-nsimus nsimus]" ;
exit 1 ;;
        *) echo the args $1 in not an argument of this script ; exit 1 ;;
    esac
done

cd WAVE${wave}/config3D/
HERE=$(pwd)
nchaine=$(($nsimus - $njobs))

# the first loop do both the installation and the launch
# of the $njobs first simulations
for i in `seq -w 001 $njobs`
do
  irun=$(printf "%03d" $i)
  pathsimu=${pathrelan}/${group}/${name}_r$irun
  [ -d "$pathsimu" ] && delexp ${name}_r$irun
  bash ${HERE}/param_${name}_r$irun -noask -go
done

# the second loop just install the other simulations
# that will be launcah automatically if the previous
# simulation succeeded
for i in `seq -w $(($nchaine + 1))  $nsimus`
do
  irun=$(printf "%03d" $i)
  pathsimu=${pathrelan}/${group}/${name}_r$irun
  [ -d "$pathsimu" ] && delexp ${name}_r$irun
  bash ${HERE}/param_${name}_r$irun -noask <<< "n"
done
