#!/bin/sh

. /etc/sysconfig/ipoffice

function clean() {
while read output;
do
  used=$(echo $output | awk '{print $1}' | cut -d'%' -f1)
  if [ $used -ge $DISK_LOAD_MAX ] ; then
     echo "Running out of space ($used%)"
     echo "cleanup method:$DISK_CLEANUP_METHOD"
     if [ $DISK_CLEANUP_METHOD = "archive" ]; then
        sh /opt/Avaya/scripts/core_archive.sh 
        sh /opt/Avaya/scripts/logs_archive.sh
        new_used=$(df /var | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5}' | cut -d'%' -f1)
        if [ $new_used -ge $DISK_LOAD_MAX ]; then
            fileno=$(ls arc* | wc -l)
            let fileno=$fileno/$DISK_CLEANUP_RATIO
            ls -C1 -t arc* | awk 'NR>"'"$fileno"'"' | xargs rm -f
        fi
     elif [ $DISK_CLEANUP_METHOD = "delete" ]; then
        sh /opt/Avaya/scripts/core_delete.sh
        sh /opt/Avaya/scripts/logs_delete.sh
    fi
  fi
done
}

#df /var | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5}' | clean

