#!/usr/bin/perl
#
# indexme - 
# Little Perl file for generating HTML index files from the 
# given filename pattern. Makes it very easy to view lots of
# (html/gif/jpg/etc..) files in a archive using your webbrowser.
#
# Written by Tobias Ekbom 2000-01-27.  (kybernetos@email.com)
# This little program is placed under GPL version 2 or later.
#
# indexme will create one of two possible HTML file indexes;
# * indexme.html (containing links to all the indexed files)
# * indexme.html (containing <img> html links for indexing graphics)
#

# Set basic variables
$cmdarg1=$ARGV[0];
$cmdarg2=$ARGV[1];
$cmdarg3=$ARGV[2];

# Look for '--help' parameter.
if ($cmdarg1 eq "--help") { &typeusageanddie }

# Check that we got at least a file definition from command line.
unless ($cmdarg1) { print "\nError: I need at least a file pattern!\n"; 
		    &typeusageanddie }

# Get command-line parameters and count them.
# (You could use -gr/-rg before I added -f... braindamage, yes.)

$cmdl_parmcnt=0;
if (($cmdarg1 eq "-g") or ($cmdarg2 eq "-g") or ($cmdarg3 eq "-g")) 
				{ $doGpxIndex="TRUE";
				  $cmdl_parmcnt++
				}
if (($cmdarg1 eq "-r") or ($cmdarg2 eq "-r") or ($cmdarg3 eq "-r")) 
				{ $recurIndex="TRUE";
				  $cmdl_parmcnt++ 
				}
if (($cmdarg1 eq "-f") or ($cmdarg2 eq "-f") or ($cmdarg3 eq "-f")) 
				{ $forcewrite="TRUE";
				  $cmdl_parmcnt++ 
				}

    
# Check so that indexme.html doesn't exist already!

unless ($forcewrite) {
    if (-e "indexme.html") 
		    { print "Error: file indexme.html already exists!\n";
		      print "(use -f to overwrite)\n";
		      exit 1 
		    }
		     }

$currdir=`pwd`;
$filepattern=$ARGV[$cmdl_parmcnt];

print "\nindexme - ";

unless ($doGpxIndex) { print "[Basic linking mode/" } 
		else { print "[Graphics linking mode/" }
unless ($recurIndex) { print "Linking single directory]\n\n"; 
			@l_files=`ls -1 --color=never $filepattern`}
		else { print "Linking directory recursively]\n\n";
			@l_files=`find . -name '$filepattern'` }

# Open file indexme.html for writing.
open (index_file, ">indexme.html");

&dohtmlbegin;

print "Indexing Files '$filepattern' in directory $currdir";
$linkcnt=0;

# Write the preffered index type.
unless ($doGpxIndex) { 
# --simple index --
		     foreach $linkfile (@l_files)
		 {
		 chomp $linkfile;
		 print index_file "<a href=\"$linkfile\">$linkfile</a><br>\n";
		 $linkcnt++
		 }
		     
		     }
# --gpx index--		     
		else { 
		     foreach $linkfile (@l_files)
		 {
		 chomp $linkfile;
		 print index_file "<a href=\"$linkfile\">";
		 print index_file "<img src=\"$linkfile\" alt=\"Image link\">";
		 print index_file "</a>\n";
		 $linkcnt++
		 }
		     
		     }

&dohtmlend;

close (index_file);
print "\nLinked $linkcnt files.\n";

# ----------------------- Subroutines ---------------------

sub typeusageanddie {
		    print "\nindexme - Generates a HTML index";
		    print " file in current directory (indexme.html)\n";
		    print "* Usage: indexme [-r] [-g] [-f] 'filename-pattern'\n";
		    print "  -r      = Recursive indexing.\n";
		    print "  -g      = Create a HTML <img> graphics index.\n";
		    print "  -f      = Force overwriting of existing indexme.html\n";
		    print "  --help  = Display this help.\n\n";
		    print "You need to place the filepattern in '' quotes";
		    print " to avoid shell matching.\n";
		    print "Example: indexme -r -g '*.gif'\n\n";
		    exit 1;
		    }



# --------HTML TOP----------------------------------->>
sub dohtmlbegin {

# ------------------------
@htmlhead="<html>
<!-- 
     File auto-generated by 'indexme' HTML-indexing perl program,
     available under GPL >=2 licence (free software).
     Written by Tobias Ekbom (kybernetos\@email.com)
     Get it from http://kyb.homepage.com
-->
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<title>[indexme] Index: '$filepattern'</title>
</head>
<body bgcolor=\"#808080\" text=\"#000000\" link=\"#FFFFFF\" vlink=\"#FFFFBB\">
<center><h2>Result from indexing '$filepattern':</h2></center>

";
# ------------------------

		 foreach (@htmlhead) { print index_file }
		  }
# --<<--



# --------HTML END----------------------------------->>
sub dohtmlend {

# ------------------------
@htmlend="

</body>
</html>";
# ------------------------		    

		 foreach (@htmlend) { print index_file }
		  }
# --<<--
