############################################################################
# Author: Amos Geng
# Date: 09/17/2014
# Purpose: Deleting all the audit file older than retention
# Parameter:    period -> retention got reduced for each time
#               retention_start -> should around the oldest file
#               count_threshold -> maximum files account for each period
#               retention -> delete by the day
############################################################################ 

#!/bin/sh

period=5
retention_start=110
count_threshold=50000
retention=60

adump_path=`sqlplus -s / as sysdba << ENDSQL
                SET HEA OFF PAGES 0
                SET LINESIZE 150
                SET FEEDBACK OFF
                SET ECHO OFF
                SELECT value FROM v\\$parameter
                WHERE name = 'audit_file_dest';
                EXIT;
ENDSQL`

echo $adump_path;

while [[ "$retention_start" -ge "$retention" ]]
do
        count=`find $adump_path -name "*.aud" -mtime +${retention_start} | wc -l`

        if [[ "$count" -lt "$count_threshold" ]];
        then
                find $adump_path -name "*.aud" -mtime +${retention_start} -exec rm {} \;
                echo "`date`: Found $count files in $retention_start period, and deleted all of them.";
        else
                echo "`date`: The audit file count is $count of period $retention_start. The number beyonds threshold $count_threshold, please check."
                exit;
        fi;

        retention_start=$((retention_start-period));
done

echo "The clean up job is done";