#!/usr/bin/perl

# camtable.cgi - generates table of cameras from formatted list

# variables
$file  = "cameras.txt";	# file of camera URLs
$twidth = 2; 		# desired width of table
$iwidth = 320;		# image width
$iheight= 240;		# image height
#
$tcol   = 1;		# initialize column variable

# print headers
print "Content-type: text/html\n\n";
print "<!-- begin generated table -->\n";
print "<TABLE BORDER='5' BGCOLOR='#404040'>\n";

# open file
open(LIST,"$file");

# main loop
while (<LIST>) {

 # skip comments
 if (!(/^#/)) {

  # read line and parse
  ($name,$url,$image,$status) = split(/,/,$_);
  $name  =~ s/"//g;	# name of camera
  $url   =~ s/"//g;	# URL of camera's homepage
  $image =~ s/"//g;	# URL of actual image
  chop($status);	# status char, either E)nabled or D)isabled

  # if all input is valid, continue
  if (($name ne "") && ($url ne "") && ($image ne "") &&
      ($status eq "E") || ($status eq "D")) {

   # start a row
   if ($tcol == 1) {
    print "<TR>\n";
   }

   # print disabled message JPG if flagged to do so
   if ($status eq "D") {
    $image = "camdisabled.jpg";
   }

   # print table element
   print "<TD ALIGN='CENTER' VALIGN='TOP'>\n";
   print "<A HREF='$url'>";
   print "<IMG SRC='$image' WIDTH='$iwidth' HEIGHT='$iheight' BORDER='0'><BR>";
   print "$name</A>\n";
   print "</TD>\n";

   # end a row
   if ($tcol == $twidth) {
    print "</TR>\n";
    $tcol = 1;
   } else {
    $tcol++;
   }

  }
 
 }

}

close(LIST);

# close the last table row
if ($tcol != 1) {
 print "</TR>\n";
}

# wrap it up
print "</TABLE>\n";
print "<!-- end generated table -->";

