#!/usr/bin/perl

# Gregory D. Troxel <gtroxel@bbn.com>

# -n ==> don't do 'get' operation
while ($_ = $ARGV[0], /^-/) {
     shift;
    last if /^--$/;
     /^-n(.*)/ && ($dontget = 1);
}

do "sub-rsadj.pl";

# check for tickets
# &krb_tk_check;

foreach $host (@HOSTS)
  {
    print "GET $host:\n";

    &cdhost($host);

    if ( ! $dontget ) {
	# files from this host
	if ( $host eq $hostname ) {
	    system "cp -p /usr/adm/ntp-messages* /usr/adm/NTPDIR/ntp-messages* ." ;
	} elsif ( $host =~ /sgi/ ) {
	    system "/usr/ucb/rcp -p $host:/usr/adm/*SYSLOG ." ;
	} else {
	    system "/usr/ucb/rcp -p $host:/usr/adm/ntp-messages* ." ;
	}
    }

    @files = split(' ', `ls ntp-messages* syslog* *SYSLOG* 2>/dev/null`);

    foreach $file (@files)
      {
        print "Processing $file ";
	if ( $file =~ /.gz$/ ) {
	    system "gunzip $file";
	    $file =~ s/.gz$//;
	}
	open(LOGF, $file) || die "can't open $file";
	$input = <LOGF>;
	if ( $input eq "" )
	{
	  print "empty <$input>";	
	  system "rm -f $file";
        }
	else
        {
	  ($month, $day) = split(' ', $input);
	  $mon = $montab{$month};
	  # change the next line for the current year
	  # or do it right and mail me a patch - gtroxel@bbn.com
	  $year = $mon > 9 ? 94 : 95;
	  $storedfile = sprintf( "D.%02d%02d%02d", $year, $mon, $day);
	  $storedfilez = sprintf( "D.%02d%02d%02d.gz", $year, $mon, $day);
	  print "> $storedfile $storedfilez: " ;
	  system "rm -f $storedfile";
	  if ( ! -e $storedfilez )
	  {
	    print "doesn't exist; renaming";
	    rename($file, $storedfile);
	    system "gzip $storedfile";
	  }
	  else
	  {
	    $result = `zcmp $storedfilez $file 2>& 1`;
	    chop $result;
	    if ( $? == 0 )
	    { print "same"; unlink($file); }
	    elsif ( $result eq "cmp: EOF on -" )
	    {
	      print "Longer, moving";
	      rename($file, $storedfile);
	      system "gzip -f $storedfile";
	    }
	    else
	    {
	      print "\nCONFLICT on $file, $storedfilez: <$result>" ;
	    }
          }
	}
	close(LOGF);
	print "\n";
      }
  }

#exec "perl $procdir/prep-data @HOSTS" ;
