#!/bin/sh
appname=`basename "$0"`
ver="0.1a-.2003040621"
copy="2003"
mail="PointedEars@gmx.de"
mail_feedback="bug-babylon@PointedEars.de"
# ----------------------------------------------------------------------------
#     babylon 0.4a+ -- babylon.com Command-Line Client 
#     Copyright (C) 2003  Thomas Lahn <PointedEars@gmx.de>
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
## Standard shell script disclaimer blurb thing:
##
## This script is a hack.  It's brute force.  It's horrible.
## It doesn't use Artificial Intelligence.  It doesn't use Virtual Reality.
## It's not perl.  It's not python.  It probably won't work unchanged on
## the "other" thousands of unices.  But it worksforme.  --ramiro
# (from /usr/local/mozilla/run-mozilla.sh)
#
#     This is work in progress. If you have an improvement, patch,
#     idea, whatever, on how to make this script better, please
#     send it to <bug-htdict@PointedEars.de>

# from /etc/rc.status

if test -z "$LINES" -o -z "$COLUMNS" ; then
    eval `stty size 2>/dev/null | (read L C; \
    echo LINES=${L:-24} COLUMNS=${C:-80})`
fi
test $LINES   -eq 0 && LINES=24
test $COLUMNS -eq 0 && COLUMNS=80

if test "$TERM" != "raw" && stty size >/dev/null 2>&1 ; then
  esc=`echo -en "\033"`
  extd="${esc}[1m"
  norm=`echo -en "${esc}[m\017"`
else
  esc=""
  extd=""
  norm=""
fi

# check for fold
fold="fold -sw $COLUMNS"
chk=`which fold 2> /dev/null`
test -z "$chk" && fold="cat"
chk=`echo x | $fold 1> /dev/null`
test -n "$chk" && fold="cat"

# check for fmt
chk=`which fmt 2> /dev/null`
if [ -z "$chk" ]; then
  fmt=$fold
else
  # check for columns
  fmt="fmt -$COLUMNS"
  chk=`echo x | $fmt 2> /dev/null`
  test -z "$chk" && fmt="fmt"
  # check for -c|--crown-margin
  chk=`echo x | $fmt -c 2> /dev/null`
  if [ -n "$chk" ]; then
    fmt=$fmt" -c"
  else
    chk=`echo x | $fmt --crown-margin 2> /dev/null`
    test -n "$chk" && fmt=$fmt" --crown-margin"
  fi
fi

function DB_Note {
  echo "Note: All babylon.com options may be subject to change."
}

function help {
  echo "${extd}${appname}${norm}
  [${extd}-i${norm}] [${extd}-l${norm} LANGUAGE] [${extd}-n${norm} ENTRIES] [${extd}-u${norm} UA] QUERY 

  ${extd}-i${norm}, ${extd}--interactive${norm}   Interactive mode (browsing)
  ${extd}-l${norm}, ${extd}--language${norm}      Translate QUERY into LANGUAGE.
    LANGUAGE            ${extd}ar${norm}      Arabic
                        ${extd}cn${norm}      Chinese(T)
                        ${extd}cn-s${norm}    Chinese(S)
                        ${extd}nl${norm}      Dutch
                        ${extd}en${norm}      English
                        ${extd}fr${norm}      French
                        ${extd}de${norm}      German
                        ${extd}gr${norm}      Greek
                        ${extd}he${norm}      Hebrew
                        ${extd}it${norm}      Italian
                        ${extd}jp${norm}      Japanese
                        ${extd}kr${norm}      Korean
                        ${extd}pt${norm}      Portuguese
                        ${extd}ru${norm}      Russian
                        ${extd}es${norm}      Spanish
                        ${extd}sw${norm}      Swedish
                        ${extd}tr${norm}      Turkish
  ${extd}-n${norm}, ${extd}--number${norm}        Return ENTRIES entries at once.
    ENTRIES             The default is 10.
  ${extd}-u${norm}, ${extd}--user-agent${norm}    Use the HTTP user agent \
specified by UA.
    UA                  Default is \`links' (preferred) or \`lynx'.
                        For proper non-interactive mode, the user agent must
                        support the \`-dump' argument to dump the rendered
                        search result to the standard output.
  QUERY               Database query string.
  "
  DB_Note
  echo
}

echo "
${extd}babylon $ver -- babylon.com Command-Line Client${norm}
Copyright (C) $copy  Thomas Lahn <$mail>
Distributed under the terms of the GNU General Public License (GPL),
see COPYING file or http://www.fsf.org/copyleft/gpl.html for details.
See http://babylon.com/ for further information.
" | $fold

# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need `tmp' as the `eval set --' would nuke the return value of getopt.

#   echo "${extd}`basename $0`${norm}
#   [${extd}-i${norm}] [${extd}-u${norm} UA] [${extd}-t${norm} S_TYPE] \
# [ ( ${extd}-d${norm} DB_ID | ${extd}-D${norm} | ${extd}-l${norm} ) ] \
# [ ( ${extd}--info${norm} | QUERY ) ]

if `getopt -T >/dev/null 2>&1` ; [ $? = 4 ] ; then
  getopt_type=long
  tmp=`getopt -o hl:iu:n: \
              -l help,language:interactive,user-agent:,number: \
              -n "$appname" -s sh \
              -- "$@"`
else
  getopt_type=short
  tmp=`getopt l:iu:n: "$@"`
fi

getopt_exit_code=$?
ua=""
opt=-dump
db=*
if [ $getopt_exit_code -eq 0 ]; then
##     getopt  returns  error  code 0 for successful parsing, 1 if
##     getopt(3) returns errors, 2 if it does not understand  its
##     own parameters, 3 if an internal error occurs like out-of-
##     memory, and 4 if it is called with -T.
#
# Note the quotes around `$tmp': they are essential!
  eval set -- "$tmp"
  number=10
  while true ; do
    case "$1" in
      -h | --help)        help; exit 0;;
      -l | --language)
        case "$2" in
          ar)   lang=15;;  # Arabic
          cn)   lang=9;;   # Chinese(T)
          cn-s) lang=10;;  # Chinese(S)
          nl)   lang=4;;   # Dutch
          en)   lang=0;;   # English
          fr)   lang=1;;   # French
          de)   lang=6;;   # German
          gr)   lang=11;;  # Greek
          he)   lang=14;;  # Hebrew
          it)   lang=2;;   # Italian
          jp)   lang=8;;   # Japanese
          kr)   lang=12;;  # Korean
          pt)   lang=5;;   # Portuguese
          ru)   lang=7;;   # Russian
          es)   lang=3;;   # Spanish
          sw)   lang=48;;  # Swedish
          tr)   lang=13;;  # Turkish
        esac
        shift 2;;
      -i | --interactive) opt=""; shift;;
      -u | --user-agent)  ua=$2; shift 2;;
      -n | --number)      number=$2; shift 2;;
      --) shift; break;;
      *) echo "Internal error!" ; exit 1;;
    esac
  done
  test -z "$query" && query="$*"
else
#   echo "getopt exited: $getopt_exit_code
#   " >&2
  test $getopt_exit_code -eq 1 && echo && help
  exit $getopt_exit_code
fi

if [ -z "$ua" ]; then
  # check for links
  chk=`which links 2> /dev/null`
  if [ $? -eq 0 ]; then
    ua=links
  else
    chk=`which lynx 2> /dev/null`
    if [ $? -eq 0 ]; then
      ua=lynx
    else
      echo "`basename $0`: No appropriate user agent detected.
      "
      exit 255
    fi
  fi
fi
if [ -z "$query" ]; then
  echo "Please provide a query string."
  echo
  help
  exit 254
else
  $ua $opt "http://info.babylon.com/cgi-bin/info.cgi?word=$query&lang=$lang&ot=2&layout=combo2.html&n=$number"
fi

echo
