#!/usr/bin/perl -w
#
# Read the temperature logfile, write it to a file to an include file
# for the webpage.
#
# Sensor #0 is the Window
# Sensor #1 is the inside of the Linux box
#
# Dec 18 21:46:47  Sensor 0 C: 8.81 F: 47.86
# Dec 18 21:46:47  Sensor 1 C: 26.41 F: 79.54
#
$ftp_src_dir  = "/tmp/";
$ftp_src_file = "nexus_temp.inc";

open( INCFILE, ">$ftp_src_dir$ftp_src_file") || die "Can't open $ftp_src_dir$ftp_src_file";
print INCFILE "<HR>\n";
print INCFILE "<CENTER>\n";

print INCFILE "Nexus Computing's current temperatures are:<P>\n";

# Run tail -2 /var/log/temperature and parse the output
open( TEMPLOG, "tail -2 /var/log/temperature |") || die "Can't fork: $!";

while( <TEMPLOG> )
{

  # Get the time and date, sensor number, temperature in c and f
  ($month,$day,$time,$d1,$sensor,$d1,$centigrade,$d1,$fahrenheight) = split( " ", $_ );

  if( $sensor eq '0' )
  {
    print INCFILE "The modems are $fahrenheight\xB0 F ($centigrade\xB0 C)<BR>\n";
  }

  if( $sensor eq '1' )
  {
    print INCFILE "The room is a ";

    if( $fahrenheight < 40.0 )
    {
      print INCFILE "Freezing";
    }

    if( $fahrenheight >= 40.0 and $fahrenheight < 70.0 )
    {
      print INCFILE "Chilly";
    }

    if( $fahrenheight >= 70.0 and $fahrenheight < 80.0 )
    {
      print INCFILE "Comfortable";
    }

    if( $fahrenheight >= 80.0 and $fahrenheight < 100.0 )
    {
      print INCFILE "Balmy";
    }

    if( $fahrenheight >= 100.0 )
    {
      print INCFILE "Blistering";
    }


    print INCFILE " $fahrenheight\xB0 F ($centigrade\xB0 C)<BR>\n";
  }
}

print INCFILE "Last updated: $month $day $time<BR>\n";

print INCFILE "</CENTER>\n";

close( TEMPLOG );
close( INCFILE );

# done!
