#!/bin/csh -f

set nonomatch


foreach dir ( frame chem share dyn_em phys phys/physics_mmm cmaq main tools wrftladj )
  if ( -d $dir ) then
     if ( $dir == cmaq ) then
        ( cd $dir ; echo $dir ; /bin/rm -f *.o *.mod ) >& /dev/null
     else
        ( cd $dir ; echo $dir ; /bin/rm -f core wrf *.f90 *.exe *.kmo *.mod *.o *.obj *.inc *.a \
                      db_* Warnings module_state_description.F module_dm.F gmeta \
                      wrfdata whatiread rsl.* show_domain* ) >& /dev/null
     endif
  endif
end

if ( -d var ) then
  ( cd var ; make clean )
endif

( cd tools/CodeBase ; make clean )

( cd inc ; /bin/rm -f *.inc namelist.default commit_decl *.o *.F *.f90 )

find . -name \*.dSYM -exec rm -rf {} \; >& /dev/null

set arg="$1"
if ( "$arg" == '-a' || "$arg" == '-aa' ) then
  if ( -d var ) then
    ( cd var ; make superclean )
    ( cd var/obsproc ; make clean )
  endif
  if ( -f Registry/Registry ) then
    /bin/cp Registry/Registry Registry/Registry.backup
    /bin/rm -f Registry/Registry
    /bin/rm -f Registry/io_boilerplate_temporary.inc
    if ( -f Registry/Registry.rconfig ) then
      /bin/rm -f Registry/Registry.rconfig
    endif
  endif
  /bin/rm -fr ./netcdf_links
  /bin/rm -fr tools/code_dbase
  ( cd external ; make -i superclean )
  ( cd external/io_grib1/WGRIB ; make clean )
  ( cd external/atm_ocn ; make clean )
  ( cd tools ; /bin/rm -f registry gen_comms.c fseeko_test fseeko64_test nc4_test.log ) >& /dev/null
  ( cd inc; /bin/rm -f dm_comm_cpp_flags wrf_io_flags.h wrf_status_codes.h ) >& /dev/null
  if ( -f configure.wrf ) then
    /bin/cp configure.wrf configure.wrf.backup
    /bin/rm -f configure.wrf >& /dev/null
  endif
  if ( "$arg" != '-aa' ) then
    ( cd run ; /bin/rm -f gm* out* fort* ideal* *.exe input_sounding p3_lookupTable_1.dat-3momI_v5.1.6 ; \
	  /bin/cp -f namelist.input namelist.input.backup.`date +%Y-%m-%d_%H_%M_%S` ; \
	  /bin/rm -f namelist.input ) >& /dev/null
    ( cd test/exp_real ; /bin/rm -f gm* out* fort* real* ) >& /dev/null
    ( cd test ; rm -f */*.exe */ETAMPNEW_DATA* */GENPARM.TBL */LANDUSE.TBL */README.namelist */README.physics_files \
	  */RRTM_DATA */SOILPARM.TBL */VEGPARM.TBL */MPTABLE.TBL */URBPARM.TBL */URBPARM_LCZ.TBL */grib2map.tbl \
	  */CAM_ABS_DATA */CAM_AEROPT_DATA \
	  */CCN_ACTIVATE.BIN */STOCHPERT.TBL \
	  */CAMtr_volume_mixing_ratio.RCP4.5 */CAMtr_volume_mixing_ratio.RCP6 */CAMtr_volume_mixing_ratio.RCP8.5 \
	  */CAMtr_volume_mixing_ratio.A1B */CAMtr_volume_mixing_ratio.A2 */CAMtr_volume_mixing_ratio \
	  */CAMtr_volume_mixing_ratio.SSP119 */CAMtr_volume_mixing_ratio.SSP126 \
	  */CAMtr_volume_mixing_ratio.SSP245 */CAMtr_volume_mixing_ratio.SSP370 \
	  */CAMtr_volume_mixing_ratio.SSP585 \
	  */CLM_*DATA */RRTMG_LW_DATA */RRTMG_SW_DATA \
	  */p3_lookup* */BROADBAND_CLOUD_GODDARD.bin \
	  */ozone.formatted */ozone_lat.formatted */ozone_plev.formatted \
	  */aerosol.formatted */aerosol_lat.formatted */aerosol_plev.formatted */aerosol_lon.formatted \
	  */kernels.asc_s_0_03_0_9 */bulkradii.asc_s_0_03_0_9 */bulkdens.asc_s_0_03_0_9 \
	  */constants.asc \
	  */masses.asc */kernels_z.asc */capacity.asc */termvels.asc */coeff_p.asc */coeff_q.asc \
	  */gribmap.txt */tr??t?? */co2_trans */namelist.output */ishmael-gamma-tab.bin \
	  */eclipse_besselian_elements.dat \
	  */HLC.TBL */wind-turbine-1.tbl \
          */ishmael-qi-qc.bin */ishmael-qi-qr.bin ) >& /dev/null
     ( cd test/em_fire; rm -rf two_fires rain )
  else if ( "$arg" == '-aa' ) then
    /bin/rm -f configure.wrf.backup
    /bin/rm -f Registry/Registry.backup
    /bin/rm -f run/namelist.input.backup.*
    /bin/rm -f phys/module_sf_noahmpdrv.F phys/module_sf_noahmp_glacier.F \
               phys/module_sf_noahmp_groundwater.F phys/module_sf_noahmplsm.F \
               run/MPTABLE.TBL
    /bin/rm -f phys/module_bl_mynnedmf.F phys/module_bl_mynnedmf_common.F \
               phys/module_bl_mynnedmf_driver.F
  endif
endif

#cms++

if ( -d chem ) then
  if ( -e chem/KPP )then
    ( cd chem/KPP; ./clean_kpp ; rm -f if_required ) 
  endif
endif
#cms--

#wrf_hydro++
  if ( -e "hydro/Makefile.comm" ) then
    (cd hydro; make -f Makefile.comm clean)
  endif
#wrf_hydro--

