#!/usr/local/bin/perl
# Should Work with Perl4 and Perl5
#
# indexmaker -- a perl script to make index.html from PDF files,
#               HTML files, VRML files and other files.
#
# by Fabrizio Pivari <pivari@geocities.com> 15 Febraury 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.2.
#

$version="4.2";
$opt_configure="indexmaker.cfg";
require "newgetopt.pl";
do NGetOpt("configure=s","output=s","help","verbose") || &printusage ;
@elem=("OUTPUT_file","META_AuthorName","META_AuthorMail","META_Keywords",
       "META_Description","TITLE_title","GIF_pdficon","GIF_getacrobat");
%option=(
      OUTPUT_file      => 'index.html',
      META_AuthorName  => 'Fabrizio Pivari',
      META_AuthorMail  => 'pivari@geocities.com',
      META_Keywords    => 'index, maker, HTML, PDF, VRML',
      META_Description => 'index.html made by IndexMaker $version',
      TITLE_title      => 'TOC file made by IndexMaker $version',
      GIF_pdficon      => 'pdficon.gif',
      GIF_getacrobat   => 'getacro.gif');

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);
if ($opt_output) {$option{'OUTPUT_file'}=$opt_output;}

$HTMLFILES       = "";
$PDFFILES        = "";
$VRMLFILES       = "";
$GRAPHICFILES    = "";
$COMPRESSEDFILES = "";
$OTHERS          = "";
open(TEMP, ">$option{'OUTPUT_file'}")
    || die("indexmaker: couldn't open tempfile $option{'OUTPUT_file'}\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="$option{'GIF_getacrobat'}" 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 .. /\)\n/)
         {
         $AUTHOR.=$_;
         $AUTHOR=&pdfspecialchar($AUTHOR);
         $AUTHOR=~s/\n/ /;
         if ($AUTHOR=~/\/Author[ \t]*\((.*)\)/i)
            {$author=&specialchar($1);}
         }
      if (/\/Subject/i .. /\)/)
         {
         $DESCRIPTION.=$_;
         $DESCRIPTION=&pdfspecialchar($DESCRIPTION);
         $DESCRIPTION=~s/\n/ /;
         if ($DESCRIPTION=~/\/Subject[ \t]*\((.*)\)/i)
            {$description=&specialchar($1);}
         }
      if ((/\/Title/i .. /\)/) && ($i ==0))
         {
         $TITLE.=$_;
         $TITLE=&pdfspecialchar($TITLE);
         $TITLE=~s/\n/ /;
         if ($TITLE=~/\/Title[ \t]*\((.*)\)/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=\"$option{'GIF_pdficon'}\" WIDTH=34 HEIGHT=20>\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 pdfspecialchar {
    local($char) = @_;
    $char =~ s/\\\n/ /;
    $char =~ s/\\\\/\\/g;
    $char =~ s/\\\(/(/g;
    $char =~ s/\\\)/)/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
    -output    file              default index.html

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
    indexmaker -o home.htm *.gif *.tiff *.jpeg

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);
}
