#!/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> 12 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 3.3.
#

require "newgetopt.pl";
do NGetOpt("title=s","help","verbose") || &printusage ;
if ($opt_help) {&printusage};
$title="TOC file";
if ($opt_title ne "") {$title=$opt_title;}
$verbose=0;
if ($opt_verbose) {$verbose=1}

$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 3.3 written by Fabrizio Pivari (pivari\@geocities.com)
on 12/12/97 -->
<HTML>
<HEAD>
<LINK REL="Author" HREF=mailto:pivari\@geocities.com>
<META NAME="Author" CONTENT="Fabrizio Pivari">
<META NAME="Keywords" CONTENT="index, maker, HTML, PDF, VRML">
<META NAME="Description" CONTENT="index.html made by IndexMaker 3.3">
<TITLE>$title</TITLE>
</HEAD>
<BODY BGCOLOR="\#ffffff">
<H1>$title</H1>
EOF

   if (@ARGV)
      {
      foreach $x (@ARGV)
         {
         $file = $x;
         $dir = &dirname($x);
         $i=0;
         $author=""; $title=""; $description=""; $VRML=""; $TITLE="";
         $META="";
         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 *\((.*)\)/) {$author=&specialchar($1);}
      if((/^\/Title *\((.*)\)/) && ($i ==0)) {$title=&specialchar($1); $i=1;}
      }
   if ($title eq "") {$title=$file;}
   $PDFFILES .= "<LI><A HREF=\"./$file\">$title<\/A> ";
   if ($author ne "") {$PDFFILES .= "written by $author ";}
   $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
    -title  Title 

files:
    with files you can use metacharacters and relative and absolute path name
    
example:
    indexmaker *.pdf
    indexmaker -t "TOC example" *.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);
}
