#!/bin/sh -vx

# This is a small sh and awk script to make a Gopher link file from
# a the catalog created by waisindex using the -catalog option. For
# very large databases this link file is uselss, but for small data-
# databases (say 50 or less records) you give the user the option of
# searching or browsing for what they are looking for.
#
# For an example connect to
#
#       Name=Brown University Job Openings
#       Type=1
#       Path=1/brown/departs/hum_res/jobinfo
#       Host=gopher.brown.edu
#       Port=70
#
# To use this script you need to change the following variables or
# override them on the command line
#
# gopherdir     Root directory of the Gopher server
#
# linkdir       Directory where the link file should be placed.
#               Link is relative to gopherdir.
#
# indexdir      Directory where the index files are. The indexdir
#               is relative to gopherdir.
#
# index         Name of the index file.
#
# title         Title for the search link
#
# For example,
#
#       ./waisfolder \
#               "/nfs/cwis" \
#               brown/departs/hum_res/jobinfo \
#               usr/hrjobs \
#               hrjobs \
#               "Search the HR Job Openings"
#
# This script builds a link file suitable for UMN's gopherd server.
# I don't expect it is too difficult to modify it to use someone
# else's WAIS index gopher gateway.
#
# AUTHOR: [email protected]
# MODIFIED: 93-06

gopherdir=${1-"/usr/gopher/data"}

linkdir=${2-""}

indexdir=${3-".waisindex"}
index=${4-"index"}
title=${5-"Search..."}

       # Remove the previous link and cache file

rm -f \
       ${gopherdir}/${linkdir}/link \
       ${gopherdir}/${linkdir}/cache

       # Create the link file

touch ${gopherdir}/${linkdir}/link

       # Add the link for the search item

cat >${gopherdir}/${linkdir}/link <<!
Name=${title}
Numb=1
Type=7
Path=7/${indexdir}/${index}
Host=+
Port=+

!

       # Add links for each record in the database

/bin/awk \
       < ${gopherdir}/${indexdir}/${index}.cat \
       >> ${gopherdir}/${linkdir}/link "

BEGIN {
       headline = \"\";
       filename = \"\";
       startbyte = 0;
       stopbyte = 0;
}

\$1 ~ /Headline/ {
       headline = \$2;
       for ( i = 3; i <= NF; i++ )
               headline = headline \" \" \$i;
}

\$1 ~ /DocID/ {
       startbyte = \$2;
       stopbyte = \$3
       filename = substr( \$4, length( \"${gopherdir}\" ) + 1 );
}

\$1 == \"\" {
       if ( ( stopbyte - startbyte ) > 0 ) {
               printf( \
                       \"Name=%s\nType=0\nPath=R%d-%d-/%s\nHost=+\nPort=+\n\n\", \
                       headline, \
                       startbyte, \
                       stopbyte, \
                       filename );
       }
}
"

       # Close the link file (I don't know why this is necessary!)

touch ${linkdir}/link

exit