#!/bin/sh

# Daily jobs required for JFFNMS

# Source the defaults file
if [ -r /etc/default/jffnms ]; then
. /etc/default/jffnms
fi

# Delete temporary images older than a day
deltmpimages()
{
    IMAGEPATH=`cat /etc/jffnms/config.ini 2>/dev/null | perl -e 'while (<>) { if ($_ =~ /^\s*images_real_path\s*=\s*(\S+)/) { print "$1\n"; exit 0; }}'`
    if [ -z $IMAGEPATH ]; then
        echo "images_real_path not found in jffnms configuration file config.ini"
		return
	fi
	# Delete any .png .dot and .map files older than a day
    find $IMAGEPATH -type f  -mtime +1 -regex '.*\.\(png\|dot\|map\)$' -exec rm {} \;
}

deloldlogs()
{
    # Find the log directory out of configuration file
    LOGPATH=`cat /etc/jffnms/config.ini 2>/dev/null | perl -e 'while (<>) { if ($_ =~ /^\s*log_path\s*=\s*(\S+)/) { print "$1\n"; exit 0; }}'`

    if [ -z $LOGPATH ]; then
        echo "log_path not found in jffnms configureation file config.ini."
		return
    fi

    # Compress logs older than 2 days
    if [ -z $COMPRESSLOGS ]; then
      COMPRESSLOGS=2
    fi

    # Delete logs older than 7 days
    if [ -z $DELETELOGS ]; then
      DELETELOGS=7
    fi

    find $LOGPATH -type f  -mtime +$DELETELOGS -exec rm {} \;

    for fname in `find $LOGPATH -mtime +$COMPRESSLOGS -name '*.log'` ; do
      gzip $fname
    done
}

#
# START HERE
#

deloldlogs
deltmpimages
