#!/usr/local/bin/perl -w

# foliate v. 21 Dec 1995, Dave Schweisguth <dcs@proton.chem.yale.edu>
# Indexes digest2html output files and links them to each other and the index

# Inspired by work by Tom Fine

# Version history
# ---------------
# 21 Dec 1995	First release.

### Preliminaries

# Emulate #!/usr/local/bin/perl on systems without #!

eval '(exit $?0)' && eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/local/bin/perl -S $0 $argv:q' if 0;

require 5;			# Perl 5 required, 5.001m recommended

### Constants

# Environment

($whatami = $0)	=~ s|.*/||;		# `basename $0`
chop($date	= `date '+%d %b %Y'`);	# dd Mon yy

# Configuration

$force		= 0;		# Overwrite existing files
$horiz		= 1;		# Make horizontal links between digests
$vert		= 1;		# Make vertical links to top index
$top		= 1;		# Make top index

$index		= 'index.html';	# Name of entry point in each directory
$tlb		= "<!-- TOP LINK BEGIN -->\n";	    # Link insertion points
$tle		= "<!-- TOP LINK END -->\n";
$blb		= "<!-- BOTTOM LINK BEGIN -->\n";
$ble		= "<!-- BOTTOM LINK END -->\n";
$author		= 'The SGI FAQ group';
$email		= 'sgi-faq@viz.tamu.edu';
$whereami	= 'http://www-viz.tamu.edu/~sgi-faq/tools/';
$toptitle	= 'SGI Frequently Asked Questions (FAQs)';

### Arguments and error-checking

# Parse args

$arg = '';			# Perl 5 lossage alert
while ($#ARGV > -1 && (($first, $rest) = ($ARGV[0] =~ /^-(.)(.*)/))) {
    # Perl 5 lossage alert
    if ($first =~ /[\0]/) {	# Switches with arguments (none at the moment)
    	shift;
    	$arg = $rest ne '' ? $rest : $ARGV[0] ne '' ? shift :
      	    &usage("$whatami: -$first requires an argument.\n");
    } elsif ($rest ne '') {
    	$ARGV[0] = "-$rest";
    } else {
	shift;
    }
    if	  ($first eq 'f') { $force = 1; }
    elsif ($first eq 'h') { $horiz = 0; }
    elsif ($first eq 'v') { $vert = 0; }
    elsif ($first eq 's') { $short = 1; }
    elsif ($first eq 't') { $top = 0; }
    elsif ($first eq 'u') { &usage(0); }
    else		  { &usage("$whatami: -$first is not an option.\n"); }
}

sub usage {
    local ($message) = $_[0];

    warn $message if $message;
    warn <<EOP;
Usage: $whatami [-fhvtu] file [...]
-f	Overwrite existing files
-h	Omit horizontal links between digests
-v	Omit vertical links from digests to top-level index
-s	Shorten URLs by omitting "$index". Appropriate when output will be
	  read only through an HTTP server which knows to append "$index".
-t	Omit top-level index (implies -v)
-u	This message
EOP
    exit !! $message;
}

# Post-process arguments and options

@ARGV || die "$whatami: Specify at least one input file.\n";
@infiles = @ARGV;

$vert = $vert && $top;

$urlindex = $short ? '' : $index;

### Do it

# Reconnaissance

foreach $n (0 .. $#infiles) {

    # Munge input filenames and precalculate "path correction" (yecch)

    $infiles[$n] =~ s|/*$||;		# Strip trailing slashes

    if ($top && $infiles[$n] eq $index) {
	die "$whatami: Index file will overwrite input file $index!\n";
    }

    if (-d $infiles[$n]) {
	$infiles[$n] .= '/';
	$urlfiles[$n] = $infiles[$n];
	$infiles[$n] .= $index;
	$urlfiles[$n] .= $index unless $short;
	$pc[$n] = '../';
    } else {
	$urlfiles[$n] = $infiles[$n];
	$pc[$n] = './';
    }

    # Read titles from input files

    open(IN, $infiles[$n]) || die "$whatami: Can't read $infiles[$n]!\n";
    grep(/<title>([^<]*)<\/title>/ && ($titles[$n] = $1), <IN>);
    close IN;

}

# Make top-level index
# Do this first, so if $index exists we'll die before doing anything else

if ($top) {
    
    if (-e $index && ! $force) {
	die "$whatami: Output file $index already exists!\n";
    }
    open(OUT, ">$index") || die "$whatami: Can't write to $index!\n";
    select OUT;
    
    print <<EOP;
<html>
<head>
<title>$toptitle</title>
</head>
<body>
<h1>$toptitle</h1>
<ul>
EOP

    foreach $n (0 .. $#infiles) {
	print "<li><a href=\"$urlfiles[$n]\">$titles[$n]</a>\n";
    }
    
    print <<EOP;
</ul>
<hr>
<address>
$author &lt;<a href="mailto:$email">$email</a>&gt;<br>
Generated by <a href="$whereami">$whatami</a>, $date
</address>
</body>
</html>
EOP

    close OUT;

}

# Process input files

foreach $n (0 .. $#infiles) {

    open(IN, $infiles[$n]) || die "$whatami: Can't read $infiles[$n]!\n";
    @buf = <IN>;
    close IN;
    
    open(OUT, ">$infiles[$n]") || die "$whatami: Can't write to $infiles[$n]!\n";
    select OUT;
    
    # Print file over itself, substituting links as we go

    $i = 0;
    until ($buf[$i] eq $tlb) {	    # Print lines up to TOP LINK BEGIN
	print $buf[$i];
	$i++;
    }
    print $buf[$i];

    # Top Up/Next/Previous links

    print "<a href=\"$pc[$n]$urlindex\">Up: $toptitle</a>\n" if $vert;
    if ($horiz && $n < $#infiles) {
	print "<br>\n" if $vert;
	print "<a href=\"$pc[$n]$urlfiles[$n+1]\">Next: $titles[$n+1]</a>\n";
    }
    if ($horiz && $n > 0) {
	print "<br>\n" if $vert || $n < $#infiles;
	print "<a href=\"$pc[$n]$urlfiles[$n-1]\">Previous: $titles[$n-1]</a>\n"
    }
    print "<hr>\n" if $vert || ($horiz && $n > 0 && $n < $#infiles);
    
    until ($buf[$i] eq $tle) {	    # Skip lines between BEGIN and END
	$i++;
    }
    until ($buf[$i] eq $blb) {	    # Print lines up to BOTTOM LINK BEGIN
	print $buf[$i];
	$i++;
    }
    print $buf[$i];
    
    # Bottom Up/Next/Previous links
    
    print "<hr>\n" if $vert || ($horiz && $n > 0 && $n < $#infiles);
    print "<a href=\"$pc[$n]$urlindex\">Up: $toptitle</a>\n" if $vert;
    if ($horiz && $n < $#infiles) {
	print "<br>\n" if $vert;
	print "<a href=\"$pc[$n]$urlfiles[$n+1]\">Next: $titles[$n+1]</a>\n";
    }
    if ($horiz && $n > 0) {
	print "<br>\n" if $vert || $n < $#infiles;
	print "<a href=\"$pc[$n]$urlfiles[$n-1]\">Previous: $titles[$n-1]</a>\n"
    }
    
    until ($buf[$i] eq $ble) {	    # Skip lines between BEGIN and END
	$i++;
    }
    while ($i <= $#buf) {	    # Print the remainder
	print $buf[$i];
	$i++;
    }

    close OUT;
}

exit;
