#! /usr/bin/env perl
# -*- perl -*-

### Time-stamp: <2009-06-16 20:00:58 MEST>
### M.C. Widerkrantz, mc at hack.org

### Create a static HTML file containing a directory listing with
### descriptions of each file, somewhat like the bulletin boards of
### old.

### The description file must be named 'files.lst' (but see
### $descriptions below) and should be located in the same
### directory. The entries in the file should begin with a filename
### and a double colon "::" followed by the description.

### The description can span many lines and paragraphs, separated by
### double newlines.
###
### Here's an example:
###
### foo:: 
### Description of foo. The first line.
### Second line.
###
### Another paragraph in the description of foo.
###
### A third paragraph.
### bar:: 
### Another file description.

use strict;
use warnings;
use POSIX;

# Set this to the URL for your stylesheet, if you have one, as in
#my $stylesheet = "/mc/default.css";
my $stylesheet = "";

# Name of the file containing the descriptions of each file in the
# directory.
my $descriptions = "files.lst";

# Name of the file to generate.
my $index = "index.html";

# Default HTML Title, settable with -t title.
my $title = "File Listing";

if ($#ARGV == 1 && $ARGV[0] eq '-t') 
{
    $title = $ARGV[1];
}

open(my $filesfile, $descriptions) 
    or die "Can't open $descriptions: $!";

open(my $indexfile, ">$index")
    or die "Can't open $index: $!";

# This is inserted at the top of each generated index.

print $indexfile '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="generator" content="flist by MC, http://hack.org/mc/">
';

if ($stylesheet)
{
    print $indexfile "<link rel=\"stylesheet\" type=\"text/css\" href=\"$stylesheet\">\n";
}

print $indexfile "<title>${title}</title>
</head>
<body>
<h1>${title}</h1>
";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) 
    = gmtime();

# Make the date readable
$year += 1900;
$mon += 1;

printf $indexfile "<p> List generated %d-%02d-%02d %02d:%02d:%02d (UTC)\n<dl>\n", $year, $mon, 
    $mday, $hour, $min, $sec;

my $filename;
while (<$filesfile>)
{
    if ($_ eq "\n")
    {
	print $indexfile "<p>";
    }

    if ($_ =~ /^.*::/)
    {
	# This is the start of a new file description.

	$filename = $_;
	chop($filename); # get rid of newline
	chop($filename); # get rid of ending ":"
	chop($filename); # get rid of ending ":"

	# Get data about this file.
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	    $atime,$mtime,$ctime,$blksize,$blocks)
	    = stat($filename);

	if ($size)
	{
	    my $prefix;
	    my $prefixstr;

	    # Get the time.
	    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) 
		= gmtime($mtime);

	    # Make the date readable
	    $year += 1900;
	    $mon += 1;

	    # Make size human readable

	    my $giga = 1073741824;
	    my $mega = 1048576;
	    my $kilo = 1024;

	    if ($size > $giga) {
		$prefix = $giga;
		$prefixstr = "Gi";
	    }
	    elsif ($size > $mega) {
		$prefix = $mega;
		$prefixstr = "Mi";
	    }
	    else {
		$prefix = $kilo;
		$prefixstr = "Ki";
	    }

	    printf $indexfile "<dt><a href=\"%s\">%s</a> %.2f %sB %d-%02d-%02d %02d:%02d:%02d (UTC)</dt><dd>\n",
	    $filename, $filename, $size / $prefix, $prefixstr, $year, $mon, 
	    $mday, $hour, $min, $sec;
	}
	else
	{
	    print $indexfile "<dt>${filename}: missing file.</dt><dd>\n";
	}
    }
    else
    {
	# We're in the middle of a description. Just add the line
	# we're reading.

	print $indexfile $_;
    }
}

print $indexfile "</dl>\n</body>\n</html>\n";

close $indexfile;

