#!/usr/bin/perl
# logres.pl
# replace an httpd log file with a new file,
#  after resolving numeric IP addresses into host names

if ($#ARGV != 0) {
    die "usage: logres.pl infile\n";
} elsif ($#ARGV == 0) {
    $infile=$ARGV[0];
}

$| = 1;					# unbuffered output
$LOGRES='/opt/ACISweb/bin/getstats/logres';
$LOGRES='/usr/local/bin/logres' unless -x $LOGRES;
die "can't find logres\n" unless -x $LOGRES;

$outfile=$infile; # output file is just input file without .wrk
die "input file must contain .wrk\n" unless $outfile =~ s|\.wrk||;
die "$outfile exists\n" if -e $outfile;

# use logres to resolve IP addresses into host names
# -k   means remove the input file after processing
# -r   means don't overwrite the hash file

&execute($LOGRES, '-k',  '-i',  $infile, '-o', $outfile);




sub execute {			# run a program and check the return code
    local(@args) = @_;
    local($rc) = 0xffff & system @args;
    if ($rc > 0x80) {
	$rc >>= 8;
	printf "%s\n  failed with return code $rc\n", "@args";
	exit(3);
    } elsif ($rc) {
	if ($rc & 0x80) {
	    $rc &= ~0x80;
	    print "coredump written\n";
	}
	printf "%s\n  aborted with signal $rc\n", "@args";
	exit(3);
    }
}
