#! /bin/sh

# need httpd directory to get at unescape;
# you may need to use another way to reach this; if your
# server is running with `chroot' enabled, you might need to
# refer to it as /support/unescape instead of giving the path.

httpd=/share/contract/src/www/apache_0.8.14/

# add the directory containing the lq-text executables to $PATH:
PATH=$PATH:/usr/local/bin

# lq-text uses $LQTEXTDIR to get its configuration files; $LQTEXTDIR
# must be the name of a directory containing the lq-text database to be
# searched.
LQTEXTDIR="/usr/local/gnats/LQTEXTDIR"

# DOCROOT is where the delivered documents live.
DOCROOT="/~lee/gnats"

# SERVER is the name of the current server, e.g. http://www.sqrex.sq.com:
SERVER="http://sqrex"

# cgi-bin is where the programs live on the server (normally "cgi-bin"):
CGI_BIN=cgi-bin

# IF you are indexing SGML, these arguments are passed to lqkwic(1):
## SGML_FLAGS='-E $LQTEXTDIR/entity-replacement.txt -c -C '
SGML_FLAGS=;

# if you add any variables, you probably need to export them here:
export httpd PATH LQTEXTDIR DOCROOT SERVER CGI_BIN SGML_FLAGS

if test ! -f "$LQTEXTDIR/data"
then
    echo Content-type: text/html
    echo
    cat << EOF
<HTML><HEAD><TITLE>lq-text Search Failed</TITLE></HEAD>
<BODY><H1>401 - lq-text Index Failed</H1>
<P>The text retrieval index isn't available.</P></BODY></HTML>
EOF
    exit 0
fi

echo Content-type: text/html
echo

if [ $# = 0 ];
then
    cat << EOM
<HTML><HEAD><TITLE>lq-text Search Form</TITLE><ISINDEX></HEAD>
<BODY><H1>lq-text Index Search</H1>

<P>To search the database, enter any word or phrase.</P>
<P>You can also try <A HREF="${SERVER}/${CGI_BIN}/search.html">a more
complex (and more powerful) search interface [not available yet]</A>.
</P>
<P>Please report problems or excitements to <A HREF="mailto:lee@sq.com">Liam</A>.
EOM

exit $?

ARGS=`$httpd/support/unescape -e "$*"`
cd $LQTEXTDIR/..

cat << EOM
<HTML><HEAD><TITLE>lq-text Search Results</TITLE><ISINDEX></HEAD>
<BODY><H1>lq-text Index Results</H1>
<H2>Query was: `echo "$ARGS" | sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g'`</H2>

EOM

# decode PATH_INFO if given
Start=1
if test ! x"$PATH_INFO" = x""
then
    case "$PATH_INFO" in
    /[1-9]/*|/[1-9][0-9]/*|/[1-9][0-9][0-9]/*|/[1-9][0-9][0-9][0-9]/*)
	OldQuery=`expr "$PATH_INFO" : '/[1-9][0-9]*/\(..*\)$'`
	if test x"$OldQuery" = x"$QUERY_STRING"
	then
	    Start=`expr "$PATH_INFO" : '/\([1-9][0-9]*\)/.*$'`
	else
	    # the query is different, so start from the top
	    Start=1
	fi
	;;
    esac
fi
set +x
End=`expr $Start + 75`

tmp=/tmp/lqwww.$$
linkfile=/tmp/lqwwwB.$$
export tmp
trap '/bin/rm -f $tmp $linkfile; exit' 0 1 2 3 15

> "$tmp"
> "$linkfile"
/usr/local/bin/lqrank "$ARGS" | mawk '
BEGIN {
    Start = '"$Start"'
    MaxInOneGo = '"${End}"' - Start
    LinkFile = "'"${linkfile}"'"
    fileCount = 0
    lastFile = ""
}

{
    print >> "'"$tmp"'"
    if ($5 != lastFile) {
	lastFile = $5
	++fileCount
    }
}

END {
    if (lastFile == 0) {
	printf "<P>No matches were found.</P>"
    } else if (NR == 1) {
	printf "<P>One match was found.</P>"
    } else if (fileCount == 1) {
	printf "<P>One document matched, containing %d matches</P>",
						    NR
    } else {
	printf "<P>%d documents matched, with a total of %d hits</P>",
						    fileCount, NR
    }
    printf "\n"
    if (NR - Start >= MaxInOneGo || Start > 1) {

	BaseURL = "'"${SERVER}${SCRIPT_NAME}"'"
	Query = "'"${QUERY_STRING}"'"

	First = Start - MaxInOneGo - 1
	if (First < 1) {
	    First = 1
	}
	Last = Start + MaxInOneGo * 2 - 1
	if (Last > NR) {
	    Last = NR
	}

	if (Start == 1) {
	    printf "<P>The first %d matches are given in this document.\n",
		    MaxInOneGo >> LinkFile
	} else {
	    if (First < Start) {
		printf "<BR><A HREF=\"%s/%d/%s?%s\">Matches %d to %d</A>\n",
		    BaseURL, First, Query, Query, First, Start - 1 >> LinkFile
	    }
	    LastInHere = Start + MaxInOneGo - 1
	    if (LastInHere > NR) {
		LastInHere = NR
	    }
	    printf "<BR>Matches %d to %d are given in this document.\n",
		    Start, LastInHere >> LinkFile
	}

	if (NR - Start >= MaxInOneGo) {
	    if (Last > Start + MaxInOneGo) {
		printf \
		    "<BR><A HREF=\"%s/%d/%s?%s\">Matches %d to %d</A><BR>\n",
			BaseURL, Start + MaxInOneGo, Query, Query,
			Start + MaxInOneGo, Last >> LinkFile
	    }
	}
	printf "</P>\n" >> LinkFile
    }
}
'

cat $linkfile

(
    # Run lqkwic -xvv (or lqkwic -t3 -x) to get information about
    # the -s and -S options and the little language that goes in them.

    /usr/local/bin/lqkwic -o${Start}-${End} -e $SGML_FLAGS  -e \
    -S \
'/PREDocument B${FileNumber}/B: A HREF="'"${SERVER}/${DOCROOT}"'/${DocName}"I${DocTitle}/I/APRE' \
    -s \
'${MatchNumber}: ${TextBefore/30r 30l}A${NL}HREF="'"${SERVER}/${CGI_BIN}"'/lqtext-deliver/${DocName}?m=${MatchInFile}&q='"${QUERY_STRING}"'" ${MatchedText}/A${NL}${TextAfter/45 45}${NL}' \
    < $tmp

) 2>&1 | sed -e 's/</\&lt;/g' -e 's/>/\&gt;/g' -e 's//</g' -e 's//>/g'
echo "</PRE>"
cat $linkfile
cat << EOM2
    <P>You can also try <A HREF="${SERVER}${CGI_BIN}/search.html">a
    more powerful search interface</A>.</P>
EOM2

# -c      -- suppress SGML flags
# -C char -- replace SGML tags with `char'
# -e      -- an empty match file indicates no matches were found
# -E file -- "file" contains SGML entity names and values 


## echo "<PRE>"
## echo args are "$*"
## set
## echo "</PRE>"

echo "</BODY></HTML>"

exit 0

