#!/usr/bin/perl -w
#maintained by bo.zhang@lsi.com
#version 1.0
#2012/09/08
#This script is to create a index.html that have every links day.png graph
#
print "we are going to use the master.html and create a new index\n";
print "we will add day.png for every link\n";
my $argv = @ARGV;
print @ARGV,"\n";
print $argv,"\n";
if ( $argv != 1 ){

      print  "Usage    $0 <router>\n";
      print  "example  $0 cisr1.lsi.com\n";
      print  "route maust be cisr1.lsi.com,only typing cisr1 will not working\n";
     exit;
}
#
#
if ( -f "/var/www/html/mrtg/$ARGV[0]/index.html" ){

   unlink("/var/www/html/mrtg/$ARGV[0]/index.html");
   print "removed the file /var/www/html/mrtg/$ARGV[0]/index.html\n";
}

my $diag_dir = "/var/www/html/mrtg/$ARGV[0]/master.html";
#my $out_dir ="/work/mrtg.txt";
#opendir DH , $diag_dir or die "Can not open $diag_dir: $!";
open ON , $diag_dir or die "cannot open $diag_dir:$!";
my $table_value=0;

while (<ON>){
 # print "$_";
 open (FILE,">>/var/www/html/mrtg/$ARGV[0]/index.html");
 syswrite(FILE,$_);
 close FILE;
 if (/HTTP-EQUIV="Expires"/){
    open (HTML,">>/var/www/html/mrtg/$ARGV[0]/index.html");
 print HTML << "TEXT";
    <script language="javascript">

         </script>
TEXT
 close HTML;
 }
 
 if (/^\<TABLE/){
    $table_value = $table_value +1;
    print $_;
    print "\n","my talbe value is",$table_value,"\n";
 }
 
 if ( $table_value==2){
    #print "Hello it works.\n";
   
   if ( $_ =~ /$ARGV[0]_(\d*).html/){
    #print $ARGV[0];
    # $& is the who match pattern (\d*) match digital like:1 2 12 123 12345 ...
    #here like we need to make cisr1.lsi.com
    #$& is cisr1.lsi.com_1.html  $1 here is (\d*) digitals 1,
    print $&; print $1;
    open (HTML,">>/var/www/html/mrtg/$ARGV[0]/index.html");
print HTML <<"TEXT";
 </TD>
<td><DIV><A HREF="/mrtg/$ARGV[0]/$&"><img src="$ARGV[0]_$1-day.png" title="day" alt="day" /></DIV>
TEXT
  close HTML;
    }
 }
 
}
close ON;
print "done :)\n "

#
#system() executes the external command after forking a separate process, and the main program waits for the child process to complete before continuing. exec() however, *never* returns so is used when you誶e finished with the original program. For both of these commands it is more efficient, and secure, to use the list form when passing arguments:
#
#system "/path/to/my/program.pl", $foo, $bar;
#
#instead of:
#
#
#system "/path/to/my/program.pl $foo $bar";