#!/bin/sh

# needed to get the network_get_ipaddr function
source /lib/functions/network.sh

# to enable this script uncomment the case loop at the bottom
# to report the interface up/down events modify the lines in the send_reportdata function

send_reportdata()
{
	# $1 stores the interface up/down event report data
	# insert your code here to report the contents of $1
	echo "$1"
}

search_rules()
{
	local SRC_IP SRC_PORT DEST_IP DEST_PORT PROTO USE_POLICY EQUALIZE
	config_get SRC_IP "$1" src_ip
	config_get SRC_PORT "$1" src_port
	config_get DEST_IP "$1" dest_ip
	config_get DEST_PORT "$1" dest_port
	config_get PROTO "$1" proto
	config_get USE_POLICY "$1" use_policy
	config_get EQUALIZE "$1" equalize
		[ "$EQUALIZE" == "1" ] && EQUALIZE="Yes" || EQUALIZE="No"
	[ "$(echo "$POLICYLIST" | grep -c "$USE_POLICY")" != "0" ] && {
		RULELIST=""$RULELIST"++"$1":+source*address*-*"$SRC_IP"+source*port*-*"$SRC_PORT"+destination*address*-*"$DEST_IP"+destination*port*-*"$DEST_PORT"+protocol*-*"$PROTO"+policy*assigned*-*"$USE_POLICY"+equalize*-*"$EQUALIZE""
	}
}

search_policies()
{
	local USE_MEMBER
	config_get USE_MEMBER "$1" use_member
	[ "$USE_MEMBER" ] && USE_MEMBER="$(echo "$USE_MEMBER" | tr -d '\n' | sed 's/ /\n/g')"
	for LINE in $USE_MEMBER; do
		[ "$(echo "$MEMBERLIST" | grep -c "$LINE")" != "0" ] && {
			POLICYLIST=""$POLICYLIST"++"$1":+"$USE_MEMBER""
			break
		}
	done
}

search_members()
{
	local IFACE
	config_get IFACE "$1" interface
	[ "$INTERFACE" == "$IFACE" ] && MEMBERLIST=""$MEMBERLIST"+"$1""
}

find_status()
{
	RULENUM=$((RULENUM+1))
	local ENABLED TRACK_IP
	config_get ENABLED "$1" enabled
	config_get TRACK_IP "$1" track_ip
	local CURRENT_IP
		network_get_ipaddr CURRENT_IP "$1"
		if [ "$CURRENT_IP" != "" ]; then
			CURRENT_IP="*with*IP*$CURRENT_IP"
		fi
	local STATUS
		if [ "$ENABLED" == "1" ]; then
			[ "$TRACK_IP" ] && STATUS="(monitored)" || STATUS="(not monitored)"
		else
			STATUS="(not enabled)"
		fi
	local RULE="$(ip route list table "$RULENUM")"
	if [ "$RULE" ]; then
		CURRENTSTATUS=""$CURRENTSTATUS"+"$1"*-*ONLINE*"$STATUS""$CURRENT_IP""
	else
		CURRENTSTATUS=""$CURRENTSTATUS"+"$1"*-*OFFLINE*"$STATUS""$CURRENT_IP""
	fi
}

report_event()
{
	config_load mwan3

	# create event information message
	local EVENTINFO="Interface [ "$INTERFACE" ($DEVICE) ] on router [ "$(uci get -p /var/state system.@system[0].hostname)" ] has triggered an [ "$ACTION" ] hotplug event on "$(date)""

	# get current status of all configured MWAN3 interfaces
	CURRENTSTATUS="---------------+CURRENT*STATUS:+---------------+"
	RULENUM=1000
	config_foreach find_status interface
	CURRENTSTATUS="$(echo "$CURRENTSTATUS" | sed -e 's/+/\n/g' -e 's/\*/ /g')"

	# create list of affected members
	MEMBERLIST="-----------------+MEMBERS*AFFECTED:+-----------------+"
	config_foreach search_members member
	MEMBERLIST="$(echo "$MEMBERLIST" | sed -e 's/+/\n/g' -e 's/\*/ /g')"

	# create list of affected policies
	POLICYLIST="------------------+POLICIES*AFFECTED:+------------------"
	config_foreach search_policies policy
	POLICYLIST="$(echo "$POLICYLIST" | sed -e 's/+/\n/g' -e 's/\*/ /g')"

	# create list of affected rules
	RULELIST="---------------+RULES*AFFECTED:+---------------"
	config_foreach search_rules rule
	RULELIST="$(echo "$RULELIST" | sed -e 's/+/\n/g' -e 's/\*/ /g')"

	# get last 50 mwan3 systemlog messages
	local MWAN3LOG="$(echo "------------------------------------------------------------------+Last*50*MWAN3*systemlog*entries.*Newest*entries*sorted*at*the*top:+------------------------------------------------------------------++$(logread | grep mwan3 | tail -n 50 | sed 'x;1!H;$!d;x')" | sed -e 's/+/\n/g' -e 's/\*/ /g')"

	# combine data into single variable for reporting
	local REPORTDATA="$(echo "$EVENTINFO,$CURRENTSTATUS,$MEMBERLIST,$POLICYLIST,$RULELIST,$MWAN3LOG" | sed 's/,/\n\n/g')"

	# report value of variable $REPORTDATA
	send_reportdata "$REPORTDATA"
}

#case "$ACTION" in
#	ifup)
#		report_event
#	;;
#
#	ifdown)
#		report_event
#	;;
#esac

exit 0
