#! /usr/bin/perl
#-----------------------------------------------------------------------------------------------------------
#
#File: pictures.pl
#Author: Vegard Olsen
#Last Modified: 120699
#
#-----------------------------------------------------------------------------------------------------------
#Input Files Format
#filename:Ârstall:tekst:copyright
#
#-----------------------------------------------------------------------------------------------------------

$i_top_file = "/www/almashriq/htdocs/admin/bilder/scripts/tmp/itop.dat";
$i_bottom_file = "/www/almashriq/htdocs/admin/bilder/scripts/tmp/ibottom.dat";
$top_file = "/www/almashriq/htdocs/admin/bilder/scripts/tmp/top.dat";
$bottom_file = "/www/almashriq/htdocs/admin/bilder/scripts/tmp/bottom.dat";
@pictures;@years;@texts;@copyright;@big;@icons;
@html_files; 

if(!@ARGV)  {
  &files_in_dir;
}

else {
  $option = @ARGV[0];
  if ($option =~ m/-l/i) {
    shift(@ARGV);   
    &files_in_list;
  }
  #elsif ($option =~ m/-t/i) {print "Thumbnails mode\n";}
  else  {
      &files_in_arg;
  }
}

#-----------------------------------------------------------------------------------------------------------
sub files_in_arg  {

  foreach $file_org (@ARGV)  {
  
    push(@pictures,$file_org);
    
  }
  &make_slides;
    
    
}

#-----------------------------------------------------------------------------------------------------------
sub files_in_dir  {
 
  print "No args\n"; 
  tmp_pictures;

  opendir( DIR, 'images/' ) || die "opendir failed on $dir: $!";
  local( @files ) = readdir( DIR );
  closedir( DIR );
  foreach $file (@files)  {
    
    if($file =~ /(.*\.jpeg|.*\.jpg|.*\.gif)/ )  { 
      push (@tmp_pictures,$file);
    }
  }
  @pictures = sort @tmp_pictures;
  print "@pictures\n";
  &make_slides;
}

#-----------------------------------------------------------------------------------------------------------
sub files_in_list  {

  my($line);my(@fields); 
  my($list_file) = @ARGV[0];
  open(LIST,"$list_file") || die "Cannot open: $list_file";
  
  while($line = <LIST>)  {
    @fields = split /:/, $line;
    push (@pictures,$fields[0]);
    push (@texts,$fields[1]);
    push (@copyright, $fields[2]);
  } 
  &make_slides; 
  close (LIST);
}


#-----------------------------------------------------------------------------------------------------------
sub print_top  {
  my($out) = @_[0];
  my($file)= @_[1];
  open(TOP, "$file") || die "Cannot open: $file"; 
  
  #printer innholdet i top til den nye filen
   while($line = <TOP>)  {
     print $out "$line";
   }
   close (TOP);
 
}
#-----------------------------------------------------------------------------------------------------------
sub print_bottom  {
  
  my($out) = @_[0];
  my($file) = @_[1];
  open(BOTTOM,"$file") || die "Cannot open: $file";
  
  while($line = <BOTTOM>)  {
    print $out "$line";
  } 
  close (BOTTOM);

}

#-----------------------------------------------------------------------------------------------------------
sub make_slide  {
 
    my($i) = @_[0];
    my($index_number) = @_[1];
    if( $index_number == 1)  {
      $index_number = "index.html";
    }
    else  {
	$index_number = "index" . "$index_number" . ".html";
    }
    print "--- Index called : $i ---\n";
    print "--- Index number: $index_number ---\n";
    open(NEW,">html/$html_files[$i]") || die "Cannot open: $html_files[$i]";
    &print_top(NEW,$top_file);
    print "$big[$i]\n";
    #print diverse inn her


    print NEW "<img src =\"/sys/almashriq-levant.gif\" border = 0 alt=\"Al mashriq - The Levant\">\n";
   print NEW "</td>\n";
   print NEW "<td align=right>\n";

   if( $i == 0) {  #Hvis vi er pÂ f¯rste index
      print NEW "<font size=-2>\[<a href=\"$html_files[$#pictures]\">Prev</a>\] \[<a href=\"javascript:window:window.close();\">Close</a>\] \[<a href=\"$html_files[$i+1]\">Next</a>\]</font>\n";
    }
    elsif ( $i == $#pictures)  {  #Hvis vi er pÂ siste index
      print NEW "<font size=-2>\[<a href=\"$html_files[$i-1]\">Prev</a>\] \[<a href=\"javascript:window:window.close();\">Close</a>\] \[<a href=\"$html_files[0]\">Next</a>\]</font>\n";
    }
    else  {
      print NEW "<font size=-2>\[<a href=\"$html_files[$i-1]\">Prev</a>\] \[<a href=\"javascript:window:window.close();\">Close</a>\] \[<a href=\"$html_files[$i+1]\">Next</a>\]</font>\n";
    }

     #Jalla-metode for Â finne ut om bildet ligger eller stÂr
    system "djpeg images/$pictures[$i] | pnmfile > data_tmp";
    open(TMP,"data_tmp") || die "Cannot open: data_tmp";
    my($picture_data) = <TMP>;
    $picture_data =~ /([0-9]*) by ([0-9]*)/;
    my($width) = $1;
    my($height) = $2;
    if($width > $height) {print "Liggende\n"; make_horis($i); }
    else { print "Staaende\n"; make_verti($i);}
    close TMP;
     
    &print_bottom(NEW,$bottom_file); 
    close(NEW);
        

}
  
  
  
#-----------------------------------------------------------------------------------------------------------
sub make_horis  {
   
    my($index) = $_[0];  
    print NEW "</td>\n";
    print NEW "</tr>\n";
    print NEW "<tr valign=top>\n";
    print NEW "<td colspan=2>\n";
    print NEW "<table width=640>\n";
    print NEW "<tr>\n";
    print NEW "<td>\n";
    print NEW "<table border=0 align=center>\n";
    print NEW "<tr>\n";
    print NEW "<td>\n";
    print NEW "<!---REMEMBER TO SET TITLE AND DATE --->\n";
    #print NEW "<a href=\"#B97\" name=\"B97\" onClick=\"javascript:popUp('../images/big/$big[$index]', 512, 768, 'big$index'); return false\"><img src=\"../images/$pictures[$index]\" border=0></a>"; 

    #print NEW "$texts[$index]";
    print NEW "<img src=\"../images/$pictures[$index]\">"; 
    #print NEW " <p> <small>&copy;Zakaria Khalil</small><br>\n";  
    #print NEW "  <small>&copy;B&oslash;rre Ludvigsen 1997-1999</small>\n";  
    #print NEW "  <small>&copy;Antonia Reime</small>\n";  
    print NEW "  <br><small>&copy;$copyright[$index]</small>\n";  
    print NEW "  <p>$texts[$index]\n";
    print NEW "</td>\n";
    print NEW "</tr>\n";
    print NEW "</table>\n"; 
    &make_thumbnail($index, "width");
}

#-----------------------------------------------------------------------------------------------------------
sub make_verti  {
   
   my($index) = $_[0];  
   print NEW "</td>\n";
   print NEW "</tr>\n";
   print NEW "<tr valign=top>\n";
   print NEW "<td colspan=2>\n";
   print NEW "<table width=640>\n";
   print NEW "<tr>\n";    
   print NEW "<td>\n";
   print NEW "<table border=0 align=center>\n";
   print NEW "  <tr> \n";   
   print NEW "  <td>\n";
   #print NEW "<a href=\"#B97\" name=\"B97\" onClick=\"javascript:popUp('../images/big/$big[$index]', 768, 512, 'big'); return false\"><img src=\"../images/$pictures[$index]\" border=0></a>";
   #print NEW "  <a href="<img src=\"../images/$pictures[$index]\"><p>\n";
   
   #print NEW "<img src=\"../$pictures[$index]\">";
   print NEW "<img src=\"../images/$pictures[$index]\">";
   #print NEW "  <small>&copy;Antonia Reime</small><br>\n";  
   #print NEW "  <small>&copy;B&oslash;rre Ludvigsen 1997-1999</small><br>\n";  
   #print NEW "  <p><small>&copy;Zakaria Khalil</small><br>\n";  
   print NEW "  <br><small>&copy;$copyright[$index]</small>\n";  
   print NEW "  <p>$texts[$index]\n";
   print NEW "  </td> \n";   
   print NEW "  </tr> \n"; 
   print NEW "  </table> \n";     



   &make_thumbnail($index, "height");
}

#-----------------------------------------------------------------------------------------------------------
sub make_thumbnail  {
  my($direction) = pop(@_);
  my($index) = pop(@_);
  print "Making thumbnail: djpeg $pictures[$index] | pnmscale -$direction 128 | cjpeg -optimize > $pictures[$index]\n";
  $pictures[$index] =~ /(.*\.)/;  
  my($file_tmp) = $1;chop $file_tmp;
  $file_tmp = "$file_tmp" . "_ico" . ".jpg";
  push(@icons,$file_tmp);
  print "$file_tmp\n";
  system "djpeg images/$pictures[$index] | pnmscale -$direction 128 | cjpeg -optimize > thumb/$file_tmp";


}




#-----------------------------------------------------------------------------------------------------------
sub make_slides  {

    #lage en array med html filer og store bilder
    for ($i = 0; $i <= $#pictures; $i++)  {
      $pictures[$i] =~ /(.*\.)/;
      my($tmp) = $1;
      my($file_html) = "$tmp" . "html";
      chop $tmp;
      my($file_big) =  "$tmp" . "_big" .".jpg";
      push(@html_files, $file_html);
      push(@big, $file_big);
    }


    #Sjekker antall index-sider
    my($check);
    my($index_number);
    my($number_of_files) = $#pictures + 1;
    print "Antall filer  $number_of_files\n";
    my($index_name);
    if( $number_of_files % 16 == 0)
    {
	$index_number = $number_of_files / 16;
        print "16 nummer-Antall indexer: $index_number\n";
    }
    else
    {
      $check = $number_of_files / 16;
      $check =~ /([0-9]+)\.([0-9]*)/;
      $index_number = $1 + 1; 
      print "Ulikt nummer-Antall indexer: $index_number\n";
    }
    my($current_index) = 0;
    for ($i = 1; $i <= $index_number; $i++)  
    {
	if($i == 1){$index_name = "index.html";}
        else{$index_name = "index" . "$i" . ".html";}
        print "$index_name\n";
        
        open(IND,">$index_name") || die "Cannot open: $index_name";
        &print_top(IND,"/www/almashriq/htdocs/admin/bilder/scripts/tmp/scripttop2.dat");
        my($last_index) = "index" . "$index_number" . ".html";
        my($next_index) = $i + 1;
        my($prev_index) = $i - 1;
        
        
        if($index_number > 1)
        {
          if( $i == 1) {  #Hvis vi er pÂ f¯rste index
	    print IND "<font size=-2><a href=\"$last_index\">[Prev]</a><a href=\"index$next_index.html\">[Next]</a></font>\n";
        }
        elsif ( $i == 2)  {  #Hvis vi er pÂ andre index
          print IND "<font size=-2><a href=\"index.html\">[Prev]</a><a href=\"index$next_index.html\">[Next]</a></font>\n";
        } 
        elsif ( $i == $index_number)  {  #Hvis vi er pÂ siste index
          print IND "<font size=-2><a href=\"index$prev_index.html\">[Prev]</a><a href=\"index.html\">[Next]</a></font>\n";
        }
        else  {
          print IND "<font size=-2><a href=\"index$prev_index.html\">[Prev]</a><a href=\"index$next_index.html\">[Next]</a></font>\n";
        }
        }  
        print IND "</td>\n";
        print IND "</tr>\n";
        print IND "<tr valign = top>\n";
        print IND "<td colspan=4>\n";
        print IND "<p>Photographs from Syria 1999 - © B&oslash;rre Ludvigsen<br>\n";
        print IND "<font size=+1><b>Miscellaneous photographs</b></font></p>\n";
       # print IND "<p>Underwater pictures from Amchit. ";
        print IND "<tr valign = top align=center>\n";
        
        for($j = $current_index; $j < $i * 16; $j++)
        {
          if($j >= $number_of_files)
          { print "over";}
  
          else  {
            print "$j\n";
            if($j % 4 == 0)
            {
              print IND "</tr>\n\n";
              print IND "<tr valign = top align=center>\n"
            }
            make_slide($j,$i);
            print IND "<td valign=top><a href=\"#B97\" name=\"B97\" onClick=\"javascript:popUp('html/$html_files[$j]', 700, 700, 'new'); return false\"><img src=\"thumb/$icons[$j]\" border=0>";
            print IND "<br><small>$texts[$j]</small></a></td>\n";
            
    
          }
          


        }
          
        print IND "</tr>\n\n";
        &print_bottom(IND,$i_bottom_file);
        close(IND);
        $current_index += 16;

    }


}


#-----------------------------------------------------------------------------------------------------------
sub make_simple_index  {

  open(IND, ">simple_index.html") || die "Cannot open: simple_index.html";
  &print_top(IND,$i_top_file);

  for ($i = 0; $i <= $#pictures; $i++)
  {
    $pictures[$i] =~ /(.*\.)/;
    #my($tmp_thumb) = "$1" . "gif";
    my($tmp_name) = $1;
    chop $tmp_name;
    #print IND "<p><a href=\"html/$html_files[$i]\"></a>$tmp_name, $years[$i], $texts[$i], $copyright[$i]</p>\n";
    print IND "<p><a href=\"html/$html_files[$i]\">$tmp_name</a></p>\n";

  }
  &print_bottom(IND,$i_bottom_file);
  close(IND);
 
}





