#!/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

  # ungrib
  ## 
  odir=${WORKfold}/ungrib
  mkdir -p ${odir}
  links='ungrib.exe Variable_Tables'
  for link in ${links}; do
    rm ${odir}/${link} >& /dev/null
    ln -s ${WRFfold}/serial/WPS/ungrib/${link} ${odir}
  done
  mkdir ${odir}/GribDir
  ln -s ${WRFfold}/serial/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 ${WRFfold}/serial/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 ${WRFfold}/serial/WRFV3/main/real.exe ${odir}

  # wrf 
  ## 
  odir=${WORKfold}/run
  links='wrf.exe CAM co2_trans ETA .TBL .tbl .formatted RRTM tr'
  for link in ${links}; do
    rm ${odir}/*${link}* >& /dev/null
    if test ${link} = 'wrf.exe'; then
      ln -s ${WRFfold}/serial/WRFV3/main/*${link}* ${odir}/wrf.serial.exe
      ln -s ${WRFfold}/dmpar/WRFV3/main/*${link}* ${odir}/wrf.dmpar.exe
    else
      ln -s ${WRFfold}/serial/WRFV3/run/*${link}* ${odir}
    fi
  done
fi

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