#!/bin/bash
# $Header: /cvs/WebShield/wsrc/files/mgmt/cleanold,v 1.2 2004/07/28 16:56:14 bwhittak Exp $
#
# Copyright (C) 2004 Networks Associates Technology Inc. All rights reserved.
#
# Remove dead files that may be left lying around by accident
#
if [ -f $NETAWSS/.profile.vars ]; then
    . $NETAWSS/.profile.vars
else
    . /var/NAIENV/.profile.vars
fi
[[ "$PATH" == "$wsPATH":* || -z "$wsPATH" ]] || PATH=$wsPATH:$PATH

exec </dev/null		# don't need any inputs
shopt -s extglob	# fancy globbing
shopt -s dotglob	# match .names like any others
shopt -s nullglob	# if glob pattern doesn't match use nothing

declare -i tshort=0	# short timeout (as for csmap working directory)
declare -i tmed=7	# medium timeout (as for tmp and epo queue)
declare -i tlog=30	# log file timeout default (read from config)
declare -i txml=30	# xml log timeout default (read from config)
declare -i tdef=5	# deferred mail timeout (read from config)

# source the XWTrans element designator strings
. shvars-xmlconf 2>/dev/null && gotStrings=true || gotStrings=false

# my name
me=${0##*/}
# a file to log our deletions
logfile="/var/log/$me.$(date +'%Y-%m-%d')"

#
###### functions ######
#

# function for logging
Log()
{
    logger -t "$me[$$]" -- "$*"
    echo "$(date +'%T')[$$]: $*" >>$logfile
}
# function to delete files and report the fact
Delete()
{
    local f s
    for f; do
	# don't delete an open file whatever else
	[[ -n "$(fuser "$f" 2>/dev/null)" ]] && continue
	# get it's status
	if s=$(ls -ld "$f"); then
	    # delete it
	    rm -f "$f" 2>/dev/null || rmdir "$f" 2>/dev/null && \
		Log "deleted: $s"
	fi
    done
}
# function to Delete files read from stdin except those listed as args
DeleteExcept()
{
    local IFS=$'\n'
    local f
    # "$*" expands to the args separated by the first char of $IFS
    # thus each arg is a separate pattern for fgrep so it excludes
    # from the stdin stream any line exactly matching any arg
    fgrep -v -x -e "$*" | while read f; do Delete "$f"; done
}

# function to delete non-directories based on mtime
HuntM()
{
    local -i t=$1
    shift
    [[ $# -gt 0 ]] && find "$@" ! -type d -mtime +$t -print 2>/dev/null |
				DeleteExcept "$@"
}
# function to delete files based on atime and directories based on mtime
HuntA()
{
    local f
    local -i t=$1
    shift
    [[ $# -gt 0 ]] && find "$@" -depth -type d -mtime +$t -print -o \
			    -atime +$t -print 2>/dev/null |
				DeleteExcept "$@"
}

# function to clean the deferred mail queues carefully
CleanDefer()
{
    if [[ $# -gt 0 ]]; then
	local tt
	if tt=$(GetXMLvalue /config/wsxmlconf/smtp.xml \
	    "//Settings[@name='retryer']/Attr[@name='RetryLifeTime']/@value") &&
	    [[ -n $tt ]]
	then
	    ((tdef=(${tt//[^0-9]}+86400)/60/60/24))
	fi
	# remove obviously old files
	HuntM $tdef "$@" 
	# remove junk retryer won't touch
	find "$@" -type f -mtime +$tshort -print |
	    while read f; do
		# file and file.rmd come in pairs - delete if partner missing
		case "$f" in
		(*.rmd)
		    [[ -f "${f%.rmd}" ]] || Delete "$f"
		    ;;
		(*)
		    [[ -f "$f.rmd" ]] || Delete "$f"
		    ;;
		esac
	    done
    fi
}

#
###### execution ######
#

# Get data from config files

# get XML log timeout from channels config
if tt=$(GetXMLvalue /config/wsxmlconf/channels.xml \
	"//ReportingConfig/Channel[@name='xml']/Default/@age") && [[ -n $tt ]]
then
    txml=${tt//[^0-9]}+1
fi
tlog=$txml	# other logs same as XML

# Start the cleanup for real

# clean out /tmp and the ePO events directory with medium timeout
HuntA $tmed /tmp /var/tmp
HuntM $tmed /logs/NETAepoagt/AgentDB/Event /logs/NETAepoagt/etc

# clean out dead XML log files still left around
if [[ $txml -gt 1 && -d "$LOGDIR" ]]; then
    HuntM $txml "$LOGDIR"
fi

# clean log files
HuntM $tlog /var/log

# clean quarantine (using the log file timeout since there isn't one for this)
#HuntA $tlog "${quarD[@]}"

# clean the deferred mail queues carefully
CleanDefer /deferred/smtp /deferred/logging /quarantine/smtp
