#!/usr/bin/perl -w
###########################################
# Mike Schilli, 2006 (m@perlmeister.com)
###########################################
use strict;
use CGI qw(:all);
use DB_File;
use Template;

my %states = (
  idle  => 'green',
  check => 'yellow',
  busy  => 'red',
);

tie my %store, "DB_File", 
  "data/phonewatch.dat" or die $!;

$store{state} = "idle" unless 
    defined $store{state};

print header();

my $new = param('state');
if($new and exists $states{$new}) {
    $store{state} = $new;
}

my $tpl = Template->new();
$tpl->process(\ join('', <DATA>),
  { bgcolor => $states{$store{state}},
    state   => $store{state},
    self    => url(),
  }) or die $tpl->error;

###########################################
__DATA__
<HEAD>
  <META HTTP-EQUIV="Refresh" 
        CONTENT="30; 
            URL=[% self %]">
</HEAD>
<BODY>
  <H1>Phone Monitor</H1>
  <TABLE CELLPADDING=5>
    <TR>
      <TD BGCOLOR="[% bgcolor %]">
        Status: <b>[% state %]</b>
      </TD>
      [% IF state == "idle" %]
      <TD>
        <A HREF="[% self %]?state=check">
        check</A>
      </TD>
      [% END %]
    </TR>
  </TABLE>
</BODY>
