#!/usr/bin/perl
# $Id: decode,v 1.1 1998/02/09 13:44:14 puhuri Exp puhuri $
#

$decode = shift @ARGV;

$decode = "gunzip -c " . $decode . "|"
    if ($decode =~ m/\.(gz|Z)$/);

open(D, $decode) || die "Decode db ($decode) failed $!";
while(<D>) {
  if (($db, $val, $value) = m/^(\S+)\t(\d+)\t(\S.*)$/) {
    $db =~ tr/a-zA-Z0-9/_/c;
    eval "\@$db\[$val\] = '$value'\n" || die "$!";
  } elsif (($bt) = m/^basetime: (\d*\.\d*)$/) {
    $basetime = $bt;
  }
}

if ($#ARGV < 0) {		# stdin
  $readstdin = 1;
  open (IN, "-") || die "Cannot (re)open STDIN";
} else {
  $readstdin = 0;
  $file = shift @ARGV;
  $file = "gunzip -c " . $file . "|"
    if ($file =~ m/\.(gz|Z)$/);
  open (IN, $file) || die "Cannot open $file: $!";
  warn "Excess arguments (" . join(" ", @ARGV) . ") ignored\n"
      if ($#ARGV >= 0);
}

while(<IN>) {
  ($ms, $elapsed, $size, $clientID, $uriID, $protoID, $uriHostID, $servID, 
   $code, $methodID, $actionID, $hierID, $contentID)
      = split "\t";
  print join (" ", $ms + $basetime, $elapsed, $clientID,
	      $actions[$actionID] . "/" . $code, $size, 
	      $methods[$methodID], 
	      ($protoID == 0?$uriID:$protocols[$protoID] . "://" . $uriHostID . "/" . $uriID),
	      "-", $hierarchies[$hierID] . "/" . $servID,
	      $content_type[$contentID]), "\n";
}
