# SccsId[] = "%W% (USL function) %G%"
              CWF_name="CLEANUP_WORK_FILES"
              if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
                 [ ."`set|egrep '^$CWF_name\(\)\{$'`" != . ] && CWF_loaded=1
              else # Korn or Bash shell and function already loaded?
                 if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
                    [ ."`typeset +f|awk '/^'$CWF_name'[=\(]?/'`" != . ] && CWF_loaded=1
                 else # Linux
                    [ ."`typeset -F|awk '/^'$CWF_name'[=\(]?/'`" != . ] && CWF_loaded=1
                 fi
              fi
              if [ 0${CWF_loaded} -eq 0 ]; then
              #----------------------------------------------------------------------#
              CLEANUP_WORK_FILES() # Function documentation located at bottom.       #
              #----------------------------------------------------------------------#
              { [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
                                  ||   { [ -x /bin/gawk     ] && AWK=/bin/gawk    ; } \
                                  ||   { [ -x /usr/bin/awk  ] && AWK=/usr/bin/awk ; }; }

                if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi

                . $SHLIB/chdir_exit_err.sh
                . $SHLIB/email_msg.sh       # Calls $SHLIB/exit.sh

                #------------------------------------------------------------#
                # If the following variables are not set, use these defaults.#
                #------------------------------------------------------------#
                : ${script_name:=`basename $0`}
                : ${sp:="                    "}

                CWF_ID="$script_name($CWF_name)"

                #-----------------------------#
                # Assign quiet option.        #
                #-----------------------------#
                CWF_opt_q=0
                if [ ."$1" = .'-q' ]; then
                   CWF_opt_q=1
                   shift
                fi

                CWF_exit_status=0
                if [ $# -lt 1 ]; then
                   #---------------------------------------------------------------#
                   # Normally, this kind of error is tagged as a FATAL ERROR,      #
                   # however, the inability to remove files is not earth shaking,  #
                   # so, we only describe it as an ERROR, and "return" failure (1).#
                   # (We do this throughout this function.)                        #
                   #---------------------------------------------------------------#
                   EMAIL_MSG "ERROR: $CWF_ID"              \
                     "${sp}Insufficient args."             \
                     "${sp}Usage: $CWF_name fileid [...]." \
                     "${sp}$script_name continues."
                   return 1
                fi

                CWF_cwd=`/bin/pwd`
                for CWF_file
                do
                   CWF_files=`ls ${CWF_file}* 2> /dev/null|grep -vi 'No such file'`
                   if [ ."$CWF_files" != . ]; then
                      if [ $CWF_opt_q -eq 0 ]; then
                         echo "`date '+%Y-%m-%d %T'` Cleaning up $CWF_file* files."
                      fi

                      CWF_dir=`dirname $CWF_file`
                      CWF_status=$?
                      if [ $CWF_dir = "." ]; then
                         #-------------------------------------------------------#
                         # Because we specify $CWF_dir in the find command       #
                         # below, fully qualified fileids (with real paythnames) #
                         # are a requirement.                                    #
                         #-------------------------------------------------------#
                         CWF_exit_status=1
                         EMAIL_MSG "ERROR: $CWF_ID"                           \
                           "${sp}$CWF_file missing fully qualified pathname." \
                           "${sp}Missing or relative pathnames (e.g ../) \c"  \
                              "are unacceptable."                             \
                           "${sp}Skipping $CWF_file, \c"                      \
                              "but continuing $script_name."
                         continue # Iterate for-loop
                      elif [ $CWF_status -ne 0 ]; then
                         CWF_exit_status=1
                         EMAIL_MSG "ERROR: $CWF_ID"                         \
                           "${sp}'dirname' failed with $CWF_status status!" \
                           "${sp}Skipping $CWF_file, \c"                    \
                              "but continuing $script_name."
                         continue # Iterate for-loop
                      fi

                      CWF_fil=`basename $CWF_file`
                      CWF_status=$?
                      if [ $CWF_status -ne 0 ]; then
                         CWF_exit_status=1
                         EMAIL_MSG "ERROR: $CWF_ID"                         \
                           "${sp}'basename' fails with $CWF_status status!" \
                           "${sp}Skipping $CWF_file, \c"                    \
                              "but continuing $script_name."
                         continue # Iterate for-loop
                      fi

                      #------------------------------------------------------------#
                      # Delete zero-length files first (noting that they are such).#
                      # (We use find because it doesn't have the lmitations of ls.)#
                      #------------------------------------------------------------#
                      CWF_zero_length_files=`find $CWF_dir -name "$CWF_fil*" \
                         -size 0c -print 2> /dev/null|grep -vi "find: cannot read dir"`
                      CWF_zero_length_n=`echo $CWF_zero_length_files|wc -w`
                      if [ $CWF_zero_length_n -gt 0 ]; then
                         for CWF_zfile in $CWF_zero_length_files
                         do
                            #------------------------------------------------------#
                            # If "quiet" option is given, skip "(zero length)" msg.#
                            #------------------------------------------------------#
                            if [ $CWF_opt_q -eq 0 ]; then
                               echo "${sp}Deleting    $CWF_zfile (zero length)"
                            fi
                            \rm -f $CWF_zfile 2>&1
                            CWF_status=$?
                            if [ $CWF_status -ne 0 ]; then
                               CWF_exit_status=1
                               EMAIL_MSG "WARNING: $CWF_ID"                \
                                 "${sp}$CWF_name 'rm $CWF_zfile' error."   \
                                 "${sp}completed with $CWF_status status!" \
                                 "${sp}$script_name continues."
                               continue # Iterate for-loop
                            fi
                         done
                      fi

                      #------------------------------------#
                      # Delete the non-zero-length files.  #
                      #------------------------------------#
                      CWF_nzero_length_files=`find $CWF_dir -name "$CWF_fil*" \
                         -print 2> /dev/null|grep -vi "find: cannot read dir"`
                      CWF_nzero_length_n=`echo $CWF_nzero_length_files|wc -w`
                      if [ $CWF_nzero_length_n -gt 0 ]; then
                         for CWF_nzfile in $CWF_nzero_length_files
                         do
                            if [ $CWF_opt_q -eq 0 ]; then
                               echo "${sp}Deleting    $CWF_nzfile"
                            fi
                            \rm -f $CWF_nzfile 2>&1
                            CWF_status=$?
                            if [ $CWF_status -ne 0 ]; then
                               CWF_exit_status=1
                               EMAIL_MSG "WARNING: $CWF_ID"                   \
                                 "${sp}$CWF_name 'rm $CWF_nzfile' error."     \
                                 "${sp}completed with $CWF_status status!"    \
                                 "${sp}$script_name continues."
                               continue # Iterate for-loop
                            fi
                         done
                      fi
                   fi # if [ -f $CWF_file* ]; then
                done # for CWF_file

                cd $CWF_cwd
                return $CWF_exit_status
              } # "CWF_" prefix identifies this function's local variables.
              fi

              #======================================================================#
              #                       D O C U M E N T A T I O N                      #
              #======================================================================#
              #                                                                      #
              #      Author: Bob Orlando                                             #
              #                                                                      #
              #        Date: December 14, 1996                                       #
              #                                                                      #
              #  Program ID: cleanup_work_files.sh                                   #
              #                                                                      #
              #       Usage: CLEANUP_WORK_FILES [-q] fileid ...                      #
              #                                  -q = Delete quietly--without echo.  #
              #                                       Errors encountered during file #
              #                                       removal are still reported.    #
              #                                                                      #
              #     Purpose: Clean up process work files with error checking,        #
              #              reporting any problems via email.                       #
              #                                                                      #
              # Description: Fileids are the file_roots of the files we're to clean  #
              #              up (the following is an example of how we're used):     #
              #                                                                      #
              #                 +----------------------------------------------+     #
              #                 | #!/bin/sh                                    |     #
              #                 | ...                                          |     #
              #                 | ..                                           |     #
              #                 | cleanup_files=""                             |     #
              #                 |                                              |     #
              #                 | stdout_root=/tmp/$name_root"_stdout"         |     #
              #                 | stdout=$stdout_root"."$Xtimestamp            |     #
              #                 | cleanup_files="$cleanup"files $stdout_root"  |     #
              #                 |                                              |     #
              #                 | stderr_root=/tmp/$name_root"_stderr"         |     #
              #                 | stderr=$stderr_root"."$Xtimestamp            |     #
              #                 | cleanup_files="$cleanup"files $stderr_root"  |     #
              #                 | ...                                          |     #
              #                 | ..                                           |     #
              #                 | CLEANUP_WORK_FILES $cleanup_files            |     #
              #                 | -- or --                                     |     #
              #                 | CLEANUP_WORK_FILES -q $cleanup_files         |     #
              #                 +----------------------------------------------+     #
              #                                                                      #
              #              Because we specify $CWF_dir in the find command we use, #
              #              fully qualified fileids (with real paythnames) are an   #
              #              absolute requirement.                                   #
              #                                                                      #
              #     Globals: No global variables assigned from this function.        #
              #              "CWF_" prefix identifies local function variables.      #
              #                                                                      #
              # Exit_status: Returns with failure (1) on error (e.g. the user        #
              #              supplies an invalid argument).  Normally, this kind     #
              #              of error is tagged as a FATAL ERROR, however, the       #
              #              inability to remove files (even if it is the user's     #
              #              fault) is not earth shaking, so we only describe it     #
              #              as an ERROR and "return" failure (1).  Otherwise we     #
              #              return an exit status of success or failure (0 or 1).   #
              #                                                                      #
              #       Calls: EMAIL_MSG and CHDIR_EXIT_ERR library functions.         #
              #                                                                      #
              #       Notes: Instead of exiting on error (like insufficient args)    #
              #              this script returns failure (1).  This is done          #
              #              because this routine is most often called at the end    #
              #              of a a process where failing to erase old files is      #
              #              noteworthy (notification is sent), but hardly           #
              #              terminal.                                               #
              #                                                                      #
              #    Modified: 2004-04-02 Bob Orlando                                  #
              #                 v1.10 * Expand $AWK testing and assignment.          #
              #                                                                      #
              #              2004-03-03 Bob Orlando                                  #
              #                 v1.9  * Change set|egrep|awk to just set|egrep.      #
              #                                                                      #
              #----------------------------------------------------------------------#
            
Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1996-2011
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Jan. 26, 2011
by Bob Orlando