#!/usr/bin/env /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" ==  "hourly" ]; then
		sleep $WAIT
		/var/asl/bin/aum -u >/dev/null 2>&1

	else
		sleep $WAIT
		/var/asl/bin/aum -ck >/dev/null 2>&1
	fi

	#
	if [ -f /var/asl/data/msa/ip.dir ]; then
		cp /dev/null /var/asl/data/msa/ip.dir
	fi

	if [ -f /var/asl/data/msa/ip.pag ]; then
		cp /dev/null /var/asl/data/msa/ip.pag
	fi

	# AEO: agent cleanup (dead agents)
	if [[ "$HIDS_AGENT_KEEP_ALIVE" == "hourly" ]]; then
    if [ -f /var/asl/bin/agent_cleanup.sh ]; then
      if [[ "$HIDS_AGENT_KEEP_ALIVE_TYPE" =~ "Disconnected" ]]; then
        /var/asl/bin/agent_cleanup.sh -d
      fi

      if [[ "$HIDS_AGENT_KEEP_ALIVE_TYPE"  =~ "Never Connected" ]]; then
        /var/asl/bin/agent_cleanup.sh -nc
      fi

      if [[ "$HIDS_AGENT_KEEP_ALIVE_TYPE"  =~ "Active" ]]; then
        /var/asl/bin/agent_cleanup.sh -a
      fi
    fi
fi


	# generate reports
	if [ -f /var/asl/bin/asl ]; then
		  if [ -f /var/asl/tmp/.upgrade ]; then
			/sbin/service tortixd restart >/dev/null 2>&1
			/var/asl/bin/asl -s -f >/dev/null 2>&1
			/sbin/service ossec-hids restart > /dev/null 2>&1
			rm -f /var/asl/tmp/.upgrade
		  else
			/var/asl/bin/asl -s >/dev/null 2>&1
		  fi

		  # Run rep report
		  if [ "$REPUTATION_REPORT" == "yes" ]; then
			if [ "$REPUTATION_FREQUENCY" == "hourly" ]; then
				/var/asl/bin/asl --rep_report >/dev/null 2>&1
			fi
		  fi
	fi


else
  echo "Error: ASL has not been configured"
  exit 1
fi
