#! /bin/perl -s
#
# hl2html	--- convert hotlists to HTML
#
# Author: Oscar Nierstrasz 1/7/93
#
#    -u suggested by Larry W. Virden (lvirden@cas.org)
#    NB: -u option prints the *first* entry for a given URL,
#    which may mean that an obsolete title is selected.
#
#    -l suggested by Andrew Tannebaum (trb@clam.com)
#    Warning: all users given as arguments are listed,
#    not just the ones for whom hot lists were found!
#    To fix this, would need to buffer the output before printing ...
#
#vn = "v1.0"; # 1/7/93
#vn = "v1.1"; # 1/8/93 -- Added -d and -u flags.
#vn = "v1.1"; # 4/8/93 -- Added -t flag (Larry Vierden).
#vn = "v1.2"; # 28/10/93 -- Added sort by title (-s)
$vn = "v1.3"; # 15/01/94 -- Added -l option (list users)

$usg = 'Usage: hl2html [<options>] <user directory> ...
    -v -- verbose
    -u -- unique URLs only
    -t -- unique Title/URLs only
    -d -- list date of each entry
    -s -- sort by title
    -l -- list users in title
' ;

chop($date = `date +%d.%m.%y`);
$omn = '<A HREF="http://cui_www.unige.ch/OSG/omn.html"><I>OMN</I></A><P>';
$sig = "<I>This file was generated by hl2html $vn on $date.</I>\n$omn<P>\n";

($#ARGV >= 0) || die "$usg";

if ($l) { ($users = ": " . join(", ",@ARGV)) =~ s|\S*/||g; };

print "<TITLE>User Hotlists$users</TITLE>\n";
print "<H1>User Hotlists$users</H1>\n";

foreach $dir (@ARGV) {
    $hl = "$dir/.mosaic-hotlist-default";
    (-f $hl) || next;
    open(hl,$hl) || (warn "Can't open $hl\n", next);
    if ($v) { print STDERR "Trying $hl ..."; }
    ($user = $dir) =~ s|.*/||;
    $entry = "<H2>$user</H2>\n<UL>\n";
    $empty = 1;
    @hl = ();
    while(<hl>) {
        chop;
        /^ncsa-xmosaic-hotlist-format-1$/ && next;
        /^Default$/ && next;
        print $entry; $empty = 0; $entry = "";
        if (/^(\w+:[^ ]+) +(.*)/) {
            $url = $1;
            $when = $2;
            $title = <hl>; # read next line
            chop $title;
            if ($t && $seen{$url} && $seent{$title}) {
                 if ($v) { print STDERR "seen $title -- skipping it\n"; }
            }
            elsif ($u && $seen{$url}) {
                 if ($v) { print STDERR "seen $title/$url -- skipping it\n"; }
            }
            else {
                $item = "<LI><A HREF=\"$url\">\n    $title</A>\n";
                if ($d) { $item .= "<I>($when)</I>\n"; }
		push(@hl,$item);
            }
            $seen{$url} = 1;
            $seent{$title} = 1;
        }
        # shouldn't happen:
        else { print STDERR " can't parse entry $_\n"; }
    }
    if ($s) { print sort byTitle @hl; }
    else { print @hl; }
    if ($v) {
        if ($empty) { print STDERR " was empty -- ignored\n"; }
        else { print STDERR " got it!\n"; };
    }
    print "</UL>\n";
    close(hl);
}

print "</DL>\n$sig\n";

sub byTitle {
	substr($a,index($a,"\n")) cmp substr($b,index($b,"\n")) ;
}

