#!/bin/bash 
## g.e. # ./blank_WRF.bash /d4/lflmd/DATA/WRF/WRFV3.3.1 /d4/lflmd/etudes/AquaPlanet/test_eraint
if test $1 = '-h'; then
  echo "**************************************"
  echo "*** Shell to prepare WRF structure ***"
  echo "***  from the scratch in a folder  ***"
  echo "**************************************"
  echo "blank_WRF.bash [WRFfold](folder with serial/dmpar WRFV3 and WPS compilation folders) [WORKfold](working directory)"
else
  rootsh=`pwd`
  WRFfold=$1
  WORKfold=$2

  errmsg='ERROR -- error -- ERROR -- error'
  main='blank_WRF.bash'

  if test $# -ne 2; then
    echo ${errmsg}
    echo "  "${main}": 2 parameters needed '"$@"' passed !!!!"
    exit
  fi

  if test -d ${WRFfold}/serial; then
    infold=${WRFfold}/serial
  else
    infold=${WRFfold}/dmpar
  fi

  # ungrib
  ## 
  odir=${WORKfold}/ungrib
  mkdir -p ${odir}
  links='ungrib.exe Variable_Tables'
  for link in ${links}; do
    rm ${dir}/${link} >& /dev/null
    ln -s ${infold}/WPS/ungrib/${link} ${odir}
  done
  mkdir ${odir}/GribDir
  ln -s ${infold}/WPS/link_grib.csh ${odir}

  # metgrid 
  ## 
  odir=${WORKfold}/metgrid
  mkdir -p ${odir}
  links='metgrid.exe METGRID.TBL.ARW'
  for link in ${links}; do
    rm ${odir}/${link} >& /dev/null
    if test ${link:0:3} = 'MET'; then
      rm ${odir}/METGRID.TBL >& /dev/null
      ln -s ${infold}/WPS/metgrid/${link} ${odir}/METGRID.TBL
    else
      ln -s ${WRFfold}/serial/WPS/metgrid/${link} ${odir}
    fi
  done

  # real 
  ## 
  odir=${WORKfold}/run
  mkdir -p ${odir}
  rm ${odir}/real.exe >& /dev/null
  ln -s ${infold}/WRFV3/main/real.exe ${odir}

  # wrf 
  ## 
  odir=${WORKfold}/run
  rm ${odir}/* >& /dev/null
  ln -s ${infold}/WRFV3/run/* ${odir}
  rm ${odir}/namelist.input
  if test -d ${WRFfold}/serial; then
    rm ${WORKfold}/wrf.exe
    ln -s ${WRFfold}/serial/WRFV3/main/wrf.exe ${odir}/wrf.serial.exe
    ln -s ${WRFfold}/dmpar/WRFV3/main/wrf.exe ${odir}/wrf.dmpar.exe
  fi
fi

echo "export LD_LIBRARY_PATH=${HOME}/bin/gcc_netcdf-4.3.0/lib:\${LD_LIBRARY_PATH}"
