#!/usr/bin/perl

# detach
chdir("/");
if($pid = fork()) {
  # parent
  exit;
}

open(LOG,"tail -f /usr/adm/everything |") || die $!;

open(MSGS,">>/usr/adm/messages") || die $!;
select((select(MSGS), $| = 1)[0]);

open(TTY8,">/dev/tty8") || die $!;
open(TTY10,">/dev/tty10") || die $!;

$last = 0;

while(<LOG>) {
  if(/syslogd/) { print MSGS ; next; }

  if(/last message repeated/) {
    next if(! $last);

    if($last == 8) { print MSGS ; print TTY8 ; }
              else { print TTY10 ; }
    next;
  } else {
    $last = 0;

    ($date, $time, $machine, $process, $message) =
     /^(\w+\s+\d+) (\d\d:\d\d:\d\d) (\S+) ([^:]+): (.+)$/;
    next if ($process =~ /^ftpd/); # webcam uploads
    next if ($process =~ /^wu.ftpd/); # webcam uploads
    next if ($process =~ /^in.comsat/); # biff
    if($process =~ /^named/) {
      print TTY10 ;
      $last = 10;
      next;
    }
  }
  print MSGS ;
  print TTY8 ;

  $last = 8;
}
