# -*- tab-width: 4 -*- ;; Emacs
# vi: set filetype=sh tabstop=8 shiftwidth=8 noexpandtab :: Vi/ViM
############################################################ IDENT(1)
#
# $Title: dwatch(8) gource module for VOP_*(9) activity $
# $Copyright: 2014-2018 Devin Teske. All rights reserved. $
# $FrauBSD: dwatch-gource/gource-vfs 2018-05-29 21:51:31 +0000 freebsdfrau $
#
############################################################ DESCRIPTION
#
# Produce gource custom log format for filesystem activity
#
############################################################ PROBE

_RAW_PROFILE=gource-vfs-raw
load_profile $_RAW_PROFILE

############################################################ GLOBALS

: ${_DEBUG=}

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

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

info "Watching '$PROBE' ..."
eval dwatch $ARGV -qFX $_RAW_PROFILE | awk -v debug=$_DEBUG '
	BEGIN {
		date = "[[:digit:]]+ [A-Z][a-z][a-z] [0-9 ][0-9]"
		time = "[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"
		stderr = "/dev/stderr"
	}
	debug { printf "DEBUG: %s\n", $0 > stderr; fflush(stderr) }
	length(datetime = substr($0, 1, 20)) != 20 { next }
	datetime !~ "^" date " " time "$" { next }
	(epoch = $9) !~ /^[[:digit:]]+/ { next }
	(execname = $6) && sub(/\[[[:digit:]]+\]:$/, "", execname) {
		if ((probe = $8) ~ /remove|rm/) type = "D"
		else if (probe ~ /lookup/) type = "M"
		else type = "A"
		sub(/.*:entry [[:digit:]]+ /, "")
		printf "%u|%s|%s|%s\n", epoch, execname, type, (path = $0)
		fflush()
	}
' # END-QUOTE

exit $SUCCESS

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