#!/usr/local/bin/perl

# 
# 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 files 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>).
#

do 'yagrip.pl' ||
  die "can't do yagrip.pl: !$";

$options = "d:";
$usage = "usage: rip_post [-d data_dir] host";
$data_dir = "rip_data";

#
# Parse JCL.
#

&getopt($options) ||
  die $usage;

if (defined($opt_d)) {
    $data_dir = $opt_d;
}

#
# Loop over each file
#

while (<>) {
    print;
    if (/^grant\|/) {
	($dummy, $post_h, $post_u, $pre_h, $pre_u, $text) = split(/[|]/);
	$grant_dst{$post_h} .= "$post_u|$pre_h|$pre_u|$text";
	$grant_src{$pre_h} .= "$pre_u|$post_h|$post_u|$text";
    }
}
foreach $host (keys %grant_dst) {
    $path = "$data_dir/grant_dst/$host";
    open(OUT, ">>$path") || die("append $path: $!");
    printf OUT $grant_dst{$host};
    close(OUT);
}
foreach $host (keys %grant_src) {
    $path = "$data_dir/grant_src/$host";
    open(OUT, ">>$path") || die("append $path: $!");
    printf OUT $grant_src{$host};
    close(OUT);
}
