#!/bin/ksh
# This is a korn shell script for accessing a machine readable
# dictionary 
# More information about script programming in UNIX can be found in 
# ...
# The input of this script is a string which should be specified on
# the command line
# 960523 Erik TKS <erik.tjong@ling.uu.se>

# Dictionary location; replace this with your own dictionary
DICTIONARY="/usr/users/staff/erikt/P/dl96/dictionary"
# source language specification in field names
LANGUAGE="ENG"
# target language specification in field names
TARGET="SWE"

# define TMPDIR if it is not already defined
if [ -z "$TMPDIR" ]
then
   TMPDIR="/tmp"
fi

# define temporary file
TMPFILE="$TMPDIR/searchScript.$$.html"

# make sure that the temporary file will be removed even in case of a crash
trap "/bin/rm -f $TMPFILE" 0 1 2 3 9

# Make initial part of html file
echo "<html>
<head>
<title>Query result</title>
</head>
<body>
<h2>Query result</h2>
" > $TMPFILE

# SEARCH PART
# convert options to variable contents
while [ -n "$1" ]
do
   case $1 in
   # example option; replace by your own and add others
   -s)
      SUMMARY="true"
      ;;
   # this one defines the search string: no change necessary
   *)
      SEARCHSTRING="$1"
      ;;
   esac
   shift
done

CASESENSITIVE="no"
# Do we have a search string?
if [ -z "$SEARCHSTRING" ]
then
   echo "No search string specified" >> $TMPFILE
else
   # put every field of the dictionary on one line
   ## first remove the strings after #
   sed 's/#.*$//' $DICTIONARY |\
   ## then replace the empty lines by }
      sed 's/^ *$/{/' |\
   ## then replace the newlines by }
      tr '\012' '}' |\
   ## replace the inserted { by new lines
      tr '{' '\012' |\
   # Do we need to search case sensitive?
   if [ "$CASESENSITIVE" = "yes" ]
   then
      ## case sensitive search required
      grep "${LANGUAGE}.WORD:  *$SEARCHSTRING *}" 
   else
      ## case insensitive search required: use -i option  
      grep -i "${LANGUAGE}.WORD:  *$SEARCHSTRING *}" 
   fi|\
   # put a paragraph tag in front of each line
   sed 's/^/<p>/' |\
   # put word in bold
   sed 's/${LANGUAGE}.WORD: \([^}]*\)}/<HR><b>\1<\/b>}/g'|\
   # at this point you have the entries you are looking for with all
   # the file names. Here you should add sed commands to change the
   # output to the format you want it to be by adding html tags or
   # optionally removing some fields.
   #
   # replace all lexical field names by <p>
   # sed 's/[A-Z\.]*: *//g' |\
   # replace end of lines } by a <br>
   sed 's/}/<br>}/g' |\
   tr '}' '\012' |\
   # convert characters to html codes 
   htmlize >> $TMPFILE
fi

# print final part of page
echo "
<p>
<hr>
<address>
`date`
</address>
</body>
</html>
" >> $TMPFILE

# show the result with lynx (change browser if the new one starts fast enough)
netscape $TMPFILE

# remove temporary file
/bin/rm $TMPFILE
