#!/usr/bin/perl -w
# pcd2html modul pcd2html_create_index
# Copyright Andreas Tille <tille@physik.uni-halle.de>
#
# This modul creates a main index to all subdirectories and
# an index of each subdirectory
## use strict ;
use pcd2html_uti ;

system "cp", "${datadir}/pcd.css", "pcd.css" ;

if ( -f "missing" ) { unlink "missing" ; } ;

$title="PCD created on `date +%Y/%m/%d` using <a href=\"http://www.physik.uni-halle.de/~e2od5/debian/pcd2html.html\">pcd2html</a>" ;
($title, $textfile) = GetTitle("index", $title, $english{"text"}) ;

%item = %english ;
if ( CreateHtmlIndex( $title, $textfile ) ) { exit ; }

$title="PCD erstellt am " . `date +%d.%m.%Y` . " mittels <a href=\"http://www.physik.uni-halle.de/~e2od5/debian/pcd2html.html\">pcd2html</a>" ;

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

%item = %german ;
CreateHtmlIndex( $title, $textfile );


##############################################################################################
#  create main index and index in subdirectories
#
sub CreateHtmlIndex  {
  my ( $keywords, $title, $textfile, $back, $home, $nums, $size, $html, $jpg, $nimg, $entry, $i,
       $sub, @sortimg, %subtitle, %images);

  $title    = $_[0];
  $textfile = $_[1];
  
  $_ = "keywords." . $item{"lang"} ;
  $keywords = File2String( $_ ) ;

  $_ = "content." . $item{"lang"} ;
  $content = File2String( $_ ) ;

  # open main index file for writing
  $html = "index" . $item{"hext"} ;
  unless ( open(HTML, ">$html" ) ) { die "Unable to open $html\n" ; }
  select HTML ;
  MyHtmlStart($title, $keywords, $content, $pcd_css, undef ) ;

  print "<h1>$title</h1>\n<p class=\"justify\">\n" ;
  if ( -f $textfile ) {
    File2HTML( $textfile ) ;
  }
  print "</p><table>\n" ;

  # count subdirectories mentioned in main rules get right tabular alignment
  $i = 0;
  $nimg = 0 ;
  open(RULES, "rules" ) || die "Unable to open rules file\n" ;
  while ( <RULES> ) {
    unless ( /^#/ ) {
      if ( /^\w/ ) { $nimg++ ; }
    }
  }
  close RULES ;

  # create table of links to the subdriectories with icon image table
  open(RULES, "rules" ) || die "Unable to open rules file\n" ;
  while ( <RULES> ) {
    unless ( /^#/ ) {
      if ( /^\w/ ) {
        /([^ ]*) *{(.*)} *\[(.*)\]/ ;
        $sub = $1 ;
        if ( $item{"lang"} eq "en" ) {
	    $subtitle{$sub} = $2 ;
        } else {
	    $subtitle{$sub} = $3 ;
        }
        $tmp_icon = undef ;
        open(SUBRULES, "$sub/rules") || die "Unable to open $sub/rules\n" ;
        while ( <SUBRULES> ) {
          unless ( /^#/ ) {
            s/\s*#.*// ;
            if ( /(\w*):(\w*)\s.*\s\*$/ ) {
              $tmp_icon = ExistingPicWithName ( "$sub/${1}_$2" ) ;
              last ;
            }
            unless ( $tmp_icon ) {
              /(\w*):(\w*)\s/ ;
              $tmp_icon = ExistingPicWithName ( "$sub/${1}_$2" ) ;
            }
	  }
        }
        close SUBRULES ;

        $entry = "<a href=\"$sub\/" . $item{"index"} . "\">";
        if ( $tmp_icon ) {
          $_ = `identify -ping $tmp_icon`;
          /$tmp_icon\s*([0-9]*)x([0-9]*)/ ;
          $entry = "<a href=\"$sub\/" . $item{"index"} . "\"><img src=\"$tmp_icon\" width=$1 height=$2 ";
          $entry = "$entry alt=\"$sub\"></a><br>$subtitle{$sub}" ;
        } else {
          $entry = "$entry$subtitle{$sub}</a>" ;
        }

        if ( $i % 3 == 0 ) { print "<tr>\n" ; }
        print "<td width=100 height=100 class=\"icon\" " ;
        if  ( $nimg-- < 3 ) { 
          print "colspan=$nimg>" ;
        } else {
          print ">" ;
        }
        print "$entry</td>\n" ;
        if ( $i % 3 == 2 ) { print "</tr>\n" ; }
        $i++ ;
      }
    }
  }
  close RULES ;

  print "</table><p><center><table cellspacing=7 class=\"linktype\" align=center><tr>\n" ;
  print "<$LinkTdString colspan=2>\n" ;
  print '<a href="' . $item{"other"} . '">' . $item{"otitle"} . "</a>\n</td></tr>\n" ;

  open(TEXTFILE, "$textfile" ) || die "Unable to open $textfile\n" ;
  while ( <TEXTFILE> ) {
    if ( /^# *back: *(.*)/ ) {
      $back = $1 ;
    }
    if ( /^# *home: *(.*)/ ) {
      $home = $1 ;
    }
  }
  close TEXTFILE ;

  if ( defined ( $back ) ) {
    if ( $next ) { print "<$LinkTdString>\n" ; }
    else         { print "<$LinkTdString colspan=2>\n" ; }
    print "<a href=\"$back\">" . $item{"top"} . "</a>\n</td>\n" ;
  }
  if ( defined ( $next ) ) {
    if ( $back ) { print "<$LinkTdString>\n" ; }
    else         { print "<$LinkTdString colspan=2>\n" ; }
    print "<a href=\"$next\">" . $item{"top"} . "</a>\n</td>\n" ;
  }

  print "</table>\n" ;
  MyHtmlEnd ( $item{"lang"} ) ;
  close HTML ;
  select STDOUT ;

  foreach $subdir ( keys (%subtitle) ) { 
    chdir $subdir ;
    print "Working in $subdir ...\n" ;
    unless ( open(HTML, ">$html" ) ) { die "Unable to open $html\n" ; }
    select HTML ;
    MyHtmlStart( $subtitle{$subdir}, undef, undef, "../$pcd_css", undef );
    print "<h1>$subtitle{$subdir}</h1>\n" ;

    open(RULES, "rules" ) || die "Unable to open rules file\n" ;
    %images  = () ;
    @sortimg = () ;
    $nimg    = 0;
    while ( <RULES> ) {
      unless ( /^#/ ) {
        if ( /(\w*):(\w*)\s\s*(\w*)/ ) {
	  $images{"$1_$2"} = $3 ;
          @sortimg         = (@sortimg, "$1_$2") ;
          $nimg++;
        } 
      }
    }

    print "<table>\n" ;
    $i = 0;
    foreach $num ( @sortimg ) {
      ($title, $textfile) = GetTitle($num, $images{$num}, $item{"text"});

      unless ( defined($title) ) {
	$title = $images{$num} ;
      }

      $jpg = ExistingPicWithName ( $num . $images{$num} ) ;

      if ( defined ($jpg) ) {
        my ( $tmp_icon ) ;
        $size = int ((( stat( $jpg ) )[7]  + 500) / 1000) ;
        $tmp_icon = ExistingPicWithName ( $num ) ;
        $_ = `identify -ping $tmp_icon`;
        /$tmp_icon\s*([0-9]*)x([0-9]*)/ ;
        $entry = '<a href="' . $num . $item{"hext"} . "\"><img src=\"$tmp_icon\" width=$1 height=$2 ";
        $entry = "$entry alt=\"$title\"></a><br>$title (${size}k)" ;
      }
      else {
        if ( $item{"lang"} eq "en" ) {
          $_ = $num ;
	  s/_.*// ;
          print STDOUT "To complete insert CD $_ and rerun pcd2html to build $images{$num}.\n" ;
          unless ( open(MISSING, ">>../missing" ) ) { die "Unable to open $html\n" ; }
	  print MISSING "$subdir/$num$images{$num}\n" ;
          close MISSING ;
          $entry = "Image $images{$num} is missing" ;
        } else {
          $entry = "Bild $images{$num} fehlt" ;
        }
      }
      if ( $i % 3 == 0 ) { print "<tr>\n" ; }
      print "<td width=100 height=100 class=\"icon\" " ;
      if  ( $nimg-- < 3 ) { 
        print "colspan=$nimg>" ;
      } else {
        print ">" ;
      }
      print "$entry</td>\n" ;
      if ( $i % 3 == 2 ) { print "</tr>\n" ; }
      $i++ ;
    }
    print "</table>\n<p>\n" ;
    print "<table cellspacing=7 class=\"linktype\" align=center><tr>\n" ;
    print "<$LinkTdString colspan=2>\n" ;
    print "<a href=\"$sortimg[0]" . $item{"hext"} . '">' . $item{"slide"} . "</a>\n" ;
    print "<$LinkTdString colspan=2>\n" ;
    print '<a href="../index' . $item{"hext"} . '">' . $item{"back_m"} . "</a>\n" ;
    print "</td></tr></table>\n" ;
    MyHtmlEnd ( $item{"lang"} ) ;
    close HTML ;
    select STDOUT ;
    chdir ".." ;
  }
  return 0 ;
}





