#!/bin/bash
#
# To be run in the tuning workdir !!
#
# Automatic postprocess for 3D simulation of wave $wave
#
# You can choose either to store it in your tuning directory or in its scratch mirror
# with the --scratch option 
#
# In WAVE${wave}/simulations, this script :
#    - create a list the crashed simulation in orig/crashed.txt (get_crashed.py)
#    - link the simulations of relances/*/assembled in orig (link.sh)
#    - clean this directory from crashed simulation (clean.sh)
#    - postprocess the simulation (postpro.sh)
#
# Septembre 2026 : Maëlle CD & Romain R
#
# TO DO :
# - manage the storage on hendrix when postprocessed simu are stored on
#   scratch
#######################################################################


if [[ $# -lt 1 ]] ; then
   echo "run on $local"
   bash run_postpro.sh --help
   exit 1
fi

isscratch=0
while (( $# > 0 )) ; do
        case $1 in
          -wave)    wave=$2; shift ; shift ;; 
          --scratch) isscratch=1 ; shift ;;
          -group)   group=$2 ; shift ; shift;;
          -h|-help|--help) echo Usage: $0 "[-wave wave_number] [-group groupname (eclis) ] [--scratch]" ; exit 1 ;;
        esac
done


HERE=`pwd` #tuning workdir path
wdir=$(echo $HERE | awk -F/ '{print $NF}') #tuning workdir name
installshared=${HERE}/../../../install_shared

if [ $isscratch -eq 1 ] ; then pproot=${WORKDIR}/${wdir} 
elif [ $isscratch -eq 0 ] ; then pproot=`pwd`
else echo "isscratch must be 0 or 1" ; exit 1 ;
fi

# source the htexplo env to have the adequates modules
source $installshared/base_env.profile 
source $installshared/env.sh 
#module purge > /dev/nul 2>&1
#module load python/3.7.6

# arborescence (absolute path)
ppdir=${pproot}/WAVE${wave}/simulations/
origdir=${ppdir}/orig
ppeddir=${ppdir}/postprocessed

mkdir -p $origdir
mkdir -p $ppeddir

# Directory where simulations are stored with eclis 
relandir=${HOME}/relances/${group}/WAVE${wave}/
eclisdir=${WORKDIR}/eclis/${group}/WAVE${wave}/

# Number of simulations
nsim=$(($(wc -l < ${HERE}/WAVE${wave}/Par1D_Wave${wave}.asc) -1))

# We work from the ${ppdir}
cd ${ppdir}

# Get information about crashed simulations
cp ${HERE}/postpro/get_crashed.py ${ppdir} 
./get_crashed.py -relandir $relandir 
cp ${relandir}/crashed.txt ${ppdir}/

#Update number of simulation that did not crashed in ntsimu
ncrashed=$(wc -l < crashed.txt)
ntsimu=$(( $nsim - $ncrashed ))


# Get relevant postprocessing scripts
cp ${HERE}/postpro/link.sh ${ppdir}/
cp ${HERE}/postpro/clean.sh ${ppdir}/
cp ${HERE}/postpro/postpro.sh ${ppdir}/
cp ${HERE}/postpro/postpro_fhstyle.sh ${ppdir}/

# Make local links to the assembled output of the simulations
if [ $(ls $origdir | wc -l) -lt $ntsimu ] ; then  
  ./link.sh -eclisdir $eclisdir -origdir $origdir 
fi

if [ $(ls $origdir | wc -l) -ne $ntsimu ] ; then  
  ./clean.sh -origdir $origdir
fi 

echo Perform the postprocessing
./postpro.sh -origdir $origdir -ppeddir $ppeddir

#Store the postprocessed simulation in hendrix if they are in scratch dir
if [ $isscratch -eq 1 ] ; then
  echo Manage the storage on hendrix !!
#cat << eod > backup_hendrix_wave${wave}.sh
##!/usr/bin/bash
##SBATCH -J backup_wave${wave}
##SBATCH --partition=transfert
##SBATCH -N 1
##SBATCH -n 1
##SBATCH --time=00:02:00
##SBATCH --ntasks-per-node=1
#
#cd ${ppeddir}
#ftput -q -o mkdir * HTEXPLO/$wdir/WAVE$wave/simulations/postprocessed/
#eod
fi
