#!/usr/bin/perl -w
# collect videotext pages       (c) 1999 Rolf Bleher <dk7in@qsl.net>
# startet via crontab entry

# todo:   setuid

#--------------------------------------------------------------------------

#         station   sample time in sec
@chan  = (("n-tv",     30),
          ("ARD",     150),
          ("ZDF",     150),
          ("ORB",     150),
          ("B1",      150),
          ("3sat",    150),
          ("RTL2",    200),
          ("VOX",     300),
          ("Pro7",    200),
          ("N3",      120),
          ("WDR",     120),
          ("mdr",     120),
          ("BR",      300),
          ("Phoenix", 120),
          ("RTL",     200),
          ("Premiere",350),
          );

#--------------------------------------------------------------------------

$SPOOL   = "/var/spool/vtx";
$VBI     = "/usr/local/bin/vbi";
$TV      = "/usr/local/bin/set-tv";
$dwait   = 150;                             # max wait before vbi refresh
$waitntv = 30;                              # refresh time for n-tv vbi

#--------------------------------------------------------------------------

system("rm $SPOOL/ARD_ZDF/*");              # clear ARD/ZDF common dir
while ($#chan > 0) {                        # for all stations...
    vbion();                                # start vbi if not running
    $stn  = shift @chan;
    $wait = shift @chan;
    while ($wait > 0) {
      system("$TV $stn");                   # set station
      $time = ($wait>$dwait)?$dwait:$wait   # min()
      $wait -= $time;
      sleep($time);                         # wait
      rfshntv($waitntv);                    # refresh n-tv Videotext
    }
    if ($stn eq 'ARD') {
        system("mv -f $SPOOL/ARD_ZDF/* $SPOOL/Ard");
    }
    if ($stn eq 'ZDF') {
        system("mv -f $SPOOL/ARD_ZDF/* $SPOOL/Zdf");
    }
}
exit 0;

#--------------------------------------------------------------------------

sub vbion () {
  $vbirun = system("$VBI test");            # is vbi running ?
  if ($vbirun == 0) {
    print("Starting vbi...\n");
    system("$VBI on");                      # start vbidecode
    sleep(60);
  } else {
    print("vbi already running...\n");
  }
}

#--------------------------------------------------------------------------

sub rfshntv ($) {                           # refresh n-tv Videotext
  my ($wait) = @_;
  system("$TV n-tv");                       # set station to n-tv
  sleep($wait);                             # wait
}

#--------------------------------------------------------------------------
