#!/usr/bin/perl -w
$ttyst = "stty sane 9600 parenb cs8 -crtscts cstopb --file=/dev/ttyS0";

# test the tube digits on start up...
# this is on OpenSUSE so it uses /dev/ttyS0
open (TUBE, "+</dev/ttyS0") or die $!;
  select TUBE;
  $| = 1;
  foreach $x (qw/ 1 6 2 7 5 0 4 9 8 3 /) {
    $b = $x x 16;
    print TUBE $b;
    sleep 1;
  };
close TUBE;

sleep 10;

$oc = "w";
$ctr = 0;

while (1) {
  readpipe "wget -q -r http://1.2.3.4:19243/ginty.html";
  open (HTML, "<1.2.3.4:19243/ginty.html");
    $c = <HTML>;
  close HTML;
  chomp $c;
  if ($oc eq $c) {
    $ctr += 2;
    if ($ctr == 20) {
      open (TUBE, "+</dev/ttyS0") or die $!;
        print TUBE "          t";
      close TUBE;
    } elsif ($ctr == 40) {
      $ctr = 0;
      $c =~ s/ /0/g;
      open (TUBE, "+</dev/ttyS0") or die $!;
        print TUBE $c;
      close TUBE;
    }
  } else {
    $oc = $c;
    $ctr = 0;
    readpipe ($ttyst);
    open (TUBE, "+</dev/ttyS0") or die $!;
      print TUBE $c;
    close TUBE;
  };
  sleep 2;
}
