#!/usr/bin/perl -w

use strict;
use CGI qw(:standard);

use vars qw( $start_time $time_start );
use vars qw( $data_dir );

require "Common.pl";

GetDataDir();
GetStartTime() or DieEarly( "Error: GetStartTime()" );

print "Content-type: text/html\n\n";
print <<EOM;
<HTML>
<HEAD>
EOM
if ( ! param( 'no_refresh' ) )
{
    print "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"10; URL=ShowLCD.pl?";
    if ( param( 'data_id' ) )
    {
        print "data_id=".param( 'data_id' )."&";
    }
    print "\">\n";
}
print <<EOM;
<style type="text/css">
<!--
.style1 {
	font-size: 16px;
	font-family: "Times New Roman", Times, serif;
	background: "#DDDDDD";
}
.style2 {
	font-size: 16px;
	font-family: "Times New Roman", Times, serif;
}
.style3 {
	font-size: 32px;
	font-family: "Times New Roman", Times, serif;
	font-weight: bold;
}
-->
</style>
</HEAD>
<BODY>
EOM

print start_form, hidden( -name => 'data_id', -default => '' );
print <<EOM;
<TABLE WIDTH=1170 BORDER=3 CELLPADDING="0" STYLE="BORDER-COLLAPSE: COLLAPSE" ALIGN="center">
<TR ALIGN="center" BGCOLOR=#FFFF00><TD CLASS="style3" COLSPAN=2>Long term congestion distribution
EOM
print "&nbsp;&nbsp;&nbsp;&nbsp;<FONT size=3>( <A HREF=\"ShowMT.pl?data_id=".param( 'data_id' )."\" TARGET=\"_blank\"><I>MT</I></A>\n";
print "&nbsp;&nbsp;<A HREF=\"ShowMTD.pl?data_id=".param( 'data_id' )."\" TARGET=\"_blank\"><I>MTD</I></A>\n";
print "&nbsp;&nbsp;<A HREF=\"ShowQoM.pl?data_id=".param( 'data_id' )."\" TARGET=\"_blank\"><I>QoM</I></A> )</FONT>\n";
print <<EOM;
</TD>
</TD><TD CLASS="style2">Start time: 
EOM
print $start_time;
print <<EOM;
</TD></TR>
<TR ALIGN="center">
EOM
system( "cat", "$data_dir/hop_info_head" );
my $no_refresh = ( param( 'no_refresh' ) ? "no_refresh=on" : "" );
print "<TD CLASS=\"style2\" BGCOLOR=#FFFF00>",
      checkbox( -name => 'no_refresh', -label => 'No automatic refresh', -onClick => 'submit()' ),
      "&nbsp;&nbsp;&nbsp;&nbsp;<A HREF=\"ShowLinkCC.pl?data_id=".param( 'data_id' )."&$no_refresh\" TARGET=\"_blank\">Per-link congestion statistics</A>\n",
      "</TD></TR>\n";
my $random_number = int( rand() * 10000000 );
if ( PlotLCD( "10sec" ) )
{
    print "<TR><TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 10 seconds</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/10sec.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "30sec" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 30 seconds (updated every 10 seconds)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/30sec.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "1min" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 1 minute (updated every 10 seconds)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/1min.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD></TR>\n";
}
if ( PlotLCD( "3min" ) )
{
    print "<TR><TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 3 minutes (updated every 1 minute)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/3min.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "10min" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 10 minutes (updated every 1 minute)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/10min.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "30min" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 30 minutes (updated every 10 minutes)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/30min.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD></TR>\n";
}
if ( PlotLCD( "1hour" ) )
{
    print "<TR><TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 1 hour (updated every 10 minutes)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/1hour.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "3hour" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 3 hours (updated every 1 hour)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/3hour.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "6hour" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 6 hours (updated every 1 hour)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/6hour.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD></TR>\n";
}
if ( PlotLCD( "12hour" ) )
{
    print "<TR><TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 12 hours (updated every 3 hours)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/12hour.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD>\n";
}
if ( PlotLCD( "24hour" ) )
{
    print "<TD><TABLE>\n";
    print "<TR><TD CLASS=\"style1\">Recent 24 hours (updated every 3 hours)</TD></TR>\n";
    print "<TR><TD><IMG SRC=\"images/24hour.png?$random_number\"></TD></TR>\n";
    print "</TABLE></TD></TR>\n";
}
print "</TABLE>\n";
print "<HR>\n";
system( "cat", "$data_dir/hop_info", "$data_dir/pairing" );
print end_form;
print <<EOM;
</BODY>
</HTML>
EOM


#
# result = PlotLCD( $filename )
#

sub PlotLCD 
{
    my ( $filename ) = @_;
    my $path = "$data_dir/$filename";
    if ( open( MAX, "<", "$path.max" ) )
    {
        chop( my $line = <MAX> );
        my ( $max_x, $max_y, $plot_time ) = split( /\t/, $line );
        
        close MAX;
        if ( open( PLOT, ">", "images/LCD.gp" ) )
        {
            my $center_y = $max_y / 2;
            $plot_time = sprintf( "%2d:%02d:%02d", $plot_time / 3600, $plot_time % 3600 / 60, $plot_time % 60 );
            print PLOT "set label \"$plot_time\" at graph -0.2,-0.3 tc lt 2\n";
            print PLOT "set xrange [0:$max_x]\n";
            my $yrange = $max_y * 1.1;
            if ( $yrange == 0 ) { $yrange = 1 };
            print PLOT "set yrange [0:$yrange]\n";
            print PLOT "set arrow from 0,$center_y to $max_x,$center_y nohead lt 2\n";
            print PLOT "set xtics ( 0";
            for ( my $i = 2; $i <= $max_x; $i +=2 )
            {
                print PLOT ", $i";
            }
            print PLOT " )\n";
            print PLOT "set x2tics mirror ( 1";
            for ( my $i = 3; $i <= $max_x; $i +=2 )
            {
                print PLOT ", $i";
            }
            print PLOT " )\n";
            print PLOT "set ytics ( $center_y, $max_y )\n";
            print PLOT "set format y \"%7.1f\"\n";
            print PLOT "set output \"images/$filename.png\"\n";
            print PLOT <<EOM;
set xlabel "Hop #"
set ylabel "Congestion count"
set ticscale 0.5
set boxwidth 0.3
set size 0.6, 0.3
set term png
EOM
            print PLOT "plot \"$path\" using 1:2 with boxes fill solid notitle\n";
            close PLOT;
            system( "gnuplot", "images/LCD.gp" );
            return 1;
        }
    }
    return 0;
}

1;