#!/bin/sh

data_dir=rip_data
usage="Usage: $0 [-d data_dir] [file]"

while :
do
    case $1 in
    -d) data_dir=${2?"$usage"}; shift;;
    -*) $ECHO $usage 1>&2; exit 1;;
     *) break;;
    esac
    shift
done

# 
# Postprocessor for one single host output from the *.rip scripts. This
# script is run (1) when data is collected; (2) whenever a new bug
# advisory comes out, to update already existing host data files.
# 
# Stdin, a list of assertions about one host, is copied to stdout, and
# new assertions about the same host are added. In addition, the script
# updates two scripts for each host. One that says how to get into that
# host (grant_dst/<hostname>), and one that says where one can get from
# this host (grant_src/<hostname>).
#

AWK=/usr/bin/awk

exec $AWK -F'|' '
		{ print }
$1 == "grant" {
		grant_dst[$2] = grant_dst[$2] \
		    sprintf "%s|%s|%s|%s\n",$3,$4,$5,$6
		grant_src[$4] = grant_src[$4] \
		    sprintf "%s|%s|%s|%s\n",$5,$2,$3,$6
}
END {
		for (i in grant_dst) {
			path = "'"${data_dir}"'" "/grant_dst/" i
			print "cat >>" path "<<_E_O_F_" | "sh"
			print grant_dst[i]		| "sh"
			print "_E_O_F_"			| "sh"
		}
		for (i in grant_src) {
			path = "'"${data_dir}"'" "/grant_src/" i
			print "cat >>" path "<<_E_O_F_" | "sh"
			print grant_src[i]		| "sh"
			print "_E_O_F_"			| "sh"
		}
}
' $*
