#!/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> 15 November 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.0.
#

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

$HTMLFILES       = "";
$PDFFILES        = "";
$GRAPHICFILES    = "";
$COMPRESSEDFILES = ""; 
$OTHERS          = "";
$out = "index.html";
open(TEMP, ">$out")
    || die("indexmaker: couldn't open tempfile $out \n");
print TEMP "<!-- IndexMaker written by Fabrizio Pivari ";
print TEMP "(pivari\@geocities.com) on 11/15/97 -->\n\n<HTML>\n<HEAD>\n";
print TEMP "<TITLE>$title<\/TITLE>\n";
print TEMP "<\/HEAD>\n<BODY BGCOLOR=\"\#ffffff\">\n";
print TEMP "<H1>$title<\/H1>\n";

   if (@ARGV)
      {
      foreach $x (@ARGV)
         {
         $file = $x;
         $dir = &dirname($x);
         $i=0;
         $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 =~ /.gif/i) || ($file =~ /jpg/i))
               {$GRAPHICFILES .= "<LI><A HREF=\"./$file\">$file<\/A></LI>\n";}
            elsif (($file =~ /.gz/i) || ($file =~ /.zip/i))
               {$COMPRESSEDFILES .= "<LI><A HREF=\"./$file\">$file<\/A></LI>\n";}
            else {$OTHERS .= qq!<LI><A HREF="./$file">$file<\/A></LI>\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 ($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";
   }

sub htmlfile
# Extracts from HTML files TITLE
   {
   while (<STDIN>)
      {
      if(/<TITLE>/i .. /<\/TITLE>/i)
         {
         $i=1;
         s/<TITLE>/<LI><A HREF=".\/$file">/i;
         s/<\/TITLE>/<\/A>\n<\/LI>\n/i;
         $HTMLFILES .= $_;
         }
      }
   if ($i eq 0) {$HTMLFILES .=  "<LI><A HREF=\"./$file\">$file<\/A></LI>\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
    -title  Title 

files:
    with files you can use metacharacters and relative and absolute path name
    
example:
    indexmaker *.pdf
    indexmaker -t "TOC example" *.pdf

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