#!/usr/bin/perl
#
#Copyright (C) 2003: Christian A. Bråthe
#For: Christian A. Bråthe, Jan Steinar Kvilesjø, Lars Martin Dobbe
#
use CGI qw(:standard :escapeHTML);

print "Content-type: text/html\n\n";

#START

$folderName = param("dirName");

sub create_html_top
{ 
    print "<html>"; 
    print "<head>";
    print "<title>E.L.F.O Recieved Pictures ( $folderName )</title>";
    print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">";
    print "<link href=\"stylesheet.css\" rel=\"stylesheet\" type=\"text/css\">";
    print "</head>"; 
 
    print "<body>";
}

sub create_header_table 
{
    print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
    print "<tr>";  
    print "<td width=\"57%\" class=\"header\">E.L.F.O. Pictures - $folderName</td>";
    print "<td width=\"43%\" class=\"header\" align=\"right\">www.hessdalen.org</td>";
    print "</tr>"; 
    print "<tr>"; 
    print "<td height=\"20\"></td>"; 
    print "</tr>"; 
    print "</table>";
}

sub create_footer_table 
{
    print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
    print "<tr>";  
    print "<td height=\"20\" colspan=\"2\"></td>"; 
    print "</tr>"; 
    print "<tr>";  
    print "<td width=\"73%\" class=\"footer\">Created 2003: Christian A. Bråthe, Jan Steinar Kvilesjø,";
    print "Lars Martin Dobbe</td>";  
    print "<td width=\"27%\" class=\"footer\" align=\"right\"><a ";
    print "href=\"read_dir.cgi\">Back to the directory index</a></td>";
    print "</tr>";
    print "</table>";
}

sub convert_filenames
{
    $where_to_read = "./images/$folderName";
     
    opendir(DIR,$where_to_read);
    @files = readdir(DIR);

    foreach(@files) 
    { 
        next if ($_ eq '.' || $_ eq '..');  # skip this and up dirs

        $oldName = $where_to_read . "/" . "$_";

        if( $_ =~ m/\b#\b/ )
        {
            ($front, $back) = split("#",$_);
            
            $newFilename = $where_to_read . "/" . $front . "NR" . $back;
            
            print "mv $oldName $newFilename";

            system "mv $oldName $newFilename";
        }
    }

}


sub list_folders 
{ 
    $where_to_read = "./images/$folderName";
    opendir(DIR,$where_to_read);
    @files = readdir(DIR); 


    print "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
    print "<tr>"; 
    print "<td width=\"30%\">Alarm occured:</td>";
    print "<td width=\"30%\" align=\"center\">On antenna number:</td>";
    print "<td width=\"40%\" align=\"center\">Captured Picture:</td>"; 
    print "</tr>";
    print "<tr>";
    print "<td colspan=\"3\"><hr size=\"2\" style=\"color:black\"></hr></td>";
    print "</tr>";

                  
    foreach(@files) 
    {    
        next if ($_ eq '.' || $_ eq '..');  # skip this and up dirs
       
        ($houre, $minute, $secound, $rest ) = split("_",$_,4);
        $antenna = split(".",$rest,1);

        

        $link = "showPicture.cgi?picName=" . $where_to_read . "/" . $_ . "&from=" . $folderName;

        print "<tr>"; 
        print "<td width=\"30%\">$houre : $minute : $secound</td>"; 
        print "<td width=\"30%\" align=\"center\">$antenna</td>";
        print "<td width=\"40%\" align=\"center\"><a href=\"$link\">$_</a></td>";
        print "</tr>";   
 
    }
     
    closedir(DIR);




    print "</table>";


}

sub create_html_bottom 
{ 
    print "</body>";
    print "</html>";
}           





create_html_top();
create_header_table();

#convert_filenames();
list_folders();
#HER SKJER DET NOE

create_footer_table();
create_html_bottom();
