#------------------
#TimeStamp Function
#------------------
TimeStamp()
{
echo "------------------------------------------------------------" >> /var/log/LogsCleanup.log
date >> /var/log/LogsCleanup.log
echo "------------------------------------------------------------" >> /var/log/LogsCleanup.log
}

# --------------------
# Triggering logrotate
# -------------------- 
 /usr/sbin/logrotate /etc/logrotate.conf

# ------------------------------------------------------------
#Finding and deleting Files that are greater that 16MB of size
# ------------------------------------------------------------
 result=$(find /var/log -size +16777216c)
 for process in $result; do
    /nn/bin/AddNnuDiagLog -mLogsCleanup -sE -wN -v11250 "The size of $process is greater than 16MB, $process will be deleted to recover /var/log partition"

    #keeps first 20 lines of the file
    head -20 $process > $process.head
    rm -f $process
 done

# -----------------------------------------------------------
#Finding and rotating Files that are greater that 5MB of size
# -----------------------------------------------------------
 head -12 /etc/logrotate.conf > /var/log/logrotate.conf
 echo "/var/log/[^v]*.* {" >> /var/log/logrotate.conf
 echo "rotate 5"  >> /var/log/logrotate.conf
 echo "size 6M"   >> /var/log/logrotate.conf
 echo "missingok" >> /var/log/logrotate.conf
 echo "}"         >> /var/log/logrotate.conf

 /usr/sbin/logrotate /var/log/logrotate.conf
 rm -f /var/log/logrotate.conf

# -------------------------------
# Searching and deleting old logs
# -------------------------------
#---------------------------------------------------
# Cleaning *.Log.5
#---------------------------------------------------
 ls /var/log/[^c]*.5* > /dev/null 2>&1
 if [ $? -eq 0 ]; then
    # Logging the Deleted Files
    TimeStamp
    ls /var/log/[^c]*.5* >> /var/log/LogsCleanup.log
    rm -f /var/log/[^c]*.5* /var/log/cron*.5*
    #echo "*.5 Cleaned"

#---------------------------------------------------
# Cleaning *.Log.4
#---------------------------------------------------
 else  
    ls /var/log/[^c]*.4* > /dev/null 2>&1
    if [ $? -eq 0 ]; then
       # Logging the Deleted Files
       TimeStamp
       ls /var/log/[^c]*.4* >> /var/log/LogsCleanup.log
       rm -f /var/log/[^c]*.4* /var/log/cron*.4*
       #echo "*.4 Cleaned"

#---------------------------------------------------
# Cleaning *.Log.3
#---------------------------------------------------
    else  
       ls /var/log/[^c]*.3* > /dev/null 2>&1
       if [ $? -eq 0 ]; then
          # Logging the Deleted Files
          TimeStamp
          ls /var/log/[^c]*.3* >> /var/log/LogsCleanup.log
          rm -f /var/log/[^c]*.3* /var/log/cron*.3*
          #echo "*.3 Cleaned"

#---------------------------------------------------
# Cleaning *.Log.2
#---------------------------------------------------
       else 
          ls /var/log/[^c]*.2* > /dev/null 2>&1
          if [ $? -eq 0 ]; then
             # Logging the Deleted Files
             TimeStamp
             ls /var/log/[^c]*.2* >> /var/log/LogsCleanup.log 
             rm -f /var/log/[^c]*.2* /var/log/cron*.2*
             echo "*.2 Cleaned"
          #else
             #echo
             #echo "LogsCleanup done"
          fi
       fi
    fi
 fi
