
      key="${1}"
      if test "${#}" -gt '0'; then shift; fi
      docspec="${*}"
      pwd="`pwd`"
      HTX_SERVER="${HTX_SERVER-http://star-www.rl.ac.uk/cgi-bin/htxserver}"
      path="${HTX_PATH-`${HTX_DIR}/htxpath`}"
      . ${HTX_DIR}/settrap
      catfile="/tmp/htx-dockey-$$.catfile"
      indexdata="/tmp/htx-dockey-$$.indexdata"
      datafile="/tmp/htx-dockey-$$.datafile"
      errfile="/tmp/htx-dockey-$$.errfile"
      resfile="/tmp/htx-dockey-$$.resfile"
      nfile="/tmp/htx-dockey-$$.nfile"
      linefile="/tmp/htx-dockey-$$.linefile"
      rm -f "${catfile}" "${datafile}" "${errfile}" "${resfile}" "${nfile}" \
            "${linefile}"
      settrap 'rm -f "${catfile}" "${datafile}" "${errfile}" "${resfile}" "${nfile}" "${linefile}"'
      if test ! "${HTX_QUIET}" = '1'; then
         nblank0="`${HTX_DIR}/msgover '0' 'gathering document data...'`"
      fi
      search="`for doc in ${docspec}; do echo "${doc}"; done | sed '\?/?d'`"
      if test -n "${search}" -o ! -n "${docspec}"; then
         alldocs="`${HTX_DIR}/allfind`"
      fi
      explicit="`for doc in ${docspec}; do echo "${doc}"; done \
                 | sed '/^[^/]*$/d'`"
      found="`for doc in ${explicit}; do
         if ( cd "${doc}.htx" 1>/dev/null 2>/dev/null ); then echo "${doc}"; fi
      done`"
      doclist=`{
         for doc in ${search} '' ${explicit} '' ${found} '' ${alldocs} ''; do
            echo "${doc}"
         done
         grep -s 2>/dev/null '^' /dev/null \
                     \`echo "${path}:" | sed 's%\([^:]\):%\1/htx.catalogue %g
                                              s%:%%g'\`
      } | awk '
      BEGIN{
         expl[ "" ] = ""
         search[ "" ] = ""
      }{
         if ( ! $0 ) {
            s++
         } else if ( ! s ) {
            given++
            search[ $0 ]++
         } else if ( s == 1 ) {
            given++
            expl[ $0 ]++
         } else if ( s == 2 ) {
            found[ $0 ]++
            nf = split( $0, f, "/" )
            doc = f[ nf ]
            if ( ! done[ doc ]++ ) print
         } else if ( s == 3 ) {
            nf = split( $0, f, "/" )
            doc = f[ nf ]
            if ( ! given || search[ doc ] ) {
               if ( ! done[ doc ]++ ) print
            }
         } else if ( s == 4 ) {
            split( $1, f, ":" )
            doc = f[ 2 ]
            if ( ! given || search[ doc ] ) {
               if ( ! done[ doc ] ) {
                   print >"'"${catfile}"'"
               }
            }
         }
      }
      END{
         for ( doc in expl ) if ( doc ) {
            if ( expl[ doc ] && ! found[ doc ] ) {
               print( "'"${HTX_SCRIPT}"': warning - document \""doc"\" not found" ) >"'"${errfile}"'"
            }
         }
         for ( doc in search ) if ( doc ) {
            if ( search [ doc ] && ! done[ doc ] ) {
               print( "'"${HTX_SCRIPT}"': warning - document \""doc"\" not found" ) >"'"${errfile}"'"
            }
         }
      }'`
      if test -f "${errfile}"; then
         cat "${errfile}" >&2
         rm -f "${errfile}"
      fi
      indexlist=''
      missing=''
      for doc in ${doclist}; do
         ifile="${doc}.htx/htx.index"
         if test -f "${ifile}" -a -r "${ifile}"; then
            indexlist="${indexlist}${ifile} "
         else
            missing="${missing}${doc} "
         fi
      done
      if test -n "${missing}"; then
         for doc in $missing; do
            echo >&2 "${HTX_SCRIPT}: warning - document ${doc} has no index file"
            ${HTX_DIR}/creindex "${doc}" \
            | sed -n 's%^\([Tt] \)%'"${doc}"'.htx/htx.index:\1%p'
         done >"${indexdata}"
      fi
      if test ! -n "${HTX_SEARCH_NAMES}" -a \
              ! -n "${HTX_SEARCH_TITLES}" -a \
              ! -n "${HTX_SEARCH_HEADINGS}" -a \
              ! -n "${HTX_SEARCH_LINES}"; then
         search_default='1'
      else
         search_default=''
      fi
      for nloop in 1 2 3; do
         if test "${search_default}" = '1'; then
            case "${nloop}" in
            1) HTX_SEARCH_TITLES='1'
               if test ! "${HTX_QUIET}" = '1'; then
                  ${HTX_DIR}/msgover >/dev/null "${nblank0}" \
                                                'searching document titles... '
                  nblank0='19'
                  nblank1='10'
               fi
               ;;
            2) unset HTX_SEARCH_TITLES
               HTX_SEARCH_HEADINGS='1'
               if test ! "${HTX_QUIET}" = '1'; then
                  nblank1=`${HTX_DIR}/msgover "${nblank1}" 'headings... '`
               fi
               ;;
            3) unset HTX_SEARCH_HEADINGS
               HTX_SEARCH_LINES='1'
               if test ! "${HTX_QUIET}" = '1'; then
                  nblank1=`${HTX_DIR}/msgover "${nblank1}" 'lines... '`
               fi
               ;;
            esac
            if test ! "${HTX_QUIET}" = '1'; then
               nblank="`expr "${nblank0}" '+' "${nblank1}"`"
            fi
         elif test ! "${HTX_QUIET}" = '1'; then
            txt='searching document '
            sep=''
            if test "${HTX_SEARCH_NAMES}" = '1'; then
               txt="${txt}names"; sep=', '
            fi
            if test "${HTX_SEARCH_TITLES}" = '1'; then
               txt="${txt}${sep}titles"; sep=', '
            fi
            if test "${HTX_SEARCH_HEADINGS}" = '1'; then
               txt="${txt}${sep}headings"; sep=', '
            fi
            if test "${HTX_SEARCH_LINES}" = '1'; then
               txt="${txt}${sep}lines"; sep=', '
            fi
            txt="${txt}... "
            nblank="`${HTX_DIR}/msgover "${nblank0}" "${txt}"`"
         fi
         sflag='T'
         if test "${HTX_SEARCH_HEADINGS}" = '1' -o \
                 "${HTX_SEARCH_LINES}" = '1'; then sflag='[Tt]'; fi
         edit1='\?^[^ :][^ :]*\.htx/htx\.index:'"${sflag}"'  *[^ ][^ ]*? {
                   s%^\([^ :][^ :]*\)\.htx/htx\.index:\([Tt]\)  *\([^ ][^ ]*\)%\2 \1 \1.htx/\3%
                   s%^\([Tt]\) [^ ]*/%\1 %
                   s%^%h %
                   p
                }'
         edit2='\?^[^ :][^ :]*htx\.catalogue:? {
                   s%^\([^:]*\)/[^:/]*:\([^ ]*\)  *\([^ ][^ ]*\)%n T \2 \1/\3%
                   p
                }'
         {
            if test -f "${indexdata}"; then cat "${indexdata}"; fi
            grep '^'"${sflag}"' ' ${indexlist} /dev/null
            if test -f "${catfile}"; then cat "${catfile}"; fi
         } | sed -n -e "${edit1}" -e "${edit2}" >"${datafile}"
         sflag=''
         if test "${HTX_SEARCH_TITLES}" = '1'; then sflag='T'; fi
         if test "${HTX_SEARCH_HEADINGS}" = '1'; then sflag="${sflag}t"; fi
         ucase=''
         if test "${HTX_CASE}" = '1'; then
            mkey="${key}"
         else
            mkey="`echo "${key}" | tr '[a-z]' '[A-Z]'`"
            ucase='y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'
         fi
         pad=''
         if test ! -n "${mkey}"; then
            mkey='^'
         elif test "${HTX_WORD}" = '1'; then
            mkey="[^A-Za-z0-9_]${mkey}[^A-Za-z0-9_]"
            pad='s%^\(.*\)$% \1 %'
         fi
         delim="`echo ' ' | tr ' ' '\001'`"
         edit1=''
         edit2=''
         edit3=''
         edit0='s%^%0000 %'
         if test "${HTX_SEARCH_NAMES}" = '1'; then
            edit1='/^.... . T / {
                      h
                      s%^.... [^ ] T \([^ ][^ ]*\).*%\1%
                      '"${ucase}
                        ${pad}"'
                      \'"${delim}${mkey}${delim}"'{
                         g
                         s%^..\(..\)%11\1%
                         h
                      }
                      g
                   }'
         fi
         if test "${HTX_SEARCH_TITLES}" = '1'; then
            edit2='/^.... . T / {
                      h
                      s%^.... [^ ] T [^ ][^ ]*  *[^ ][^ ]* *%%
                      '"${ucase}
                        ${pad}"'
                      \'"${delim}${mkey}${delim}"'{
                         g
                         s%^.\(.\).\(.\)%1\11\2%
                         h
                      }
                      g
                   }'
         fi
         if test "${HTX_SEARCH_HEADINGS}" = '1'; then
            edit3='/^.... . t / {
                      h
                      s%^.... [^ ] t [^ ][^ ]*  *[^ ][^ ]* *%%
                      '"${ucase}
                        ${pad}"'
                      \'"${delim}${mkey}${delim}"'{
                         g
                         s%^.\(..\).%1\11%
                         h
                      }
                      g
                   }'
         fi
         edit4='/^1/ {
                   s%^1\(.\)\(.\)\(.\)%A \1 \2 \3 0%p
                   s%^A . . . . n . [^ ][^ ]*  *\([^ ][^ ]*\).*%\1%w '"${nfile}"'
                }'
         sortflags=''
         if test "${HTX_SORT}" = '1'; then
            sortflags='-r -n -k1,1 -k2,2 -k3,3 -k4,4'
         fi
         if test "${HTX_SHOWMATCH}" = '1'; then
            editcnts='/^[Tt] / {
               s%^T \([^ ][^ ]*\) \([^ ][^ ]*\) \([^ ][^ ]*\) \([^ ][^ ]*\)%T ( \1 names, \2 titles, \3 pages, \4 lines, )%
               s%^t \([^ ][^ ]*\) \([^ ][^ ]*\) \([^ ][^ ]*\) \([^ ][^ ]*\)%t ( \1 names, \2 titles, \3 headings, \4 lines, )%
               s%^[Tt] %%
               s% 1 names% 1 name%
               s% 1 titles% 1 title%
               s% 1 headings% 1 heading%
               s% 1 pages% 1 page%
               s% 1 lines% 1 line%
               s% 0 names,%%
               s% 0 titles,%%
               s% 0 headings,%%
               s% 0 pages,%%
               s% 0 lines,%%
               s%^( \([^)]*\), )\(.*\)$%\2 (\1)%
            }'
         else
            editcnts='s%^[Tt] [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* *%%'
         fi
         {
            if test -f "${datafile}"; then
               sed -n -e "${edit0}" -e "${edit1}" -e "${edit2}" -e "${edit3}" \
                      -e "${edit4}" "${datafile}"
            fi
            if test -f "${nfile}"; then
               for file in `cat "${nfile}"`; do
                  if test ! -r "${file}"; then echo "B ${file}"; fi
               done
               rm -f "${nfile}"
            fi
            if test "${HTX_SEARCH_LINES}" = '1'; then
               gflags=''
               if test ! "${HTX_CASE}" = '1'; then gflags='-i'; fi
               if test "${HTX_WORD}" = '1'; then gflags="${gflags} -w"; fi
               nb='4'
               ndone='0'
               nmatch='0'
               nmax="`echo "${doclist}" | wc -w | awk '{print $1}'`"
               for doc in ${doclist}; do
                  grep ${gflags} -c "${key}" \
                     `${HTX_DIR}/showhtml "${doc}.htx"` /dev/null \
                  | sed '\%^/dev/null:%d
                     \%^0*$%d
                     \%:0*$%d
                     s%^\(.*\):\(.*\)$%\2 '"${doc}"' \1%
                     s%^\([^ ]*\) [^ ]*/\([^ /]*\)%\1 \2%
                     s%^\([^ ]*\) %A 0 0 0 \1 h c %' \
                  | tee "${linefile}"
                  ndone="`expr "${ndone}" '+' '1'`"
                  if test -s "${linefile}"; then
                     nmatch="`expr "${nmatch}" '+' '1'`"
                  fi
                  rm -f "${linefile}"
                  if test ! "${HTX_QUIET}" = '1'; then
                     pc="`expr '(' '100' '*' "${ndone}" ')' '/' "${nmax}"`"
                     case "${nmatch}" in
                     1) txt=" (${pc}% done, 1 match)... ";;
                     *) txt=" (${pc}% done, ${nmatch} matches)... ";;
                     esac
                     nb="`${HTX_DIR}/msgover "${nb}" "${txt}"`"
                  fi
               done
               if test ! "${HTX_QUIET}" = '1'; then
                  ${HTX_DIR}/msgover "${nb}" '... ' >/dev/null
               fi
            fi
            if test "${HTX_SEARCH_HEADINGS}" = '1' \
                 -o "${HTX_SEARCH_LINES}" = '1'; then
               sflag='T'
               if test "${HTX_SEARCH_LINES}" = '1'; then sflag='[Tt]'; fi
               sed -n 's%^\(h '"${sflag}"' \)%C \1%p' "${datafile}"
            fi
         } | awk '
         BEGIN{
            fnl[ "" ] = ""
            fthere[ "" ] = ""
         }{
            if ( $1 == "A" ) {
               nct = $2
               tct = $3
               hct = $4
               lct = $5
               type = $7
               doc = $8
               file = $9
               if ( lct == "0" ) {
                  if ( type == "T" ) {
                     if ( ! dthere[ doc ]++ ) line[ ++nline ] = substr( $0, 11 )
                     dnn[ doc ] += nct
                     dnt[ doc ] += tct
                  } else if ( type == "t" ) {
                     line[ ++nline ] = substr( $0, 11 )
                     fthere[ file ] = 1
                     fnh[ file ] = 1
                     docname[ file ] = doc
                  }
               } else if ( lct != "0" ) {
                  fnl[ file ] = lct
                  docname[ file ] = doc
               }
            } else if ( $1 == "B" ) {
               remote[ $2 ] = 1
            } else if ( $1 == "C" ) {
               type = $3
               doc = $4
               file = $5
               hdata[ file ] = substr( $0, 3 )
               if ( type == "T" ) tdata[ doc ] = hdata[ file ]
            }
         }
         END{
            for ( file in fnl ) if ( file ) {
               doc = docname[ file ]
               dnl[ doc ] += fnl[ file ]
               if ( ! fthere[ file ]++ ) {
                  if ( hdata[ file ] ) {
                     line[ ++nline ] = hdata[ file ]
                  } else {
                     line[ ++nline ] = "h t "doc" "file" [no heading]"
                  }
               }
            }
            for ( file in fthere ) if ( file ) {
               doc = docname[ file ]
               dnh[ doc ]++
               if ( ! dthere[ doc ]++ ) {
                  if ( tdata[ doc ] ) {
                     line[ ++nline ] = tdata[ doc ]
                  } else {
                     line[ ++nline ] = "h T "doc" ""???"" [no title]"
                  }
               }
            }
            for ( i = 1; i <= nline; i++ ) {
               split( line[ i ], f )
               type = f[ 2 ]
               doc = f[ 3 ]
               file = f[ 4 ]
               if ( type == "T" ) {
                  if ( dnn[ doc ] ) dnn[ doc ] = 1
                  if ( ! written[ doc ]++ ) {
                     if ( remote[ file ] ) {
                        print( int( dnn[ doc ] ), int( dnt[ doc ] ), int( dnh[ doc ] ), int( dnl[ doc ] ), "r "substr( line[ i ], 3 ) )
                     } else {
                        print( int( dnn[ doc ] ), int( dnt[ doc ] ), int( dnh[ doc ] ), int( dnl[ doc ] ), line[ i ] )
                     }
                  }
               } else {
                  print( 0, 0, int( fnh[ file ] ), int( fnl[ file ] ), line[ i ] )
               }
            }
         }' | sort ${sortflags} -f -k9 | awk '
         {
            if ( $6 == "T" ) {
               doc[ ++ndoc ] = $0
            } else {
               page[ ++npage ] = $0
               docname = $7
               pagelist[ docname ] = pagelist[ docname ]npage" "
            }
         }
         END{
            print( int( ndoc ) ) >"'"${resfile}"'"
            if( ndoc ) {
               print( "<UL>" )
               for ( i = 1; i <= ndoc; i++ ) {
                  print( "<LI>" )
                  print( doc[ i ] )
                  if ( brief != "1" ) {
                     split( doc[ i ], f )
                     docname = f[ 7 ]
                     np = split( pagelist[ docname ], pages )
                     if ( np > 0 ) {
                        print( "<UL>" )
                        for ( ip = 1; ip <= np; ip ++ ) {
                           print( "<LI>" )
                           print( page[ pages[ ip ] ] )
                           print( "</LI>" )
                        }
                        print( "</UL>" )
                     }
                  }
                  print( "</LI>" )
               }
               print( "</UL>" )
            }
         }' brief="${HTX_BRIEF}" - \
         | sed -e \
            's%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* . . [^ ][^ ]*  *[^ ][^ ]* *\)$%\1 [no title]%
             s%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* . . [^ ][^ ]*\)  *\([^/]\)%\1 '"${pwd}"'/\2%
             s%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* r .\) \([^ ][^ ]*\)  *[^ ][^ ]*%\1 \2 '"${HTX_SERVER}"'/\2.htx/\2.html?xref_%
             s%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]* h T [^ ][^ ]*  *[^ ][^ ]*\)%\1#xref_%
             s%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]*\) . T \([^ ][^ ]*\) \([^ ][^ ]*\) \(.*\)%T \1 <A HREF="\3">\2</A> - \4%
             s%^\([^ ][^ ]* [^ ][^ ]* [^ ][^ ]* [^ ][^ ]*\) . t \([^ ][^ ]*\) \([^ ][^ ]*\) \(.*\)%t \1 <A HREF="\3">\4</A>%' \
               -e "${editcnts}"
         if test -f "${resfile}"; then
            ndoc="`cat "${resfile}"`"
            rm -f "${resfile}"
         else
            ndoc='0'
            break
         fi
         if test "${ndoc}" != '0' -o \
                 "${search_default}" != '1'; then break; fi
      done
      if test ! "${HTX_QUIET}" = '1'; then
         case "${ndoc}" in
         0) txt='no matches found';;
         1) txt='1 document matched';;
         *) txt="${ndoc} documents matched";;
         esac
         ${HTX_DIR}/msgover "${nblank}" "${txt}" >/dev/null
         echo '' >/dev/tty
      fi
      rm -f "${datafile}" "${catfile}" "${indexdata}"
      exit "${ndoc}"
