#!/bin/csh -f
# $Id: in.httpd,v 1.10 1992/09/04 06:19:01 putz Exp $
# in.httpd
#  Perform System 33 query, results as WWW hypertext
#
#  /sys33/doc/<id>				Retrieve document
#        /desc/<id>				Retrieve descriptor
#        /area/<Area>/<And,or>/<op>/<field>/	Single field search
#                    /full			Full command search
#  /phone?<name>				search phone file
#  /locate?<name>				locate a user
#  /finger/machine				run finger
#  /finger/machine?user				run finger
#  /man?<keyword>				search man pages
#  /file/<filename>				Retreive files iff o+r
#  file:<filename>				gateway to file systems
#
set noglob
set webroot = /import-writable/www/parc/www33
set path = ($webroot /usr/ucb /bin /usr/bin)
set clientid = www-gateway:UNKNOWN

# -r 0x42 requests file locations and simple formats
# -S -F -A -B outputs: server/filename, format, start, nbytes
alias call33 system33client -q -c $clientid -r 0x42 -N -S/net/%s -F -A -B

setenv MANPATH /import/doc/top/man:/usr/share/man

set line = `echo $< | tr -d '\015' `	# delete CR from CR-LF end of line
set line = ($line)			# Break into words
if ($#line != 2) set line = ("")	# caught below as error
if ("$line[1]" !~ [gG][eE][tT]*) then
    echo 'invalid HTTP request.  Expected "GET ..."'
    exit
endif
set w3id = $line[2]			# Pick up second arg

if ("$w3id" =~ file://*) then		# gateway request
    set w3id = "`echo $w3id | sed -e 's=^file://[^/]*/=/file/='`"
endif

if ("$w3id" =~ *\?*) then ############################# Search

#   echo you asked for a query of $w3id
    set halves = `echo $w3id | tr '?' ' ' `	# Split keywords from index name
    set parts = `echo $halves[1] | tr '/' ' ' `	# Split index name by slashes
    if ($#halves < 2) then
	set words = ()
    else
	set words = `echo $halves[2] | tr '+' ' ' `  # Split keywords by plusses
    endif
    if ($parts[1] == "phone") then
	echo "<TITLE>PARC/XSoft Phone Listing for $words</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words</H1>"
	echo "<XMP>"
	/import/misc/bin/phone "$words"
	echo "</XMP>"
	echo "Enter a name to look for in the PARC/XSoft phone list"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "locate") then
	echo "<TITLE>Locate $words at PARC</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words</H1>"
	echo "<XMP>"
	/import/misc/bin/locate "$words"
	echo "</XMP>"
	if (-e /tilde/$words[1]/.plan) echo "<A HREF=file:/tilde/$words[1]/.plan>plan file</A>"
	if (-e /tilde/$words[1]/.project) echo "<A HREF=file:/tilde/$words[1]/.project>project file</A>"
	echo "<P>Enter a name of someone to locate (may take a minute)"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "finger" && $#parts == 1) then
	if ($#words == 0) goto FingerMachine
	echo "<TITLE>Check users @ $words</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words</H1>"
	echo "<XMP>"
	set machines = `echo $words | sed 's/ / @/g'`
	set finger = /usr/ucb/finger
	if ("$machines" =~ *.* && "$machines" !~ *parc*) set finger = /import/misc/bin/ifinger
	$finger @$machines
	echo "</XMP>"
	echo "<P>Enter one or more host names"
	echo "or check a particular user on <A HREF=/finger/$words[1]>$words[1]</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "finger" && $#parts > 1) then
	set machine = $parts[2]
	echo "<TITLE>Check $words @ $machine</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words @ $machine</H1>"
	echo "<XMP>"
	set finger = /usr/ucb/finger
	if ("$machine" =~ *.* && "$machine" !~ *parc*) set finger = /import/misc/bin/ifinger
	$finger $words @$machine
	echo "</XMP>"
	echo "<P>Enter a user name or select another <A HREF=/finger/>machine</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "apropos" || $parts[1] == "man") then
	echo "<TITLE>PARC Unix Manuals: $words</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words</H1>"
	echo "Select a <A HREF=/manpage/>man page</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	echo "or enter another keyword"
	echo "<DL>"
	/usr/ucb/man -k $words:q | \
		sed -e 's/^\.[^ ]* //' \
		-e 's/</&lt;/g' -e 's/>/&gt;/g' -e 's/&/&amp;/g' \
		-e 's/^\([-a-zA-Z0-9_.]*\)\(.*\)(\(.*\))[ 	]*- \(.*\)$/\1>\2>\3>\4/g' | \
		awk -F'>' '/>/ {printf "<DT><A HREF=/man/%s/%s>%s</A>%s(%s)<DD>%s\n", substr($3,1,1), $1, $1, $2, $3, $4; next} {print "<DT>"$0"<DD>"}'
	echo "</DL>"
	exit
    endif
    if ($parts[1] == "manpage") then
	echo "<TITLE>PARC Unix Manual: $words</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$words</H1>"
	echo "Enter the name of a manual page"
	echo "or search by <A HREF=/apropos/>keyword</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	echo "<PLAINTEXT>"
	/usr/ucb/man $words:q | col -b | cat -s
	exit
    endif
    if ($parts[1] != "sys33") goto error
    if ($#parts < 6) then			# provide default search
	set parts = ( sys33 area default and contains title )
    endif
    set area = $parts[3]
    set conjtype = $parts[4]		# and, or
    set relop = $parts[5]		# equal, contains, etc.
    set fieldname = $parts[6]

    set query = ()
    foreach w ($words)
	set query = ( $query $conjtype.$relop $fieldname $w )
    end

    cat << ===CAT_END===
<TITLE>System 33 $area search results</TITLE>
<ISINDEX>
<H1>$words</H1>
Select one of the matching documents below, or select another <A HREF=/sys33/>search area</A>, or enter another query. (search method: "$fieldname $relop...$conjtype...")
<DL>
===CAT_END===
    call33 -p descriptionsearch $area $query -X'===DESC_END===\
' | s33descs_to_html_items
    echo "</DL>"

else       ############################################# Not search

#   echo you asked to retrieve $w3id
	
    set parts = `echo $w3id | tr '/' ' ' `    # Break at slashes into array

    if ($#parts == 0) then			# Retrieve Welcome Page
	cat << ===CAT_END===
<TITLE>Welcome to the PARC Web</TITLE>
<H1>PARC Web</H1>
This is the PARC World Wide Web Server on `hostname`.
Select one of the following:
<DL>
<DT><A HREF=/phone/>Phone List</A>
<DD>Search the PARC/XSoft phone list.
<DT><A HREF=/locate/>Locate</A>
<DD>Locate a user at PARC, or check a <A HREF=/finger/>machine</A>.
<DT><A HREF=/apropos/>Unix Help</A>
<DD>Search the PARC Unix Manuals.
<DT><A HREF=/file/>Files</A>
<DD>Browse the PARC Unix file system.
<DT><A HREF=/sys33/>System 33</A>
<DD>Access the PARC System 33 Document Server.
<DT><A HREF=file:/import/www/top/default.html>PARC Info</A>
<DD>PARC and External Information.
</DL>
===CAT_END===
	exit
    endif

    if ($parts[1] == "phone") then
	echo "<TITLE>PARC/XSoft Phone List</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>Phone List</H1>"
	echo "Enter a name to look for in the PARC/XSoft phone list"
	echo "<P>or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "locate") then
	echo "<TITLE>PARC Locator</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>Locate</H1>"
	echo "Enter a name of someone to locate (may take a minute)"
	echo "<P>or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "finger" && $#parts == 1) then
FingerMachine:
	echo "<TITLE>Check a Unix Machine</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>Check a Unix Machine</H1>"
	echo "Enter one or more host names"
	echo "<P>or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "finger" && $#parts > 1) then
	echo "<TITLE>Check a user @ $parts[2-]</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$parts[2-]</H1>"
	echo "Enter a user name or select another <A HREF=/finger/>machine</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "file" && $#parts > 1) then
	set fname = `echo $w3id | sed 's:^/file::'`
	if (-d $fname/) then
	    echo "<TITLE>PARC $fname</TITLE>"
	    echo "<H2>$fname:t</H2>"
	    echo "<DIR>"
	    /bin/ls -la $fname | awk '$8 != "." {printf "<LI><A HREF='http:/file$fname'/%s>%s</A>\n", $8, $8}'
	    echo "</DIR>"
	    exit
	else if ($fname:e != html) then
	    echo "<TITLE>PARC $fname</TITLE>"
	    echo "<H2>$fname:t</H2>"
	    echo "<PLAINTEXT>"
	endif
	cat $fname
	exit
    endif
    if ($#parts > 1 && $parts[1] == "manpage") then
	echo "<TITLE>PARC Unix Manual: $parts[2-]</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$parts[$#parts]</H1>"
	echo "Enter the name of a manual page"
	echo "or search by <A HREF=/apropos/>keyword</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	echo "<PLAINTEXT>"
	/usr/ucb/man $parts[2-]:q | col -b | cat -s
	exit
    endif
    if ($#parts > 1 && $parts[1] == "man") then
	echo "<TITLE>PARC Unix Manual: $parts[2-]</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>$parts[$#parts]</H1>"
	echo "Enter a keyword"
	echo "or select a <A HREF=/manpage/>man page</A> by name"
	echo "or return to the <A HREF=/>PARC Web</A>"
	echo "<PLAINTEXT>"
	/usr/ucb/man $parts[2-]:q | col -b | cat -s
	exit
    endif
    if ($parts[1] == "manpage") then
	echo "<TITLE>PARC Unix Manual</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>Unix Manuals</H1>"
	echo "<P>Enter the name of a manual page"
	echo "<P>or search by <A HREF=/apropos/>keyword</A>"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif
    if ($parts[1] == "apropos" || $parts[1] == "man") then
	echo "<TITLE>PARC Unix Manual Search</TITLE>"
	echo "<ISINDEX>"
	echo "<H1>Unix Help</H1>"
	echo "<P>Enter keywords to find Unix manual pages"
	echo "<P>or select a <A HREF=/manpage/>man page</A> by name"
	echo "or return to the <A HREF=/>PARC Web</A>"
	exit
    endif

    if ($parts[1] != "sys33") then
	cat << ===CAT_END===
<TITLE>Error</TITLE>
<H1>Error</H1>
Sorry, retrieval type "$parts[1]" is not supported.<P>
Please select another part of the <A HREF=/>PARC Web</A>.
===CAT_END===
	exit
    endif
    if ($#parts < 2) then
	cat << ===CAT_END===
<TITLE>PARC System 33</TITLE>
<H1>System 33</H1>
System 33 is a document server at PARC.
Documents are in the following search areas:
<DL>
<DT><A HREF=/sys33/area/PARC/and/contains/title>PARC titles</A>
<DD>Documents from PARC itself (title words).
<DT><A HREF=/sys33/area/PARC/and/prefix/keyword>PARC keywords</A>
<DD>Documents from PARC itself (keywords).
<DT><A HREF=/sys33/area/ALPHAPEDIA/and/equal/word>Alphapedia</A>
<DD>Random House Encyclopedia (words in articles).
<DT><A HREF=/sys33/area/GROLIER/and/equal/word>Grolier</A>
<DD>Grolier Encyclopedia (words in articles).
<DT><A HREF=/sys33/area/XNSMAIL/and/equal/word>XNSMAIL</A>
<DD>PARC Mail Archives (words in messages).
</DL>
Select a search area above or you may select another part of the
<A HREF=/>PARC Web</A>.
===CAT_END===
        exit
    endif	
    if ($parts[2] == "doc") then		# Document itself
	set docid = $parts[3]
	set res = `call33 -p retrieve -f html text -d $docid`
	if ($#res == 0) then
		echo "Sorry, failed to find or convert document $docid"
		exit
	endif
	if ($res[2] != "html") then
	    echo "<TITLE>System 33 Document</TITLE>"
	    echo "Search for a <A HREF=/sys33/area/PARC/and/equal/DocID?$docid>description</A> for this document or select a System 33 <A HREF=/sys33/>search area</A>"
	    echo "<PLAINTEXT>"
	endif
	s33byterange $res[3] for $res[4] $res[1]
	exit

    else if ($parts[2] == "desc") then		# Retrieve descriptor
	set res = `call33 -p retrieve -f desc -d $parts[3]`
	if ($#res == 0) then
		echo "Sorry, failed to find description $parts[3]"
		exit
	endif
	if ($res[2] == "html") then
	    s33byterange $res[3] for $res[4] $res[1]
	else
	    echo "<TITLE>System 33 Document Description</TITLE>"
	    echo "Select one of the description or document links below or select a System 33 <A HREF=/sys33/>search area</A><P>"
	    s33byterange $res[3] for $res[4] $res[1] | s33desc_to_html
	endif
	exit
    else if ($parts[2] == "area") then		# Retrieve index cover page
	set area = $parts[3]
	set conjtype = and
	set relop = equal
	set fieldname = keyword
        if (-e $webroot/hypertext$w3id) then
		cat $webroot/hypertext$w3id
	else if ($#parts >= 6) then
	    set area = $parts[3]
	    set conjtype = $parts[4]		# and, or
	    set relop = $parts[5]		# equal, contains, etc.
	    set fieldname = $parts[6]
	    cat << ===CAT_END===
<TITLE>System 33 $area $fieldname index</TITLE>
<ISINDEX>
<H1>$area $fieldname index</H1>
Select another <A HREF=/sys33/>search area</A>or enter one or more words to find documents.
<DL>
<DT>search area:<DD>$area
<DT>search method:<DD>"$fieldname $relop...$conjtype..."
</DL>
===CAT_END===
	else if ($#parts < 4) then
	    cat << ===CAT_END===
<TITLE>System 33 area $area</TITLE>
<ISINDEX>
<H1>$area query types</H1>
Select one of the query types below,
or select another <A HREF=/sys33/>search area</A>,
or enter some keywords (search method: "$fieldname $relop...$conjtype...").
<DL>
<DT><A HREF=/sys33/area/$area/and>and</A><DD>combine query words with "and"
<DT><A HREF=/sys33/area/$area/or>or</A><DD>combine query words with "or"
</DL>
===CAT_END===
	else if ($#parts < 5) then
	    set conjtype = $parts[4]		# and, or
	    cat << ===CAT_END===
<TITLE>System 33 area $area</TITLE>
<ISINDEX>
<H1>$area ...$conjtype... query types</H1>
Select one of the query types below,
or select another <A HREF=/sys33/>search area</A>,
or enter some keywords (search method: "$fieldname $relop...$conjtype...").
<DL>
<DT><A HREF=/sys33/area/$area>back</A><DD>choose another $area query type
</DL>
<MENU>
===CAT_END===
	    set ops = `call33 -X -p descriptionsearch $area | sed 's/\([<>\\]\)/\\\1/g' `
	    foreach op ($ops)
		echo "<LI><A HREF=/sys33/area/$area/$conjtype/$op>$op</A>"
	    end
	    echo "</MENU>"
	else if ($#parts < 6) then
	    set conjtype = $parts[4]		# and, or
	    set relop = $parts[5]		# equal, contains, etc.
	    cat << ===CAT_END===
<TITLE>System 33 area $area</TITLE>
<ISINDEX>
<H1>$area ...$relop...$conjtype... query types</H1>
Select one of the query types below,
or select another <A HREF=/sys33/>search area</A>,
or enter some keywords (search method: "$fieldname $relop...$conjtype...").
<DL>
<DT><A HREF=/sys33/area/$area/$conjtype>back</A><DD>choose another $area query operator
</DL>
<MENU>
===CAT_END===
	    set fields = (title doctype keyword word desccreator)
	    foreach field ($fields)
		echo "<LI><A HREF=/sys33/area/$area/$conjtype/$relop/$field>$field</A>"
	    end
	    echo "</MENU>"
	endif
    endif
endif		# Not search
exit

error:
echo "Bad document id syntax: $w3id"
exit

