#! /usr/bin/perl

# This script makes the generated files used by the describe(1) web
# page.  It's not pretty, but it works.
#
# Devin Reade, January 1998.
#
# $Id: mkgen,v 1.2 1998/01/17 21:27:25 gdr Exp $
#

$gnohome_internal = "/home/gno/public_html";
$gnohome_external = "http://www.gno.org/gno";

$gendir   = "describe/gen";     # relative to $gnohome_*
$entrydir = "describe/entry";	# relative to $gnohome_*
$basedir  = $gnohome_internal . '/' . $gendir;

$headinfo =
    '<!-- This page is auto-generated -->' . "\n" .
    '<META name="description" content="List of Utilities for GNO/ME">' . "\n".
    '<META name="keywords" content="Apple IIgs ORCA/Shell James Brookes">'
    . "\n".
    '<BASE HREF="http://www.gno.org/">'. "\n";
$bodyinfo =
    '<BODY background="icons/bluebg.jpg" text="#000000" bgcolor="#00C0FF"'.
    ' link="#0000FF" alink="#001177" vlink="#001177">' . "\n";
$tailinfo =
    '<hr>'  . "\n" .
    'Comments on this page should be forwarded to Devin Reade at' . "\n" .
    '<A HREF="mailto:gdr@eddore.myrias.com">gdr@eddore.myrias.com</A>' . "\n" .
    '<p>' . "\n" .
    '<A HREF="http://www.visi.com/~nathan/a2/a2viewable.html">' . "\n" .
    '<img alt="Apple II Viewable Page" height=31 width=88  border=0' . "\n" .
    'src="icons/a2viewable.gif"></A>' . "\n" .
    '<br>' . "\n" .
    '</body>' . "\n" .
    '</html>';

$categoryMap{""}			= "";
$categoryMap{"gno/archive"}		= "archive";
$categoryMap{"gno/comm"}		= "comm";
$categoryMap{"gno/daemons"}		= "daemon";
$categoryMap{"gno/doc.utils"}		= "docutil";
$categoryMap{"gno/editors"}		= "edit";
$categoryMap{"gno/file.convert"}	= "file.convert";
$categoryMap{"gno/file.manip"}		= "file.manip";
$categoryMap{"gno/games"}		= "games";
$categoryMap{"gno/graphics"}		= "graphics";
$categoryMap{"gno/gui"}			= "gui";
$categoryMap{"gno/languages"}		= "lang";
$categoryMap{"gno/libraries"}		= "lib";
$categoryMap{"gno/mail"}		= "mail";
$categoryMap{"gno/network"}		= "network";
$categoryMap{"gno/news"}		= "news";
$categoryMap{"gno/patches"}		= "patch";
$categoryMap{"gno/programming"}		= "prog";
$categoryMap{"gno/scripting"}		= "script";
$categoryMap{"gno/shells"}		= "shells";
$categoryMap{"gno/sounds"}		= "sound";
$categoryMap{"gno/sys"}			= "sys";
$categoryMap{"gno/sysadmin"}		= "sysadmin";
$categoryMap{"gno/text.utils"}		= "text";
$categoryMap{"orca/scripting"}		= "script";
$categoryMap{"unsorted"}		= "unsorted";

$categoryNames{"archive"}	= "Archiving Utilities";
$categoryNames{"comm"}		= "Communications Utilities";
$categoryNames{"daemon"}	= "Daemons";
$categoryNames{"docutil"}	= "Documentation Utilities";
$categoryNames{"edit"}		= "Editors, Word Processors, and Document " .
				  "Viewers";
$categoryNames{"file.convert"}	= "File Conversion Utilities";
$categoryNames{"file.manip"}	= "File Manipulation Utilities";
$categoryNames{"games"}		= "Games";
$categoryNames{"graphics"}	= "Graphics Programs";
$categoryNames{"gui"}		= "GUI";
$categoryNames{"lang"}		= "Programming Languages";
$categoryNames{"lib"}		= "Libraries";
$categoryNames{"mail"}		= "Mail Readers";
$categoryNames{"network"}	= "Networking Utilities";
$categoryNames{"news"}		= "News Readers";
$categoryNames{"patch"}		= "Patch Programs";
$categoryNames{"prog"}		= "Programming Utilities";
$categoryNames{"script"}	= "Scripting Utilities";
$categoryNames{"shells"}	= "Shells";
$categoryNames{"sound"}		= "Sound and Music Utilities";
$categoryNames{"sys"}		= "System Utilities";
$categoryNames{"sysadmin"}	= "System Administration Utilities";
$categoryNames{"text"}		= "Text Manipulation Utilities";
$categoryNames{"unsorted"}	= "Utilities Not Yet Sorted";

while (<>) {
	($name, $classes) = split;
	$namelist{$name} = $classes;
}

@keylist = keys(%namelist);
@sorted = sort(@keylist);
$currentLetter = '';

foreach $f (@sorted) {

    # create the describe entry
    open(pp, "/usr/local/bin/describe $f|")
	|| die ("couldn't open pipe to 'describe $f'");
    $filename = "$gnohome_internal/$entrydir/$f.html";
    open(fp2, "> $filename") || die("couldn't open $filename");
    printf(fp2
	   "<html>\n" .
	   "<head>\n" .
	   "<title>Apple IIgs Describe Database -- %s</title>\n" .
	   "</head>%s" .
	   "<body bgcolor=#ffffff>\n" .
	   "<h1>Describe Entry for %s</h1>\n" .
	   "<pre>\n",
	   $f, $headinfo, $f);
    while($_ = <pp>) {
	printf(fp2 "%s", $_);
    }
    printf(fp2 "</pre>\n%s\n", $tailinfo);
    close(fp2);
    close(pp);

    # make the index-by-name page
    $l = substr($f, 0, 1);
    if ($l ne $currentLetter) {
	($currentLetter eq '') || 
	    (printf(fp "</ul>%s\n", $tailinfo) && close(fp));
	$filename = $basedir . '/' . $l . '.html';
	open(fp, "> $filename") || die("couldn't open $filename");
	$currentLetter = $l;
	$cap = $l;
	$cap =~ tr/a-z/A-Z/;
	printf(fp
	       "<html>\n" .
	       "<head>\n" .
	       "<title>Apple IIgs Describe Database -- Index by %s</title>\n".
	       "%s\n" .
	       "</head>\n" .
	       "%s\n" .
	       "<h1>Describe Entries by Name: %s</h1>\n" .
	       "<ul>\n",
	       $cap, $headinfo, $bodyinfo, $cap);
    }
    printf(fp "<li><A HREF=\"%s/%s/%s.html\">%s</A>\n", 
	   $gnohome_external, $entrydir, $f, $f);

    # save the category for later
    @catlist = split(/:/, $namelist{$f});
    foreach $catentry (@catlist) {
	if (!defined($categoryMap{$catentry})) {
	    printf(stderr "WARNING: Unregistered category \"%s\" for %s: " .
		   "skipped\n",
		   $catentry, $f);
	} elsif ($categoryMap{$catentry} ne '') {
	    $categories{$categoryMap{$catentry}} .= $f . ':';
	}
    }
}

($currentLetter eq '') || 
    (printf(fp "</ul>\n%s\n", $tailinfo) && close(fp));

$letters = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
@letter = split(/\s+/, $letters);

foreach $l (@letter) {
    $cap = $l;
    $cap =~ tr/a-z/A-Z/;
    $filename = "$basedir/$l.html";
    if (! -f $filename) {
	open(fp, "> $filename") || die("couldn't open $filename");
	printf(fp
	       "<html>\n" .
	       "<!-- This page is auto-generated -->\n" .
	       "<head>\n" .
	       "<title>Apple IIgs Describe Database -- Index by " . $cap .
	       "</title>\n" .
	       "</head>\n" .
	       "<body bgcolor=#ffffff>\n" .
	       "<H1>Empty Page</H1>\n" .
	       "There are currently no utilities registered that begin ".
	       "with " . $cap . ".<p>%s\n",
	       $tailinfo);
	close(fp);
    }
}

#
# Now do them by category
#

@catkeys = keys(%categories);
foreach $k (@catkeys) {
    $file = $basedir . '/' . $k . '.html';
    open(fp, "> $basedir/$k.html") || die("could not open $basedir/$k.html");
    printf(fp
	   "<html>\n" .
	   "<head>\n" .
	   "<title>Apple IIgs Describe Database -- By Category</title>\n" .
	   "%s\n" .
	   "</head>\n" .
	   "%s\n",
	   $headinfo, $bodyinfo);
    if (defined($categoryNames{$k})) {
	printf(fp
	       "<h1>%s</h1><hr>\n", 
	       $categoryNames{$k});
    } else {
	printf(stderr "WARNING:  No category name for \"%s\"\n", $k);
    }
    printf(fp "<ul>\n");
    @files = sort(split(/:/, $categories{$k}));
    foreach $f (@files) {
	printf(fp "<li><A HREF=\"%s/%s/%s.html\">%s</A>\n",
	       $gnohome_external, $entrydir, $f, $f);
    }
    printf(fp "</ul>\n%s\n", $tailinfo);
    close(fp);

}

# make category pages for which there is no entry

@catkeys = values(%categoryMap);
foreach $k (@catkeys) {
    $filename = $basedir . "/" . $k . '.html';
    if (! -f $filename) {
	open(fp, ">$filename") || die("couldn't open $filename: $!");
    printf(fp
	   "<html>\n" .
	   "<!-- This page is auto-generated -->\n" .
	   "<head>\n" .
	   "<title>Apple IIgs Describe Database -- By Category</title>\n" .
	   "%s\n" .
	   "</head>\n" .
	   "%s\n",
	   $headinfo, $bodyinfo);
	if (defined($categoryNames{$k})) {
	    printf(fp
		   "<h1>%s</h1><hr>\n", 
		   $categoryNames{$k});
	} else {
	    printf(stderr "WARNING:  No category name for \"%s\"\n", $k);
	}
	printf(fp 
	       "There are currently no <b>describe</b>(1) entries in this ".
	       "category.\n" .
	       "%s\n", $tailinfo);
	close(fp);
    }
}
