#!/bin/bash 
## e.g. # preparing_runs.bash /homedata/lfita/etudes/WRFdynamicoSENS/Lothar /data/lfita/DATA/WRF/WRFV3.6.1/gfortran/dmpar/WRFV3 /home/lfita/etudes/WRF4L/camelot-ng /homedata/lfita/etudes/WRFdynamicoSENS/Lothar/metgrid mp1:pbl1:pbl2:rad1 /homedata/lfita/etudes/WRFdynamicoSENS/Lothar/control gfortran
if test $1 = '-h'; then
  echo "******************************************"
  echo "*** Shell to prepare a set of WRF runs ***"
  echo "******************************************"
  echo "preparing_runs.bash [workfold] [WRFfold] [WRF4L] [metgrid] [explabs] [namelist] [compilation]"
  echo "  [workfold]: working folder"
  echo "  [WRFfold]:folder with WRF compilation (down to WRFV3)"
  echo "  [WRF4L]: Folder with WRF PBS scripts"
  echo "  [metgrid]: folder with the 'met_em' files"
  echo "  [explabs]: ':', list of labels for the experiments"
  echo "  [infold]: folder of reference (with namelist.input, tslist, lidarlist, ...)"
  echo "  [compilation]: which kind of compilation (gfortran/ifort)"
else
  workfold=$1
  WRFfold=$2
  WRF4L=$3
  metgrid=$4
  folders=$5
  infold=$6
  compilation=$7

#######    #######
## MAIN
    #######
  rootsh=`pwd`

  folds=`echo ${folders} | tr ':' ' '`

  if test ! -d ${workfold}; then
    echo ${errmsg}
    echo "  folder: '"${workfold}"' does not exist !!"
    exit
  fi

  if test ! -d ${WRF4L}; then
    echo ${errmsg}
    echo "  folder: '"${WRF4L}"' does not exist !!"
    exit
  fi

  if test ! -d ${infold}; then
    echo ${errmsg}
    echo "  folder: '"${infold}"' does not exist !!"
    exit
  fi

  for fold in ${folds}; do
    echo ${fold}
    odir=${workfold}/${fold}

    rm -rf ${odir} >& /dev/null
    mkdir -p ${odir}
    if test $? -ne 0; then
      echo ${errmsg}
      echo "  creation of folder'"${odir}"' failed !!"
      exit
    fi
    cd ${odir}
    ln -s ${WRFfold}/run/* ./
    rm namelist.input

    cp ${infold}/namelist.input ./
    cp ${infold}/tslist ./ >& /dev/null
    cp ${infold}/lidarlist ./ >& /dev/null
    ln -s ${metgrid}/met_em* ./
    if test ${compilation} = 'gfortran'; then
      cp ${WRF4L}/run_real.pbs ./ 
      cp ${WRF4L}/run_wrf.pbs ./ 
    elif test ${compilation} = 'ifort'; then
      cp ${WRF4L}/run_real-intel.pbs ./ 
      cp ${WRF4L}/run_wrf-intel.pbs ./ 
    fi
  done
fi

