#!/bin/bash

source "/etc/asl/config"
export LANG="en_US.UTF-8"

if [ "$CONFIGURED" ==  "yes" ]; then
  # determine actual retention in days to use
  RET_DAYS=$HIDS_CLEAN_DIFF
  RET_DAYS_MODSEC=$MODSEC_CLEAN_ALERT
  if [ "$RETENTION_USE_CONSOLIDATED" == "yes" ]; then
    arr=($RETENTION_CONSOLIDATED)
    s_value=${arr[0]}
    s_period=${arr[1]}

    if [ "$s_period" == "month" ] || [ "$s_period" == "months" ]; then
      RET_DAYS=$((s_value * 30))
      RET_DAYS_MODSEC=$RET_DAYS
    elif [ "$s_period" == "year" ] || [ "$s_period" == "years" ]; then
      RET_DAYS=$((s_value * 365))
      RET_DAYS_MODSEC=$RET_DAYS
    elif [ "$s_period" == "day" ] || [ "$s_period" == "days" ]; then
      RET_DAYS=$s_value
      RET_DAYS_MODSEC=$RET_DAYS
    fi
  fi

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

  # Clear old alerts
  if [ $RET_DAYS_MODSEC -gt 0 ]; then
     /usr/bin/find /var/asl/data/audit/ -maxdepth 2 \
	-type d -ctime +$RET_DAYS_MODSEC -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
  fi


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


	# Update geo db
	if [ -d /usr/share/doc/xtables-addons-2.6/ ]; then
		/var/asl/bin/xt_geoip-sync >/dev/null 2>&1
	fi
else
  echo "Error: ASL has not been configured"
  exit 1
fi

