#!/usr/bin/wsrunxslt /config/wsxmlconf/system.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
	Script for cleaning a directory of old files
	Copyright (C) 2004 Networks Associates Technology Inc.
	All rights reserved.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:param name="param_0"></xsl:param>

<xsl:template match="/">
<xsl:text>
Log="/var/log/cleanactive.$(date +'%Y-%m-%d')"
# function to delete files and report the fact
Delete()
{
    local f s
    for f; do
	# don't delete an open file whatever else
	[[ -n "$(lsof -t "$f" 2>/dev/null)" ]] &amp;&amp; continue
	# get it's status
	if s=$(ls -ld "$f"); then
	    # delete it
	    rm -f "$f" 2>/dev/null || rmdir "$f" 2>/dev/null &amp;&amp;
		echo "$(date +'%T'): $s" >>$Log
	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 files based on atime (and ctime)
HuntA()
{
   find "${1}" -type f -amin +${2} -cmin +${3} -print 2>/dev/null |
	    DeleteExcept "${1}"
    return 0
}
</xsl:text>

    <xsl:for-each select="//GlobalSettings[@name='system']/Policy[@name='system_variables']/PolicyStatement/Settings[@name='dir-clean']/NetObjectList[@name='dirlist']">
       <xsl:call-template name="directory"/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="directory">
    <xsl:for-each select="NetObject[@type='DISKCLEAN']">
       <xsl:call-template name="clean-dir"/>
    </xsl:for-each>
</xsl:template>

<xsl:template name="clean-dir">
    <xsl:choose>
        <xsl:when test="Enabled/@value='1'">
            <xsl:text>HuntA </xsl:text>
            <xsl:value-of select="Directory/@value"/><xsl:text> </xsl:text>
            <xsl:value-of select="LastAtime/@value"/><xsl:text> </xsl:text>
            <xsl:value-of select="LastCtime/@value"/><xsl:text> </xsl:text>
            <xsl:text>&#10;</xsl:text>
        </xsl:when>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

