#!/usr/bin/perl -w

$use_archive = 0;		# ignore the archive for now.
&read_archive_files();

$dummy_file = "first";
$file = $dummy_file;
$site = "";
$author = "";

$ellfile = $ARGV[0];
$counts = `grep '^;;;' $ellfile | wc -l`;
($num_entries) = $counts =~ /(\d+)/;

$first_entry = 1; $last_file = "";


### Print out header info.
$date = localtime;
print <<END0;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<head>
<title>Emacs Lisp List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#ffffff">

<h1>Emacs Lisp List</h1>

<p>Last updated: $date</p>

<p>$num_entries entries</p>

<p>Mail me (<a href="mailto:sje30\@cam.ac.uk">sje30\@cam.ac.uk</a>)
with any suggestions for changes to this list.</p>

<p><a href="ell-date.html">View recent changes</a> to the list.</p>
END0

print <<END1;

<p> 2013-06-07.  I am no longer actively updating the list, as I hope
that most people will start using the various packaging systems (such
as <a href="http://www.emacswiki.org/emacs/ELPA">ELPA</a> or <a
href="http://melpa.milkbox.net/">MELPA</a>) instead for modern elisp.
However, if you have an old link on here that you want to update,
email me and I will fix it.</p>


<p> Sat 24 Jun 2006.  Update <a href="ell.el">ell.el</a> to point to
ELL at Cambridge.</p>

<p> Mon 20 Sep 2004.  Nascif Abousalh-Neto has fixed a bug in <a
href="ell.el">ell.el</a> - some entries were not found.  The new
version of ell.el now parses the XML file rather than html.

<p> Fri 22 Nov 2002.  Visit the <a 
href="http://www.emacswiki.org/cgi-bin/wiki.pl?ElispArea">
EmacsWiki</a> if you are looking for somewhere to upload your lisp file.
(Do post it to gnu.emacs.sources too.)

<p> Wed 23 Oct 2002.  Thanks to the efforts of Nascif Abousalh-Neto,
the list can now be viewed by the date when an entry for a lisp file
was updated.  Please see the list <a href="ell-date.html">sorted by
date</a>.

END1

print <<END2;


<p>Please read the <a href="ellcover.html">cover notes</a> for more
details.</p>

<hr>
END2

### Process the entries.
### Entries are terminated by the line "Authors:" in the source file.

&write_index();

$next_letter = 'a';
@letters_found = ();

while (<>) {
    if (/^;;;\s-*(.*)$/) {
        &write_record();
        $file = $1;
    }
    elsif (/^S:\s-*(.*)$/) {
        $site = $1;
    }
    elsif (/^A:\s-*(.*)$/) {
        $author = $1;
    }
    elsif (/^N:\s-*(.*)$/) {
        $note = $1;
    }
    elsif (/^Authors:$/) {
        # start processing the authors.
        last;                   # bail out of loop
    }
}

write_record();                 # write out the last record.

print STDERR "letters found: @letters_found\n";

write_index();

write_footer();                 # write out the code for bottom of page.


sub write_index
{
    ## Put the links into each bit of the alphabet.
    print "\n<p>Quick jump to first entry with this letter:\n";
    for ('a' .. 'z') {
        print "<a href=\"#$_\">$_</a>\n";
    }
    print "</p>\n";
}

sub write_record
{
    return if $file eq $dummy_file;
    $filename = "xx.el";
    #print "file is $file\n";

    if (!defined($author)) {
        printf STDERR "author not defined - line %d\n", $.;
        exit(-1);
    }

    if (!defined($site)) {
        printf STDERR "site not defined - line %d\n", $.;
        exit(-1);
    }

    if (($filename, $desc) = $file =~ /^(\S+)\s+(.+)$/) {

        ## For comparing filename with previous, we need to remove
        ## .el and turn entries into lowercase.
        $sfilename = $filename;
        $sfilename =~ s/\.el$//;        # remove .el if it exists.
        $sfilename = lc $sfilename;     # turn lower.

        if ((!$first_entry) && ($last_file gt $sfilename)) {
            print STDERR "warning: $last_file gt $sfilename\n";
        }
        $last_file = $sfilename; $first_entry = 0;

        $first = substr($filename, 0, 1);
        if ($first ge $next_letter) {
            ##print "found first instance of $next_letter\n";
            ##print "\n<a name=\"$next_letter\"></a>\n";
            print "\n<h2><a name=\"$next_letter\">\u$next_letter</a></h2>\n";
            $next_letter++;
            ##print "now look for $next_letter\n";
            push @letters_found, $first;
        }
        $site =~ s/&/&amp;/g;
        print "\n<p><a href=\"$site\">$filename</a>\n$desc";

    } else {
        print STDERR "error parsing $file, space at start of line?\n";
    }

    # Use a neutral term like contact rather than author, so that it covers
    # multiple authors.

    print "<br>\nContact: $author";
    if (defined $note) {
        print "<br>\nNote: $note";
    }
    print "\n";

    if ($use_archive && exists($archive{$filename})) {
	##print "$filename is in the archive\n";
	##print "<br><a href=\"ftp://ftp.elisparchive.net/pub/elisp/elisp-archive/All/$filename\">Archived</a>\n";
	print "<br><a href=\"http://ftp.codefactory.se/pub/lispmeralda/elisp-archive/All/$filename\">Archived</a>\n";
    }

    ### remove values from perl now...
    undef $site; undef $filename; undef $desc; undef $author; undef $file;
    undef $note;
}

sub write_footer
{

    print <<ENDFOOTER;

</body>
</html>
ENDFOOTER
}

sub read_archive_files
{
    ## Read in the archive status file to see which files are available.
    ## Then we can link to them.
    open(FP, "lispmeralda-status") || die "Cannot open archive file";
    $references=0;
    while(<FP>) {
	if (!$references) {
	    if (/^References$/) {
		$references = 1;
	    }
	} else {
	    ## process this line.
	    if (/elisp-archive.All.(.*)$/) {
		## we have an archive entry.
		##print "found $1\n";
		$archive{$1} = 1;
	    }
	}
    }
}

__END__

=head1 NAME

ellgen - Generate the ELL files

=head1 SYNOPSIS

ellgen [options] [files]

=head1 DESCRIPTION

ellgen ell > ell.html

to create the HTML file.

=head1 OPTIONS

=head1 ENVIRONMENT

=head1 EXAMPLES

=head1 FILES

=head1 AUTHOR

Stephen Eglen stephen@gnu.org

=head1 SEE ALSO

=head1 DIAGNOSTICS

=head1 BUGS

=cut

