#!/usr/bin/perl
#
# THE FERRET: This script generates an index.html for the directory it is
#             run in. It requires the following three files (well, it requires
#             two of them but would be REALLY GRATEFUL if you provided the
#             third):
#
#             .header   The HTML header to the index.html file we're making.
#             .footer   The HTML footer to the index.html file we're making.
#             .descs    A list of files and descriptions.
#
#              Now this last one, .descs, is the one it doesn't need, since
#              the script will blithely go ahead and create a new one based on
#              the current state of the directory.
#
#              It does the following things:
#
#              - Reads in the .descs file, if any.
#              - Makes sure every file in the .descs file exists.
#              - Finds out if the items are directories instead of files.
#              - Checks to see if there are any unlisted files.
#              - Generates a new .descs from the gathered info.
#              - Generates a new index.html from the gathered info.

# The COMMA() Subroutine, when given a number, puts the commas in the
# right places. I came up with this in my PERL Class, and I've very fond
# of it.

sub comma {
          $_[0]=reverse $_[0];
          $_[0]=~s/(...)/$1,/g;
          $_[0]=~s/,$//;
          $_[0]=reverse $_[0];
         }

$are = "are";
$filez = "files";

# Read in .descs into an array.

print "[] Opening the .descs file and reading in descriptions...\n";

if (-f ".descs") {

open (DESCS,".descs");

   while($line=<DESCS>)
         {
   chomp $line;
   @slag=split(/ /,$line,2);
   $trashheap{$slag[0]}= [$slag[1]];
         }

close(DESCS);

print "[] Checking for Directories and Missing Files...\n";

  foreach $micro(sort(keys(%trashheap)))
          {
          chomp $micro;
          if (-d $micro)
             {
             }

          elsif (!-f $micro)
                {
                $smite=delete $trashheap{"$micro"};
                print "   Deleting $micro...\n";
                }
           }

# Now, go through a list of all the files in this directory
# and see if there are any files not listed in the .descs file.

print "[] Checking for unlisted (added) files...\n";


# Here's the old version, which blocked out the .tar.gz files.
# We've since switched things around so this isn't a problem.
# open (FILES,"/bin/ls | grep -v tar.gz | ");

open (FILES,"/bin/ls | grep -v .descs | grep -v .musings | grep -v .windex.html | ");

while (<FILES>)
     {
     chomp $_;
     if (not exists $trashheap{$_})
        {
        if ( not /^\./)
        {
        print "   $_ is not listed in .descs. Adding. \n";
        @slag=split(/ /,$_,2);
        $trashheap{$slag[0]}= [$slag[1]];
        }
        }
      }

# Let's quickly throw out the index.html file entry. No need for it to
# be in the listing.

if (-f "index.html" )
   {
$smite=delete $trashheap{"index.html"};
   }

# Now, Generate your new index.html.

print "[] Now generating the new INDEX.HTML page...\n";

open ( INDEX, "> index.html");

if (-f ".header")
  {
  open(FROM,".header");

  while (<FROM>)
        {
        print INDEX "$_";
        }
  }


elsif (not -f ".header")
     {
     print "[] There is no .header file. The index.html might be unreadable.\n";
     }

# Add a line for making the table lynx-friendly.

print INDEX "<tab indent=60 id=T><br>\n";

# Go through it once so that Directories appear on the top.

print "[] First indexing directories, if any....\n";

$holder = "0";

foreach $smitten(sort(keys(%trashheap)))
       {

chomp $smitten;
if (-d $smitten)
             { $holder++;
               print "   $smitten is a Directory.\n";
       print INDEX "<TR VALIGN=TOP><TD VALIGN=TOP><B><A HREF=\"",$smitten,"\">",$smitten,"</A></B><TAB TO=T><TD WIDTH=20></TD><TD><B>",@{$trashheap{$smitten}},"</B></TD></TR>\n";
             }

       }

if ($holder)
       { print INDEX "<TR><TD>&nbsp;</TD></TR>\n";}

print "[] Now indexing files, if any...\n";

# Show the files that are available.

foreach $smitten(sort(keys(%trashheap)))
       {

# If this is a directory, take it out of the race, but add up the directories.

 if (-d $smitten)
       {
       $ndirs++;
       }

 else {

# Get the size of the file.

       @stats=stat("$smitten");

# Is there a thumbnail?

$thumb="";
if ( -f ".png/$smitten.png") {
  $thumb="[<A HREF=\".png/$smitten.png\">?</A>] \n";
  }

       print INDEX "<TR VALIGN=TOP><TD ALIGN=TOP><A HREF=\"",$smitten,"\">",$smitten,"</A> $thumb <tab to=T><TD> ",$stats[7],"<BR><TD> ",@{$trashheap{$smitten}},"\n";

# Add the amount of bytes in this listed file to the total bytes.
# Add one to the total number of files listed.

       $total+=$stats[7];
       $nfiles++;

       }
       }

print INDEX "</TABLE>";

$total=comma($total);
$nfiles=comma($nfiles);

print INDEX "<P><TABLE WIDTH=100%>";

if ($nfiles != "" )
  {

  if ($nfiles eq "1")
     {
     $are="is";
     $filez="file";
     }


print INDEX "<TR><TD ALIGN=RIGHT><SMALL>There ",$are," ",$nfiles," ",$filez," for a total of ",$total," bytes.</SMALL>";
  }


if ($ndirs != "" )
  {

  $are="are";
  $filez="directories";

  if ($ndirs eq "1")
     {
     $are="is";
     $filez="directory";
     }


print INDEX "<TR><TD ALIGN=RIGHT><SMALL>There ",$are," ",$ndirs," ",$filez,".</SMALL>";
  }

print INDEX "</TABLE>";

if (-f ".footer")
  {
  open(FROM,".footer");

  while (<FROM>)
            {
        print INDEX "$_";
        }
  }

#


elsif (not -f ".footer")
  {
print "[] There is no .footer file. The index.html might be unreadable.\n";
  }

# Finally, throw out a new .descs file.

print "[] Creating a new .descs file...\n";

open(DESCS,">.descs");

# Put the files that are available.

foreach $smitten(sort(keys(%trashheap)))
       {
       print DESCS $smitten," ",@{$trashheap{$smitten}},"\n";
       }
close (DESCS);

 }

else { print "There's no .descs file. Please touch one to encourage the ferret.\n";
}