#!/p/kd/fdc/solaris9/wermit +
#
# Script to build a clickable HTTP index of the Kermit FTP archive.
# F. da Cruz, Columbia University, December 2006

# set flag on # debugging

if flag echo \%0 | \v(timestamp) | Debugging...

.title = Kermit FTP archive Web access        # Title for index page
.index = ~kermit/index.html.tmp               # Working copy of index
.final = ~kermit/index.html                   # Final name for index
if flag .final = ~kermit/testing123.html      # Different name for testing
.tmp = ~/tmp/index.txt                        # Directory listing file
.skip = 0                                     # Flag to skip a directory

cd ~kermit                                    # CD to Kermit tree
if fail exit 1 "CD ~kermit failed"            # Check
directory /nobackup /recursive /output:\m(tmp) # Get directory tree listing
if fail exit 1                                # Check
if flag echo Directory OK...

fopen /read \%c \m(tmp)                       # Open the listing
if fail exit 1                                # Check
fopen /write \%o \m(index)                    # Start the index file
if fail exit 1                                # Check

# Write the HTML header

fwrite /line \%o <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
fwrite /line \%o <html lang="en">
fwrite /line \%o <head>
fwrite /line \%o <title>\m(title)</title>
fwrite /line \%o -
 <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
fwrite /line \%o <META http-equiv="Content-Style-Type" content="text/css">
fwrite /line \%o </head>

# Write the beginning of the contents

fwrite /line \%o <body>
fwrite /line \%o <h2>\m(title)</h2>
fwrite /line \%o <blockquote>
fwrite /line \%o <i>This page created \v(timestamp) by</i> -
 <a href="scripts/ckermit/index"><tt>index.ksc</tt></a>
fwrite /line \%o </blockquote>
fwrite /line \%o <p>

fwrite /line \%o Click on any link to download a file with HTTP.
fwrite /line \%o You might need to right-click and choose "save link as" or
fwrite /line \%o "save target as" to prevent your browser from feeding the
fwrite /line \%o file to some random helper application.  The "daily" 
fwrite /line \%o <a href="http://www.columbia.edu/kermit/ckdaily.html">
fwrite /line \%o C-Kermit development build</a> tarball is
fwrite /line \%o <a href="test/tar/x.tar.gz">HERE</a>.  For other files,
fwrite /line \%o scroll down the long list or use the links just below for
fwrite /line \%o quick access to the main areas.
fwrite /line \%o <p>

# Write a quick index to the main sections

fwrite /line \%o [ <a href="#a">a</a> ] &nbsp;
fwrite /line \%o [ <a href="#b">b</a> ] &nbsp;
fwrite /line \%o [ <a href="#c">c</a> ] &nbsp;
fwrite /line \%o [ <a href="#d">d</a> ] &nbsp;
fwrite /line \%o [ <a href="#e">e</a> ] &nbsp;
fwrite /line \%o [ <a href="#c-kermit">c-kermit</a> ] &nbsp;
fwrite /line \%o [ <a href="#archives">archives</a> ] &nbsp;
fwrite /line \%o [ <a href="#bin">binaries</a> ] &nbsp;
fwrite /line \%o [ <a href="#scripts">scripts</a> ] &nbsp;
fwrite /line \%o [ <a href="#test">test</a> ] &nbsp;
fwrite /line \%o [ <a href="#charsets">charsets</a> ] &nbsp;
fwrite /line \%o [ <a href="#newsgroups">newsgroups</a> ]

# Write the file listing

fwrite /line \%o <pre>
.prev = xxx

while 1 {
    fread /line \%c line                # Read a directory listing line
    if fail break                       # Check for end of file
    .s2 := \s(line[44])                 # Get the filename
    if eq "\s(line[1:1])" "d" {         # If it's a directory....
        if flag {
            echo \m(line)
            echo line[8] = '\s(line[8:1])'
        }
        # Don't list the contents of directories that lack read access
        if \m(skip) if == 1 \findex(\m(root),\m(s2)) continue
        .skip = 0
        if eq "-" "\s(line[8:1])" {     # If no world-read permission
            .skip = 1                   # don't show this directory
        } else {
            # Also skip these specific directories...
            if equ \m(s2) public_html .skip = 1
            if equ \m(s2) old .skip = 1
            if equ \m(s2) cu .skip = 1
        }
        # If we're skipping this directory also skip all its subdirectories
        undef root
        if skip .root := \m(s2)
        if flag sho mac skip root
        continue                        # Don't list directories themselves
    } else {                            # Just popped out of a closed subdir
        if \m(skip) if != 1 \findex(\m(root),\m(s2)) .skip = 0
    }
    # If we're skipping this directory don't list this file
    if \m(skip) continue
    if not \findex(->,\m(line)) {       # Don't make links to symlinks
        .s1 := \s(line[1:43])           # 1st part of line
        .s3 := \fword(\m(s2),1,/,-)     # Top-level subdirectory
        if not eq "\m(s3)" "\m(prev)" {
            # If entering a new directory put a line and an anchor
            fwrite /line \%o <a name="\m(s3)"><p><hr></a>
        }
        .prev := \m(s3)                 # Remember for next time
        .line := \m(s1)<a href="\m(s2)">\m(s2)</a> # Construct line with link
    }
    fwrite /line \%o \m(line)           # Write the line to the index
}
# Done - Finish the page, close the files, install the index.

fwrite /line \%o </pre>
fwrite /line \%o <hr>
fwrite /line \%o [ <a href="index.html">Top</a> ] &nbsp; -
 <tt>index.ksc / \v(timestamp)
fwrite /line \%o </body>
fwrite /line \%o </html>
fclose \%c
fclose \%o
chmod 644 \m(index)
rename \m(index) \m(final)
if flag directory \m(final)
exit 0
