###################################################################
echo "------------------------------------------------"
echo "J101 - " 
echo "------------------------------------------------"
echo "History: OCT 1996 - First implementation of this new script."
echo "History: FEB 1998 - Dropped /com/ncepdate (2-digit year)"
echo "                    from utility zdate.sh"
echo "History: MAR 1999 - Adapted for running on CLASS-VIII IBM"
echo "         FEB 2001 - Split from the ex100.sh.sms script"
echo "         OCT 2004 - Renamed ex101.sh.sms to exprod_cleanup.sh.sms"
echo "         DEC 2011 - Add cleanup of /com/output/transfer"
echo "         DEC 2011 - Modify cleanup of /tmpnwprd and add "
echo "                    recursive flag to correct rm of "
echo "                    /nwges/wsr subdirectories"
echo "         MAY 2012 - Add cleanup of /com/web/prod/realtime/thanks/" 
#####################################################################

cd $DATA

#####################################################################
#
# START FLOW OF CONTROL
#
# 1) Clean /home/nwprod directory. *This is not currently done
# 2) Clean /com directories.
# 3) Thin /com directories.
# 4) Clean /nwges directories.
# 5) Thin /nwges directories.
# 6) Remove /tmpnwprd directories or files older than one day.
# 7) Clean /dcom/us007003 directories owned by nwprod
# 8) Clean /com or /pcom sub-directories
# 9) Thin /com ensemble sub-directories (gefs and cmc) 
#####################################################################

########################################
set -x
msg="JOB $job HAS BEGUN"
postmsg "$jlogfile" "$msg"
##########################################

#####################################################################
#
# 1.  Clean /home/nwprod directory.
#
#####################################################################
#rm -rf /home/nwprod/*

#########################################################################
#
# 2.  Clean /com directories
#
#########################################################################
if test "$cleanup_com" = "YES"
then
   grep -v "#" $PARMutil/cleanup_rm_com | sed "s/ //g" > tmp_clean
   for clean_list in `cat tmp_clean`
   do
      NET=`echo $clean_list | awk -F"|" '{print $2}'`
      directory=`echo $clean_list | awk -F"|" '{print "/com/"$1"/\$clean_envir"}'`
      prod_keep00=`echo $clean_list | awk -F"|" '{print $3}'`
      prod_keep12=`echo $clean_list | awk -F"|" '{print $4}'`
      para_keep00=`echo $clean_list | awk -F"|" '{print $5}'`
      para_keep12=`echo $clean_list | awk -F"|" '{print $6}'`
      test_keep00=`echo $clean_list | awk -F"|" '{print $7}'`
      test_keep12=`echo $clean_list | awk -F"|" '{print $8}'`
      if [ `echo $clean_list | awk -F"|" '{print $1}'` = output ]; then
        transfer_keep00=`echo $clean_list | awk -F"|" '{print $9}'`
        transfer_keep12=`echo $clean_list | awk -F"|" '{print $10}'`
        
        if test `eval echo $"cleanup_com_transfer"` = "YES"
        then
           comt=/com/output/transfer
           keep=`eval echo $"transfer_keep${cyc}"`
           sh $USHutil/cleanup_rmdir.sh $PDY $comt $keep
        fi
      fi

      for clean_envir in prod para test
      do
         if test `eval echo $"cleanup_com_${clean_envir}"` = "YES"
         then
            com=`eval echo $directory`
            keep=`eval echo $"${clean_envir}_keep${cyc}"`

            sh $USHutil/cleanup_rmdir.sh $PDY $com $keep $NET
         fi
      done
   done
fi

#########################################################################
#
# 3.  Thin /com directories
#
#########################################################################
if test "$thin_com" = "YES"
then
   grep -v "#" $PARMutil/cleanup_thin_com | sed "s/ //g" > tmp_clean
   for clean_list in `cat tmp_clean`
   do
      NET=`echo $clean_list | awk -F"|" '{print $2}'`
      directory=`echo $clean_list | awk -F"|" '{print "/com/"$1"/\$clean_envir"}'`
      keep_flag=`echo $clean_list | awk -F"|" '{print $3}'`
      prod_keep00=`echo $clean_list | awk -F"|" '{print $4}'`
      prod_keep12=`echo $clean_list | awk -F"|" '{print $5}'`
      para_keep00=`echo $clean_list | awk -F"|" '{print $6}'`
      para_keep12=`echo $clean_list | awk -F"|" '{print $7}'`
      test_keep00=`echo $clean_list | awk -F"|" '{print $8}'`
      test_keep12=`echo $clean_list | awk -F"|" '{print $9}'`

      for clean_envir in prod para test
      do
         if test `eval echo $"thin_com_${clean_envir}"` = "YES"
         then
            com=`eval echo $directory`
            keep=`eval echo $"${clean_envir}_keep${cyc}"`

            if test "$keep_flag" = "K"
            then
               rmfile=$PARMutil/cleanup_com_${NET}_keeplist
            elif test "$keep_flag" = "R"
            then
               rmfile=$PARMutil/cleanup_com_${NET}_rmlist
            else
               rmfile=NONE
               msg="No Flag Set for ${com}/${NET} in cleanup_thin_com table"
               postmsg "$jlogfile" "$msg"
            fi
   
            if test -f $rmfile
            then
               $USHutil/cleanup_thindir.sh $PDY $com $NET $keep $rmfile
            else
               if test $rmfile != "NONE"
               then
                  msg="File $rmfile not found.  No thinning of $com/$NET occurred."
                  postmsg "$jlogfile" "$msg"
               fi
            fi
         fi
      done
   done
fi

#########################################################################
#
# 4.  Clean /nwges directories
#
#########################################################################
if test "$cleanup_nwges" = "YES"
then
   grep -v "#" $PARMutil/cleanup_rm_nwges | sed "s/ //g" > tmp_clean
   for clean_list in `cat tmp_clean`
   do
      NET=`echo $clean_list | awk -F"|" '{print $1}'`
      prod_keep00=`echo $clean_list | awk -F"|" '{print $2}'`
      prod_keep12=`echo $clean_list | awk -F"|" '{print $3}'`
      para_keep00=`echo $clean_list | awk -F"|" '{print $4}'`
      para_keep12=`echo $clean_list | awk -F"|" '{print $5}'`
      test_keep00=`echo $clean_list | awk -F"|" '{print $6}'`
      test_keep12=`echo $clean_list | awk -F"|" '{print $7}'`

      for clean_envir in prod para test
      do
         if test `eval echo $"cleanup_nwges_${clean_envir}"` = "YES"
         then
            keep=`eval echo $"${clean_envir}_keep${cyc}"`

            sh $USHutil/cleanup_rmdir.sh $PDY /nwges/$clean_envir $keep $NET
         fi
      done
   done
fi

#########################################################################
#
# 5.  Thin /nwges directories
#
#########################################################################
if test "$thin_nwges" = "YES"
then
   ######################################################
   # 
   # Thin RAP guess files
   # 
   ######################################################

   for gesday in $PDYm3 $PDYm4 $PDYm5 $PDYm6 $PDYm7
   do
     Year=`echo $gesday |cut -c1-4`
     Mon=`echo $gesday |cut -c5-6`
     Day=`echo $gesday |cut -c7-8`
     timestr=${Year}-${Mon}-${Day}
     for clean_envir in prod para
     do
     rm -f /nwges/${clean_envir}/rap/rapges/*${gesday}*
     rm -f /nwges/${clean_envir}/rap/rapges_pcyc/*${gesday}*
     rm -f /nwges/${clean_envir}/rap/rapbc/*${gesday}*
     rm -f /nwges/${clean_envir}/rap/rapbc/*${timestr}*
     done
   done

   ######################################################
   #
   # Thin WSR guess files
   #
   ######################################################
   for gesday in $PDYm6 $PDYm7
   do
      rm -rf /nwges/wsr/${gesday}*
      rm -rf /nwges/wsr/ec${gesday}*
      rm -rf /nwges/wsr/nc${gesday}*
   done

   for day in $PDYm6 $PDYm7
   do
      rm -rf /com/aqm/para/AQM3X/aqm.$day
   done
fi

#########################################################################
#
# 6. Remove /tmpnwprd directories or files older than one day.
#  Dec 2011 - Changed from mtime to ctime to avoid premature cleanup of 
#  files added via 'cp -p'. Added -prune to save time and error msgs.
#  (No need to descend if the directory itself has an old time stamp).
#
#########################################################################
if test "$thin_tmpnwprd" = "YES"
then
   find /tmpnwprd/ -ctime +1 -prune -exec rm -rf {} \;
fi

#########################################################################
#
# 7. Cleanup /dcom/us007003 directories owned by nwprod
#
#########################################################################
export PDYM10=`$USHutil/finddate.sh $PDY d-10`
export PDYM11=`$USHutil/finddate.sh $PDY d-11`
export PDYM12=`$USHutil/finddate.sh $PDY d-12`
for gesday in $PDYM10 $PDYM11 $PDYM12
do
   rm -rf /dcom/us007003/${gesday}
   rm -rf /dcom/us007003/para/${gesday}
done

#Clean up subsurface data tanks (YYYYMM), keep previous 6 months
DOM=`echo $PDY |cut -c7-8`
if test $DOM = "01"
then
   CURRENT_MONTH=`echo $PDY |cut -c5-6`
   YYYY=`echo $PDY |cut -c1-4`
   MMP2=`expr $CURRENT_MONTH - 7`
   if [ $MMP2 -lt 10 ]; then MMP2=0$MMP2; fi
   YYYYP2=$YYYY
   if [ $MMP2 -le 0 ]; then
         MMP2=`expr $MMP2 + 12`
         if [ $MMP2 -lt 10 ]; then MMP2=0$MMP2; fi
         YYYYP2=`expr $YYYY - 1`
   fi
   rm -rf /dcom/us007003/${YYYYP2}${MMP2}
fi

#########################################################################
#
# 8. Clean /com or /pcom subdirectories 
#    Used for atypical directory structures not of the type
#    /com/${NET}/${envir}/${RUN}.${PDY} or pcom directories where the
#    files are not overwritten.
#
#########################################################################

   ######################################################
   #
   # Clean old atcf data in /com/tpc/prod
   #
   ######################################################
   find /com/tpc/prod/atcf/??8* -mtime +7 -exec rm -rf {} \;
   find /com/tpc/prod/atcf/??9* -mtime +7 -exec rm -rf {} \;
   find /com/tpc/prod/atcf/??[0-4]* -mtime +240 -exec rm -rf {} \;

   ######################################################
   #
   # Clean cdas and cdas2 data older than 2 months in /com/arkv/prod
   #
   ######################################################
   find /com/arkv/prod/cdas* -type d -mtime +60 -exec rm -rf {} \;
   find /com/arkv/prod/cdas2* -type d -mtime +60 -exec rm -rf {} \;

   ######################################################
   #
   # Clean amsu daily directories under /com/tpc/prod/amsu that are 
   # older than 5 days 
   #
   ######################################################
   sh $USHutil/cleanup_rmdir.sh $PDY /com/tpc/prod/amsu 5 amsu 

   ######################################################
   #
   # Clean psurge directories that are older than 5 days in 
   # /pcom/psurge (due to storm id, they will not be overwritten)
   # 
   ######################################################
   find /pcom/psurge -type f -mtime +5 -exec rm -rf {} \;

   ######################################################
   #
   # Cleanup gaugeqc data in /com/hourly/prod/gaugeqc that
   # are older than 45 days.
   #
   ######################################################
   find /com/hourly/prod/gaugeqc -type f -mtime +45 -exec rm -rf {} \;

   ######################################################
   #
   # Cleanup selected cities/travelers data in /pcom/prod/obx that
   # are older than 7 days.
   #
   ######################################################
   find /pcom/prod/obx -type f -mtime +7 -exec rm -rf {} \; 

   ######################################################
   #
   # Cleanup verification data in /com/verf/prod/vsdb that       
   # are older than 40 days.
   #
   ######################################################
   find /com/verf/prod/vsdb/gridtobs/* -type f -mtime +40 -exec rm -rf {} \; 
   find /com/verf/prod/vsdb/precip/* -type f -mtime +40 -exec rm -rf {} \; 

   ######################################################
   #
   # Clean thanks daily directories under the /com/web/prod/realtime/thanks
   # older than 63 days
   #
   ######################################################
   sh $USHutil/cleanup_rmdir.sh $PDY /com/web/prod/realtime/thanks 63

#########################################################################
#
# 9. Thin /com ensemble (gefs and cmc) sub-directories
#
#########################################################################
for day in $PDYm3 $PDYm4
do
   rm -rf /com/gens/para/gefs.$day/*/pgrbb
   rm -rf /com/gens/para/gefs.$day/*/pgrb2b
   rm -rf /com/gens/para/gefs.$day/*/sfcsig

   rm -rf /com/gens/prod/gefs.$day/*/pgrbb
   rm -rf /com/gens/prod/gefs.$day/*/pgrb2b
   rm -rf /com/gens/prod/gefs.$day/*/sfcsig
done

$USHutil/cleanup_thin_subdir.sh

#####################################################################
# GOOD RUN
set +x
echo "**************JOB PROD_CLEANUP COMPLETED NORMALLY ON THE IBM"
echo "**************JOB PROD_CLEANUP COMPLETED NORMALLY ON THE IBM"
echo "**************JOB PROD_CLEANUP COMPLETED NORMALLY ON THE IBM"
set -x
#
#####################################################################

msg="JOB $job HAS COMPLETED NORMALLY."
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
