#!/usr/local/bin/perl
# Works with Perl4 and Perl5
#
# indexmaker -- a perl script to make index.html from PDF files
#
# by Fabrizio Pivari <pivari@geocities.com> 19 Dicember 1997
#
# Copy, use, and redistribute freely, but don't take my name off it and
# clearly mark an altered version.  Fixes and enhancements cheerfully 
# accepted.
#
# This is version 4.0.
#

$version="4.0";
$opt_configure="./indexmaker.cfg";
require "newgetopt.pl";
do NGetOpt("configure=s","help","verbose") || &printusage ;
@elem=("META_AuthorName","META_AuthorMail","META_Keywords","META_Description",
       "TITLE_title");
%option=(
      META_AuthorName => 'Fabrizio Pivari',
      META_AuthorMail => 'Fabrizio.Pivari@agip.it',
      META_Keywords => 'index, maker, HTML, PDF, VRML',
      META_Description => 'index.html made br IndexMaker 4.0',
      TITLE_title => 'TOC file made by IndexMaker 4.0');

if ($opt_help) {&printusage};
$verbose=0;
if ($opt_verbose) {$verbose=1}
open (CNF, "$opt_configure") || die "Error: $opt_configure - $!\n";
while (<CNF>)
   {
   s/\t/ /g;  #replace tabs by space
   next if /^[ \t]*\#/; #ignore comment lines
   next if /^[ \t]*$/;  #ignore empty lines
   foreach $elem (@elem) 
      {if (/[ \t]*$elem[ \t]*:[ \t]*(.*)/i) {$option{$elem}=$1;}}
   }
close(CNF);

$HTMLFILES       = "";
$PDFFILES        = "";
$VRMLFILES       = "";
$GRAPHICFILES    = "";
$COMPRESSEDFILES = "";
$OTHERS          = "";
$out = "index.html";
open(TEMP, ">$out")
    || die("indexmaker: couldn't open tempfile $out \n");
print TEMP <<EOF;
<!doctype HTML public "-//W3O//DTD W3 HTML 3.2//EN">
<!-- Made by IndexMaker $version written by Fabrizio Pivari (pivari\@geocities.com)
on 11/01/97 -->
<HTML>
<HEAD>
<LINK REL="Author" HREF=mailto:$option{'META_AuthorMail'}>
<META NAME="Author" CONTENT="$option{'META_AuthorName'}">
<META NAME="Keywords" CONTENT="$option{'META_Keywords'}">
<META NAME="Description" CONTENT="$option{'META_Description'}">
<TITLE>$option{'TITLE_title'}</TITLE>
</HEAD>
<BODY BGCOLOR="\#ffffff">
<H1>$option{'TITLE_title'}</H1>
EOF

   if (@ARGV)
      {
      foreach $x (@ARGV)
         {
         $file = $x;
         $dir = &dirname($x);
         $i=0;
         $author=""; $title=""; $description=""; $VRML=""; $TITLE="";
         $DESCRIPTION=""; $PDF=""; $META=""; $AUTHOR="";
         if (!open(STDIN, "$file"))
            {print STDERR "indexmaker: couldn't open $x for input\n";exit(1);}
         else
            {
            if ($file =~ /.pdf/i) {&pdffile;}
            elsif (($file =~ /.html/i) || ($file =~ /.htm/i)) {&htmlfile;}
            elsif (($file =~ /.vrml/i) || ($file =~ /.vrl/i)) {&vrmlfile;}
            elsif (($file =~ /.gif/i) || ($file =~ /jpg/i))
               {
               $GRAPHICFILES .= "<LI><A HREF=\"$file\">$file<\/A></LI>\n";
               if($verbose eq 1) {print "Analizing graphic    file: $file\n";}
               }
            elsif (($file =~ /.gz/i) || ($file =~ /.zip/i))
               {
               $COMPRESSEDFILES .= "<LI><A HREF=\"$file\">$file<\/A></LI>\n";
               if($verbose eq 1) {print "Analizing compressed file: $file\n";}
               }
            else
               {
               $OTHERS .= qq!<LI><A HREF="$file">$file<\/A></LI>\n!;
               if($verbose eq 1) {print "Analizing others         : $file\n";}
               }
            }
         }
      }

if ($HTMLFILES ne "")
   {
   print TEMP "<H2>HTML files</H2>\n<UL>\n$HTMLFILES\n<\/UL>\n";
   }

if ($PDFFILES ne "")
   {
   print TEMP "<H2>PDF files ";
   print TEMP qq!<A HREF="http://www.adobe.com/prodindex/acrobat/readstep.html">
!;
   print TEMP qq!<IMG BORDER=0 SRC="./getacro.gif" WIDTH=88 HEIGHT=31></A>\n!;
   print TEMP "</H2>\n<UL>\n$PDFFILES\n<\/UL>\n";
   }

if ($VRMLFILES ne "")
   {
   print TEMP "<H2>VRML files</H2>\n<UL>\n$VRMLFILES\n<\/UL>\n";
   }

if ($GRAPHICFILES ne "")
   {
   print TEMP "<H2>Graphic files</H2>\n<UL>\n$GRAPHICFILES\n<\/UL>\n";
   }

if ($COMPRESSEDFILES ne "")
   {
   print TEMP "<H2>Compressed files</H2>\n<UL>\n$COMPRESSEDFILES\n<\/UL>\n";
   }

if ($OTHERS ne "")
   {
   print TEMP "<H2>Others</H2>\n<UL>\n$OTHERS\n<\/UL>\n";
   }




print TEMP "<\/BODY>\n<\/HTML>\n";
close(TEMP);

sub dirname
# return filename with the basename part stripped away
   {
   local($file) = @_;
   if ($file =~ /\//) {$file =~ s:/[^/]*$::;}
   else {$file = ".";}
   return($file);
   }

sub pdffile
# Extracts from PDF files TITLE
   {
   while (<STDIN>)
      {
      if (/\/Author/i .. /\)/)
         {
         $AUTHOR.=$_;
         $AUTHOR=~s/\n/ /;
         if ($AUTHOR=~/\/Author.*\(([^\(]*)\)/i)
            {$author=&specialchar($1);}
         }
      if (/\/Subject/i .. /\)/)
         {
         $DESCRIPTION.=$_;
         $DESCRIPTION=~s/\n/ /;
         if ($DESCRIPTION=~/\/Subject.*\(([^\(]*)\)/i)
            {$description=&specialchar($1);}
         }
      if ((/\/Title/i .. /\)/) && ($i ==0))
         {
         $TITLE.=$_;
         $TITLE=~s/\n/ /;
         if ($TITLE=~/\/Title.*\(([^\(]*)\)/i)
            {
            $title=&specialchar($1);
            $i=1;
            }
         }
      }
   if ($title eq "") {$title=$file;}
   $PDFFILES .= "<LI><A HREF=\"$file\">$title<\/A> ";
   if ($author ne "") {$PDFFILES .= "written by $author ";}
   if($description ne "") {$PDFFILES .= "<BR>\nDescription: $description\n";}
   $PDFFILES .= "<IMG SRC=\"./pdficon.gif\" WIDTH=28 HEIGHT=31>\n<\/LI>\n";
   if($verbose eq 1) {print "Analizing PDF        file: $file\n";}
   }

sub htmlfile
# Extracts from HTML files TITLE
   {
   while (<STDIN>)
      {
      if(/<META/i .. />/)
         {
         $META.=$_;
         $META=~s/\n/ /;
         if ($META=~/NAME=\"Author\" *CONTENT=\"([^"]*)\"/i) {$author=$1;}
         if ($META=~/NAME=\"Description\" * CONTENT=\"([^"]*)\"/i)
            {$description=$1;}
         }
      if(/<TITLE>/i .. /<\/TITLE>/i)
         {
         $TITLE.=$_;
         $TITLE=~s/\n/ /;
         $TITLE=~/<TITLE>(.*)<\/TITLE>/i;
         $title=$1;
         }
      }
   if ($title eq "") {$title=$file;}
   $HTMLFILES .=  "<LI><A HREF=\"$file\">$title<\/A> ";
   if($author ne "") {$HTMLFILES .= "written by $author";}
   if($description ne "") {$HTMLFILES .= "<BR>\nDescription: $description\n";}
   $HTMLFILES .= "</LI>\n";
   if($verbose eq 1) {print "Analizing HTML       file: $file\n";}
   }

sub vrmlfile
# Extracts from VRML files Info
   {
   while (<STDIN>)
      {
# VRML 2.0
      if(/WorldInfo/i .. /}/)
         {
         $VRML.=$_;
         $VRML=~s/\n/ /;
         if ($VRML=~/info.*\[([^\[]*)\]/i) {$description = &specialchar($1);}
         if ($VRML=~/title.*\"([^"]*)\"/i) {$title = &specialchar($1);}
         }

# VRML 1.0
      else
         {
         $VRML.=$_;
         $VRML=~s/\n/ /;
         if ($VRML=~/Info.*\"([^"]*)\"/i) {$description = &specialchar($1);}
         }
      }
   if ($title eq "") {$title = $file;}
   $VRMLFILES .= "<LI><A HREF=\"$file\">$title<\/A> $description\n";
   $VRMLFILES .= "</LI>\n";
   if($verbose eq 1) {print "Analizing VRML       file: $file\n";}
   }

sub specialchar {
    local($char) = @_;
    $char =~ s/&/&amp;/g;
    $char =~ s/</&lt;/g;
    $char =~ s/>/&gt;/g;
    return($char);
    }

sub printusage {
    print <<USAGEDESC;

usage:
        indexmaker [-options ...] files

where options include:
    -help                        print out this message
    -verbose                     verbose
    -configure file              Default indexmaker.cfg

files:
    with files you can use metacharacters and relative and absolute path name
    
example:
    indexmaker *.pdf
    indexmaker -c tests/test.cfg *.pdf
    indexmaker -v */*.html

If you want to know more about this tool, you might want
to read the docs. They came together with indexmaker!

Home: http://www.geocities.com/CapeCanaveral/Lab/3469/indexmaker.html

USAGEDESC
    exit(1);
}
