#!/bin/bash
#shell script to automatically delete core and javacore files in system
#code mostly taken from skulker 1.31 bsh script (AIX)
#
#This script should be called from crontab once a week.

#first, the remove_file function -  
#Remove the file only if no process has it currently open
# NOTE: If /sbin/fuser is not on the HMC, the open file check is effectively bypassed
remove_file()
{
	if [ -z "`/sbin/fuser $1 2>/dev/null`" ]; then
	   /bin/rm -f $1 2>/dev/null
	fi
}

#skfile=/skulker.log
#date=`/bin/date`
#uname=`/bin/uname -nm`
#/bin/echo "$0 started at $date on $uname" >> $skfile


# Uncomment the NATIVE entry that is appropriate for your system.
# For Distributed environments, '/native' is a path to the local filesystems;
# '/' is sufficient for standalone systems.
# NATIVE=/native/
NATIVE=/

#clean out files in mail queues
if [ -d ${NATIVE}var/spool/mail ]
then
  for file in `/usr/bin/find ${NATIVE}var/spool/mail -type f 2>/dev/null`
  do
	remove_file $file
  done
fi
if [ "$1" == "now" ]
then
# No need to remove RMC trace. trace.something are files needed by RMC
# clean everything immediately
#  for file in `/usr/bin/find ${NATIVE}var/ct/IW/log/mc -name 'trace' -type f 2>/dev/null`
#  do 
#     remove_file $file
#  done

  if [ -d /opt/hsc/data/pedbg ]; then
     for i in /opt/hsc/data/pedbg/*.pdrm
     do
       if [ -f $i ]; then
          while read line
          do
             if [ "$line" != "" ]; then
               eval "$line"
             fi
          done < $i
       fi
     done
  fi
fi

# No need to do this anymore trace.something is needed by RMC
#clean out old tracefiles that have been stored for 10 days or more
#if [ -d ${NATIVE}var/ct/IW/log/mc ]
#then
#  for file in `/usr/bin/find ${NATIVE}var/ct/IW/log/mc -name 'trace' -mtime +10 -type f 2>/dev/null`
#  do 
#     remove_file $file
#  done
#fi

# Get rid of core files in any directory that are more than 2 weeks old,
# and that have not been accessed in 1 week. Remove javacore files
# and heapdump too.  Also remove these files regardless of age if the
# file system they reside on has less than 500M of available space.
# Other types of files may come later. 
#
# Use the -xdev flag to prevent find from traversing a filesystem
# on a different device, this will prevent it from searching nfs
# mounted filesystems.  You may want to add filesystems here
# that you want to be cleaned up.

#If there is no space left on a filesystem, then remove stuff immediately
for mntpoint in `/bin/df -l -m | /usr/bin/tail +2 | /bin/awk '{print $6}'`
do
#  echo `/bin/df "$mntpoint" -l -m | /usr/bin/tail +2 | /bin/awk '{print $4}'`
#  echo $mntpoint

  if [ `/bin/df "$mntpoint" -l -m | /usr/bin/tail +2 | /bin/awk '{print $4}'` -lt 500 ] 
  then
    for n in `/usr/bin/find "$mntpoint" \( -name core -o -name 'core.[0-9]*[0-9]' -o -name 'javacore*' -o -name 'heapdump.*.phd' \) -xdev -type f`
    do
	remove_file $n
    done
          
    if [ -e ${NATIVE}var/adm/wtmp -a "$mntpoint" = ${NATIVE}var ]
    then
      if [ `/usr/bin/du ${NATIVE}var/adm/wtmp | /bin/awk '{ print $1}'` -gt 0 ]
      then
        cat /dev/null > ${NATIVE}var/adm/wtmp
      fi
    fi 
#else, wait until cores and javacores are 2 weeks old and not viewed for 1 week to remove them
 else
    for n in `/usr/bin/find "$mntpoint" \( -name core -o -name 'core.[0-9]*[0-9]' -o -name 'javacore*' -o -name 'heapdump.*.phd' \) -xdev -atime +7 -mtime +14 -type f`
    do
	remove_file $n
    done
 fi
done

#date=`/bin/date`
#/bin/echo "$0 finished at $date on $uname" >> $skfile






