#!/usr/bin/perl -w
# pcd2html module pcd2html_create_html
# Copyright Andreas Tille <tille@debian.org>
# License: GPL
#
# This modul creates HTML files for each image using
# .eng, .deu, .tec and .drf files if available
use pcd2html_uti;
use strict;

my ( $key,
     $source,
     $dir, 
     $title,
     $textfile,
     $num,
     $name,
     $image,
     $next,
     $prev,
     %keys,   # Available keys with directories
     %dirs, 
     %item,   # List of items for translations. 
              # Can be %pcd2html_uti::german or %pcd2html_uti::english
     $O, $Q, $E, $WikiPediaLink
   ) ;
unless ( $ARGV[0] ) { die "Usage: pcd2html_create_html <image_info_file>" ; };

%keys = OpenRules();

$_ = $ARGV[0] ;
/^(\w*)_(\w*)\.\w{3,4}$/ ;
my $pickey = $1 ;
my $picnum = $2 ;

($key, $source) = GetKeyAndSource($pickey, $picnum, $ARGV[0]);

($num, $name, $image, $O, $Q, $E, $WikiPediaLink ) = GetParametersFromRules($pickey, $picnum) ;

if ( ! defined ( $source ) ) {
  if ( -f $image ) {
    $source = $image ;
  } else {
    die "Image $image is missing and Photo CD with key $pickey is not inserted." ;
  }
}

print "   Creating $num$name.html from $source ...\n" ;

%item = GetTranslationItems("eng") ;
CreateFooterString ( $item{"lang"}, "../index".$item{"text"} ) ;

my $html_target = "${num}$item{\"hext\"}";

if ( -f "$html_target" ) {
  if ( $picnum =~ /extra/ ) {
    my ( $target_time, $extra_time ) ;

    $target_time = (stat($html_target))[10];
    $extra_time = (stat("${key}_extra"))[10];
    if ( $target_time > $extra_time ) {
      unless ( open(IMG, ">>$ARGV[0]" ) ) { die "Unable to open $ARGV[0]\n" ; }
      print IMG "rules according to this image not changed\n" ;
      close IMG ;
      system "touch", "$html_target" ;
      exit 0;        
    }
  } else {
    if ( CheckChanges($html_target, $ARGV[0], $O, $E, $Q) == 0 ) {
      system "touch", "$html_target" ;
      exit 0;
    }
  }
}

open(HTML, ">$html_target" ) || die "Unable to open $html_target\n" ;
select (HTML) ;

($title, $textfile) = GetTitle($num, $name, $item{"text"}) ;
($next, $prev) = NextPrev($num) ;

CreateHtml($num, $image, $title, $textfile);
close HTML;

%item = GetTranslationItems("deu") ;
CreateFooterString ( $item{"lang"}, "../index".$item{"text"} ) ;
open(HTML, ">${num}$item{\"hext\"}" ) || die "Unable to open ${num}$item{\"hext\"}\n" ;
select (HTML) ;

%item = GetTranslationItems("deu") ;
($title, $textfile) = GetTitle($num, $name, $item{"text"}) ;

CreateHtml($num, $image, $title, $textfile);
close HTML;

sub CreateHtml {
  my ( $num, $image, $title, $textfile, $internal, $cite, $w, $h );

  $num   = $_[0];
  $image = $_[1];
  $title = $_[2];
  $textfile = $_[3];

  if ( $item{"lang"} eq "en" ) {
    if ( $image =~ /extra/ ) {
      $internal = undef ;
    } else {
      my $internal_info_key = GetInternalInfoKey() ;
      $internal = "\n<!-- $internal_info_key: O=$O, E=$E, Q=$Q -->\n" ;
    }
  }
  MyHtmlStart($title, undef, undef, "../".GetCSS(), $internal ) ;

  if ( $image =~ /extra/ ) {
    open(RULES, "rules" ) || die "Unable to open rules file\n" ;
    while ( <RULES> ) {
      unless ( /[ \t]*#/ ) {
	if ( /extra/ ) {
          chomp ;
          s/(\w*):extra\s*([\w\.]*)\s*.*/$1_extra$2/ ;
          $image = $_ ;
        }
      }
    }
    close RULES ;
  }
  print "<table width=\"100%\">\n <tr>\n" ;
  print "  <th class=\"prev\">\n" ;
  if ( $prev ) {
#    former "<<" / ">>" navigation is replaced by small icons
#    print '   <a href="' . $prev . $item{"hext"}. '" class="prev">' . "&lt;&lt;</a>\n" ;
    ($w, $h) = ImageSize("$prev.jpg");
    print '   <a href="' . $prev . $item{"hext"}. '" class="prev">' . 
          "<img src=\"$prev.jpg\" width=\"$w\" height=\"$h\" alt=\"&lt;&lt;\" /></a>\n" ;
  } else {
    print IndexLink($item{"index"}, "prev", 1)
  }
  print "  </th>\n" ;

  ($w, $h) = ImageSize($image);
  if ( defined ($w) && defined ($h) ) {
    print "  <th rowspan=\"2\">\n   " ;
    if ( $WikiPediaLink ) { print "<a class=\"big\" href=\"http://commons.wikipedia.org/wiki/Image:$WikiPediaLink\">" ; }
    print "<img class=\"big\"\n         src=\"$image\" width=\"$w\" height=\"$h\"" ;
    if ( $title ) {
      print " alt=\"$title\"" ;
    } else {
      print " alt=\"$image\"" ;
    }
    print "/>" ;
    if ( $WikiPediaLink ) { print "</a>" ; }
  } else {
    print "  <th>\n   " ;
    if ( $WikiPediaLink ) { print "<a class=\"big\" href=\"http://commons.wikipedia.org/wiki/Image:$WikiPediaLink\">" ; }
    print "<img class=\"big\"\n         src=\"$image\" alt=\"$title\"/>" ;
    if ( $WikiPediaLink ) { print "</a>" ; }
  }
  print "\n  </th>\n" ;

  print "  <th class=\"next\">\n" ;
  if ( $next ) {
#    former "<<" / ">>" navigation is replaced by small icons
#    print '   <a href="' . $next . $item{"hext"}. '" class="next">' . "&gt;&gt;</a>\n" ;
  
##  Unfortunately the next icon does not yet exist.
##  Calculating this image at this time would require a complete rewrite of pcd2html
#### rewrite in process ...
    ($w, $h) = ImageSize("$next.jpg", 1);
    print '   <a href="' . $next . $item{"hext"}. '" class="next">' . "<img src=\"$next.jpg\"";
    print " width=\"$w\" height=\"$h\" alt=\"&gt;&gt;\" /></a>\n" ;
  } else {
    print IndexLink($item{"index"}, "next", 1)
  }
  print "  </th>\n </tr>\n <tr>" ;
  print "  <th class=\"indexl\">\n" ;
#  former "V<br />V" navigation is replaced by index-icons
#  print "   <a href=\"" . $item{"index"} . "\" class=\"index\">V<br />V</a>\n" ;
  print IndexLink($item{"index"}, "index", 1);
  print "  </th>\n  <th class=\"indexr\">\n" ;
  print IndexLink($item{"index"}, "index", 0);
  print "  </th>\n </tr>\n" ;

  if ( $textfile ne "none" ) {
    print "  <tr>\n    <th></th>\n    <th class=\"description\">" ;
    File2HTML( $textfile, 1, $title ) ;
    print "  </th>\n    <th></th>\n  </tr>\n" ;
  }

  # technical information
  if ( -f "$num.tec" ) {
    print "  <tr>\n    <th></th>\n    <th>\n" ;
    print "    <table class=\"tectype\">\n" ;
    open(TEC, "$num.tec" ) || die "Unable to open rules $num.tex\n" ;
    while ( <TEC> ) {
      s?([^:]*:)(.*)?     <tr><th class=\"tectype\">$1</th><th class=\"tectype\">$2</th></tr>? ;
      print ;
    }
    close TEC ;
    print "    </table>\n" ;
    print "  </th>\n    <th></th>\n  </tr>\n" ;
  }

  if ( $item{"lang"} eq "de" && -f "$num.drf" ) {
    print "  <tr>\n    <th></th>\n    <th class=\"linktype\">\n" ;
    print "   <a href=\"${num}_drf.html\">Bildkritiken aus de.rec.fotografie</a>\n" ;
    print "  </th>\n    <th></th>\n  </tr>\n" ;
  }
  print "</table>\n" ;

  MyHtmlEnd( );

  close HTML ;

  if ( $item{"lang"} eq "de" && -f "$num.drf" ) {
    my ( $print_from, $print_date, $print_any ) ;

    open(DRFOUT, ">${num}_drf.html" ) || die "Unable to open ${num}_drf.html: $!\n" ;
    select DRFOUT;
    
    $_ = "Bildkritiken aus de.rec.fotografie zu $title" ;
    MyHtmlStart($_, undef, undef, "../".GetCSS(), undef ) ;
    open (DRFIN, "$num.drf" ) || die "Unable to open $num.drf\n" ;
    $print_from = $print_date = undef ;
    $print_any  = 0;
    my $pre = 0;
    while ( <DRFIN> ) {
      unless ( /^#/ ) {
        if ( /From:*\s*(.*)$/ ) {
          $print_from = $_ = $1 ;
          /(.*)\s*<(.*)>/ ;
          if ( defined($2) ) {
            $print_from = "<a href=\"mailto:$2\">$1</a>" ;
          } 
	  next ;
	}
        if ( /Date: (.*)/ ) {
          $print_date = $1 ;
	  next ;
	}
        if ( defined( $print_from ) && defined ( $print_date ) ) {
          if ( $print_any ) { print "</p>\n" ; }
	  print "<p class=\"From\">\n$print_from ($print_date):</p>\n" ;
	  print "<p class=\"Kritik\">\n" ;
          $print_from = $print_date = undef ;
          $print_any++ ;
          if ( /^\s*$/ ) { next ; }
        }
        Iso2Html () ;
        if ( /^\s*> / ) {
          unless ( $pre ) {
            print "<cite>\n" ;
            $pre = 1 ;
          }
        } else {
          if ( $pre ) {
            print "</cite>\n" ;
            $pre = 0 ;
          }
        }       
        if ( /^\s*$/ ) { print "<br />" ; } 
	print ; 
        if ( $pre ) { print "<br />" ; }
      }
    }
    close DRFIN ;
    print "</p>\n" ;
    MyHtmlEnd( ) ;
    close DRFOUT ;
  }

}

sub NextPrev {
  my ($rulenum, $next, $prev) ;
  
  $_ = $_[0] ;
  s/_/:/ ;
  $rulenum = $_ ;

  open(RULES, "rules" ) || die "Unable to open rules file\n" ;
  while ( <RULES> ) {
    unless ( /^#/ ) {
      if ( /$rulenum/ ) { 
        while ( <RULES> ) {
          unless ( /^#/ ) {
            if ( /^([^:]*):(\d*) / ) { $next = "$1_$2" ; }
            else {
              if ( /^([^:]*):extra / ) {
                $next = "$1_extra" ;
	      } else {
                $next = undef ; 
	      }
            }
            close RULES ;
            return ($next, $prev) ;
          } 
        }
        return (undef, $prev) ;
      }
      if ( /^([^:]*):(\d*) / ) { $prev = "$1_$2" ; }
      else {
        if ( /^([^:]*):extra / ) {
          $prev = "$1_extra" ;
	} else {
          $prev = undef ; 
	}
      }
    }
  }
  close RULES ;
  return (undef, undef) ;
}
