#!/usr/bin/perl
#
# create an index.html file with links for a listing of all files.
#

{

    @files =();

    $topname=".";

    addfiles($topname,\@files);

    open(NEWINDEX,">","index.html.new")|| die "Failed to open index.html.new!";
    print(NEWINDEX 
	"<!DOCTYPE html PUBLIC \"-//w3c//dtd html 4.0 transitional//en\">\n");
    print(NEWINDEX "<html>\n");
    print(NEWINDEX "<head>\n");
    print(NEWINDEX "<meta http-equiv=\"Content-Type\"\n");
    print(NEWINDEX "content=\"text/html; charset=iso-8859-1\">\n");
    print(NEWINDEX "<title>File Listing</title>\n");
    print(NEWINDEX "</head>\n");
    print(NEWINDEX "<body>\n");
    print(NEWINDEX "<h1>File Listing</h1>");

    print(NEWINDEX "<ul>\n");    
    foreach(@files){
        $formatted=$_;
        $formatted =~ s/^\.\///g;
	print(NEWINDEX "<li><a href=\"$_\">$formatted</a></li>\n");
    }
    print(NEWINDEX "</ul>\n");
    
    print(NEWINDEX "</body>\n");
    print(NEWINDEX "</html>\n");

    close(NEWINDEX);
    print(STDOUT "The new index file was created and named index.html.new\n");
    print(STDOUT "Please move it to the proper location and correct name.\n");

    }

sub addfiles{

    my ($TOPNAME, $files) = @_ ;
    if( opendir(TOPDIR,$TOPNAME) ){
	my @top = readdir(TOPDIR);
	foreach(@top){
	    if(  !(($_ eq "\.") || ($_ eq "\.\.")) ){
		addfiles("$TOPNAME/$_",$files);
	    }
	}
	close TOPDIR;
    } else {
	push @$files, $TOPNAME;
    }
    
}

