#!/bin/bash

source "/etc/asl/config"
export LANG="en_US.UTF-8"
WAIT=$(echo $RANDOM | cut -c1-3)


if [ "$CONFIGURED" ==  "yes" ]; then

  # Automatic Updates
  if [ "$AUTOMATIC_UPDATES" ==  "daily" ]; then
        sleep $WAIT
	/var/asl/bin/aum -u >/dev/null 2>&1
  fi

  # Clear old alerts
  if [ $MODSEC_CLEAN_ALERT -gt 0 ]; then
     /usr/bin/find /var/asl/data/audit/ -maxdepth 2 \
	-type d -ctime +$MODSEC_CLEAN_ALERT -exec /bin/rm -rf {} \; >/dev/null 2>&1
  fi

  # Clean old updates
  /usr/bin/find /var/asl/updates -maxdepth 1 \
	-type f -ctime +7 -exec /bin/rm -f {} \; >/dev/null 2>&1

  # Clean old state files
  if [ -d /var/ossec/queue/diff ]; then
  	/usr/bin/find /var/ossec/queue/diff/*/533 -maxdepth 1  -type f -ctime +1 -exec /bin/rm -f {} \; >/dev/null 2>&1
  	/usr/bin/find /var/ossec/queue/diff/* -name state* -type f -ctime +$HIDS_CLEAN_DIFF -exec /bin/rm -f {} \; >/dev/null 2>&1
  	/usr/bin/find /var/ossec/queue/diff/* -name diff* -type f -ctime +$HIDS_CLEAN_DIFF -exec /bin/rm -f {} \; >/dev/null 2>&1
  fi

  # Clean old malware scan reports
  /usr/bin/find  /var/asl/reports -name *.log type f -ctime +$HIDS_CLEAN_DIFF -exec /bin/rm -f {} \; >/dev/null 2>&1

  # Run DB rotate script
  if [ -f /var/asl/bin/asl_db_rotate ]; then
  	/var/asl/bin/asl_db_rotate >/dev/null 2>&1
  fi

  # Run rep report
  if [ "$REPUTATION_REPORT" == "yes" ]; then
  	if [ "$REPUTATION_FREQUENCY" == "daily" ]; then
		/var/asl/bin/asl --rep_report >/dev/null 2>&1
	fi
  fi
  
else
  echo "Error: ASL has not been configured"
  exit 1
fi

