#!/usr/bin/perl -w
# pcd2html module pcd2html_create_makefile
# Copyright Andreas Tille <tille@physik.uni-halle.de>
#
# This modul creates HTML files for each image using
# .eng, .deu, .tec and .drf files if available
use pcd2html_uti;

unless ( $ARGV[0] ) { die "Usage: pcd2html_create_html <image_info_file>" ; };

$JPEG_QUALITY = 70 ;
$CONVERT = "convert -verbose" ;
$internal_info_key = "pcd2html internal info" ;

if ( OpenRules () ) { die "Error while opening rules file.\n" ; }
$key = pcd_get_key() || die "Can't obtain Kodak Photo-CD key\n"; 

$_ = $ARGV[0] ;
if ( /extra/ ) {
  /^(\w*)_extra.*\.\w{3,4}$/ ;
  $pickey = $1 ;
  $picnum = "extra" ;
} else {
  /^(\w*)_([0-9]{3})\.\w{3,4}$/ ;
  $pickey = $1 ;
  $picnum = $2 ;
}
if ( $pickey ne $key ) {
  unless ( open(IMG, ">$ARGV[0]" ) ) { die "Unable to open $ARGV[0]\n" ; }
  print IMG "Wrong PCD inserted\n" ;
  close (IMG) ;
  exit 0 ;
}

$icon = "${key}_$picnum.jpg" ;

unless ( $picnum =~ /extra/ ) {
  $source = "$cdrom/photo_cd/images/img0" . $picnum . ".pcd";
}

while ( <RULES> ) {
  if ( /^$pickey:$picnum/ ) {
    chomp ;
    $line = $_ ;
    if ( /extra/ ) {
      /(\w*):extra *([^ ]*)/ ;
      $image = $1 . "_extra_" . $2 ;
    } else {
      /(\w*):(\d{3}) *([^ ]*).*/ ;
      $image = "$1_$2$3.jpg" ;
      if ( defined ($O = GetOperationParameter($line, '\[.*\]', '[^ ]* *\[(.*)\]' )) ) {
        $options = $O;
      } else {
        $options = "";
        $O = "" ;
      }
      if ( defined ($E = GetOperationParameter($line, "!", "[^\\!]*!([1-5])")) ) {
        $source = "$source\[$E\]";
      } else {
        $E = "" ;
      }
      if ( defined ($Q = GetOperationParameter($line, "Q", "[^\\Q]*Q([0-9]*)" )) ) {
        $quality = "-quality $Q";
      } else {
        $quality = "-quality $JPEG_QUALITY";
        $Q = "" ;
      }
      last ;
    }
  }
}
close RULES ;

$_ = $image ;
s/\.\w{3}$// ;
$basename = $_ ;

print "   Working on $basename ...\n" ;
if ( $ARGV[0] =~ /extra/ ) {
  $_ = $basename ;
  s/([^\.]*)\.+/$1/ ;  # What did I meant here???
  $name = $_ ;
  $_ = $ARGV[0] ;
  s/\.\w{3,4}// ;
  $num = $_ ;
} else {
  $_ = $basename ;
  s/(\w*_\d\d\d).*/$1/ ;
  $num=$_ ;
  $_ = $basename ;
  s/$num// ;
  $name = $_ ;
}

$html_target = "${num}$english{\"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 ( open(HTML, "$html_target" ) ) {
      while ( <HTML> ) {
        if ( /<!-- $internal_info_key: O=([^,]*), E=(\d*), Q=(\d*) -->/ ) {
          $HO = $1;
          $HE = $2;
          $HQ = $3;
          last ;
        }
      }
      close HTML ;
      if ( $O eq $HO && $E eq $HE && $Q eq $HQ ) {
        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;        
      }
    }
  }
}

if ( $picnum =~ /extra/ ) {
  my ( $tmp_icon ) ;
  system "${key}_extra" ;
  $tmp_icon = ExistingPicWithName ( "${key}_extra" ) ;
  unless ( $tmp_icon ) {
    system "$CONVERT -colors 64 -dither -geometry 80x64 $image $icon 2>> $ARGV[0]" ;
  }
} else {
  system "$CONVERT $quality $options $source $image 2> $ARGV[0]" ;
  system "$CONVERT -colors 64 -dither -geometry 80x64 $image $icon 2>> $ARGV[0]" ;
}

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

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

%item = %english ;
CreateHtml($num, $image, $title, $textfile);
close HTML;

open(HTML, ">${num}$german{\"hext\"}" ) || die "Unable to open ${num}$english{\"hext\"}\n" ;
select (HTML) ;

($title, $textfile) = GetTitle($num, $name, $german{"text"}) ;

%item = %german ;
CreateHtml($num, $image, $title, $textfile);
close HTML;

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

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

  if ( $item{"lang"} eq "en" ) {
    if ( $image =~ /extra/ ) {
      $internal = undef ;
    } else {
      $internal = "\n<!-- $internal_info_key: O=$options, E=$E, Q=$Q -->\n" ;
    }
  }
  MyHtmlStart($title, undef, undef, "../$pcd_css", $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><tr>\n" ;
  if ( $prev ) {
    print "<th valign=top class=\"nextprev\">" ;
    print '<a href="' . $prev . $item{"hext"}. '">' . "&lt;&lt;</a>\n</td>\n" ;
  }

  $_ = `identify -ping $image`;
  /$image\s*([0-9]*)x([0-9]*)/ ;
  if ( defined ( $1 ) && defined ( $2 ) ) {
    $w = $1 + 10;
    $h = $2 + 10;
    print "<th class=\"image\" width=$w height=$h>\n" ;
    print "<img src=\"$image\" width=$1 height=$2 alt=\"$title\"\>" ;
  } else {
    print "<th class=\"image\">\n" ;
    print "<img src=\"$image\" alt=\"$title\"\>" ;
  }
  print "</th>\n" ;

  if ( $next ) {
    print "<th valign=top class=\"nextprev\">" ;
    print '<a href="' . $next . $item{"hext"}. '">' . "&gt;&gt;</a>\n</th>\n" ;
  }
  print "</tr></table>\n" ;

  print "<p class=\"description\">\n" ;
  if ( $textfile ne "none" ) {
    File2HTML( $textfile, 0 ) ;
  }

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

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

  # Navigation links
  print "<p>\n" ;
  print "<table cellspacing=7 class=\"linktype\"><tr>\n" ;

  if ( $prev ) {
    # make single entries the same size as index
    if ( $next ) { print "<$LinkTdString>\n" ; }
    else         { print "<$LinkTdString colspan=2>\n" ; }
    print '<a href="' . $prev . $item{"hext"}. '">' . $item{"prev"} . "</a>\n</td>\n" ;
  }
  if ( $next ) {
    if ( $prev ) { print "<$LinkTdString>\n" ; }
    else         { print "<$LinkTdString colspan=2>\n" ; }
    print '<a href="' . $next . $item{"hext"}. '">' . $item{"next"} . "</a>\n</td>\n" ;
  }
  print "<tr><$LinkTdString colspan=2>\n" ;
  print '<a href="' . $item{"index"} . '"> ' . $item{"back"} . "</a>\n" ;
  print "</td></tr></table>\n" ;

  MyHtmlEnd( $item{"lang"} );

  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, "../$pcd_css", undef ) ;
    open (DRFIN, "$num.drf" ) || die "Unable to open $num.drf\n" ;
    $print_from = $print_date = undef ;
    $print_any  = 0;
    $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( "de" ) ;
    close DRFOUT ;
  }

}

sub GetOperationParameter {
  $_ = $_[0] ;
  if ( /$_[1]/ ) {
    /$key:([0-9][0-9][0-9]) *$_[2].*/ ;
    return $2 ;
  }
  return undef ;
}

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

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


