# -*- tab-width: 4 -*- ;; Emacs
# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: dwatch(8) JSON module for network activity $
# $Copyright: 2014-2018 Devin Teske. All rights reserved. $
# $FrauBSD: dwatch-json/json-net-config 2018-10-01 15:01:11 -0700 freebsdfrau $
#
############################################################ DESCRIPTION
#
# Produce JSON custom log format for network activity
#
############################################################ GLOBALS

eval "$( echo "$EVENT_TEST" | awk -v argv="$ARGV" '
	function set(var, value)
	{
		gsub(/'\''/,"&\\\\&&", value)
		printf "%s='\''%s'\''\n", var, value
	}
	$0 { buf = buf $0 }
	END {
		#
		# EVENT_TEST for -k "ssh"
		# execname == "ssh"
		#
		head = buf
		tail = ""
		while (match(head, /execname == "[^"]*"/)) {
			tail = substr(head, 1, RSTART - 1)
			repl = substr(head, RSTART, RLENGTH)
			head = substr(head, RSTART + RLENGTH)
			if (sub(/ \|\|[[:space:]]*\(/, "", head))
				sub(/\)$/, "", head)
			sub(/^execname == "/, "", repl)
			sub(/"$/, "", repl)
			argv = argv " -k \"" repl "\""
		}
		buf = tail head

		#
		# EVENT_TEST for -k "ssh*"
		# strstr(execname, "ssh") == execname
		#
		head = buf
		tail = ""
		while (match(head, "strstr\\(execname, \"[^\"]*\"\\) == " \
			"execname")) \
		{
			tail = substr(head, 1, RSTART - 1)
			repl = substr(head, RSTART, RLENGTH)
			head = substr(head, RSTART + RLENGTH)
			if (sub(/ \|\|[[:space:]]*\(/, "", head))
				sub(/\)$/, "", head)
			sub(/^strstr\(execname, "/, "", repl)
			sub(/"\) == execname$/, "", repl)
			argv = argv " -k \"" repl "*\""
		}
		buf = tail head

		#
		# EVENT_TEST for -k "*ssh"
		# strstr(execname, "ssh") == (execname + strlen(execname) - 3)
		#
		head = buf
		tail = ""
		while (match(head, "strstr\\(execname, \"[^\"]*\"\\) == " \
			"\\(execname \\+ strlen\\(execname\\) - " \
			"[[:digit:]]+\\)")) \
		{
			tail = substr(head, 1, RSTART - 1)
			repl = substr(head, RSTART, RLENGTH)
			head = substr(head, RSTART + RLENGTH)
			if (sub(/ \|\|[[:space:]]*\(/, "", head))
				sub(/\)$/, "", head)
			sub(/^strstr\(execname, "/, "", repl)
			sub("\"\\) == \\(execname \\+ strlen\\(execname\\)" \
				" - [[:digit:]]+\\)", "", repl)
			argv = argv " -k \"*" repl "\""
		}
		buf = tail head

		set("EVENT_TEST", buf)
		set("ARGV", argv)
	}
' )"

############################################################ PROBE

_RAW_PROFILE=$PROFILE-raw
load_profile $_RAW_PROFILE

############################################################ MAIN

if [ "$DEBUG$EXIT_AFTER_COMPILE" ]; then
	eval dwatch $ARGV -qX $_RAW_PROFILE
	exit
fi

info "Watching '$PROBE' ..."
eval dwatch $ARGV -qX $_RAW_PROFILE $PROBE_ARG | awk '
	sub(/^.*: /, "") { print; fflush() }
' # END-QUOTE
exit $SUCCESS

################################################################################
# END
################################################################################
