:
##########################################################################
# Title      :	translate - translate words between German and English
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1999-04-27
# Requires   :	dumphtmltbl, [nicecol], urlencode, wget
# Category   :	WWW
# SCCS-Id.   :	@(#) translate	1.17 10/01/03
##########################################################################
# Description
#	Uses the LEO online dictionary (http://www.leo.org) to translate
#	words between German and english
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.17'

# Set this variable to prevent auto-detection of the fastest AWK:
#: ${NAWK:=gawk}

# Program to get an URL specified on the command line, and write the
# data to standard output
: ${GETURL:=wget}
: ${GETURLFLAGS="-q -O -"}

CGIURL='http://dict.leo.org/ende?search='

usage () {
    echo >&2 "$PN - translate words between German and English, $VER
usage: $PN word [word ...]

This script accesses the LEO translation service http://dict.leo.org/ ."
    exit 1
}

msg () {
    for msgLine
    do echo "$PN: $msgLine" >&2
    done
}

fatal () { msg "$@"; exit 1; }

###############################################################################
# searchprog - search program using search PATH
# usage: searchprog program
###############################################################################

searchprog () {
    _search=$1; shift

    for _dir in `echo "$PATH" | sed "s/^:/.:/;s/:\$/:./;s/:/ /g"`
    do
        [ -x "$_dir/$_search" ] || continue
        echo "$_dir/$_search"
        return 0
    done

    return 1
}

###############################################################################

while getopts :h opt
do
    case "$opt" in
    	# your flags here
	h)	usage;;
	\?)	usage;;
    esac
done
shift `expr ${OPTIND-1} - 1`

[ $# -lt 1 ] && usage

# Search for a suitable "awk" implementation; prefer the fastest one
: ${NAWK:=`searchprog mawk || searchprog gawk || searchprog nawk || echo awk`}

for word
do
    # Get the query result page

    # Note that the resulting page may contain very large lines
    # (> 8K) some standard "awk" (i.e. Solaris 2.5) cannot
    # handle. GNU awk ("gawk") does not have these restrictions.
    # If you don't have "gawk", you could try to limit the line
    # size using other tools before the data reaches "awk".

    encoded=`echo "$word" | urlencode`

    $GETURL $GETURLFLAGS "$CGIURL$encoded" |	# get page data
	{
	    grep . ||
	    	msg "WARNING: could not retrieve page: $CGIURL$encoded"
	} |
    	#umbruch |			# fold long lines
	$NAWK '
	    {
		gsub(/<\/tr>/, "\n&");
		gsub(/<\/table>/, "\n&");
		print
	    }
	' |
	$NAWK '
	    /ENGLIS[C]*H/ {				# Start of result table
	    	do {
		    if ($0 !~ /der.*im.*Trainer/) print "!", $0
		} while (getline >= 0 && $1 !~ /<\/table/)
	    }
	' |
	sed -e 's/&nbsp;/ /g' -e 's/&#160;/ /g' \
		-e 's/&gt;/>/g' -e 's/&lt;/</g' \
		-e 's/	/ /g' -e 's/  */ /g' |
	tr -d ' ' |
	dumphtmltbl -f |		# ...and extract table data
	nicecol -i '\t' |
	egrep . || msg "no translation for \"$word\""
done
