#!/bin/sh -fu

TARGET_OSEC_CONF_DIR='/etc/osec'

LOG_FACILITY="${LOG_FACILITY:-user}"
BASE_LOG_PRIORITY="${BASE_LOG_PRIORITY:-info}"
ALERT_LOG_PRIORITY="${ALERT_LOG_PRIORITY:-alert}"

_integalert_action="${INTEGALERT_ACTION:-check}"

if [ -e "$TARGET_OSEC_CONF_DIR"/integalert.conf ]; then
   . "$TARGET_OSEC_CONF_DIR"/integalert.conf
fi
if [ -e /etc/sysconfig/integalert ]; then
   . /etc/sysconfig/integalert
fi
if [ -e "$TARGET_OSEC_CONF_DIR/$PROFILE"/sender.conf ]; then
   . "$TARGET_OSEC_CONF_DIR/$PROFILE"/sender.conf
fi

INTEGALERT_ACTION="$_integalert_action"

case "$SUBPROFILE" in
    main)
	mode_clause=
	;;
    *)
	mode_clause=", $SUBPROFILE mode"
	;;
esac

# Pipe out the report to log file:
echo "=========== $(date) ===========" >"$REPORT_LOG"
cat >>"$REPORT_LOG"
echo "System integrity ${INTEGALERT_ACTION}$mode_clause ($STAT)" >>"$REPORT_LOG"

priority="$BASE_LOG_PRIORITY"
[ "$ADDED" = '0' ] || priority=$ALERT_LOG_PRIORITY
[ "$REMOVED" = '0' ] || priority=$ALERT_LOG_PRIORITY
[ "$CHANGED" = '0' ] || priority=$ALERT_LOG_PRIORITY

# Write a summary message to the system log:
logger -t "$LOG_TAG" -p "$LOG_FACILITY.$priority" \
    "System integrity ${INTEGALERT_ACTION}$mode_clause ($STAT)"
