#!/bin/bash
#
# translate -- script for translating words
# V0.5: Copyright(C) 1999 by Jochem Huhmann <joh@revier.com>
# Modified by Wolfgang Jährling <wolfgang@pro-linux.de>
#
# translate 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, or (at your option) any
# later version.
#
# translate 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 XEmacs; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

PATH=$PATH:/usr/bin
# Built-in defaults
VERSION="__VERSION__"
DATE="__DATE__"
LOCDIR=~/.translate
GLOBDIR=/usr/share/trans
GLOBALCONF=/etc/translate.conf
LOCALCONF=$LOCDIR/translate.conf
LANGUAGE=de-en
WHOLEWORD=false
DONT_ASK=false
INVERS=false
COLOR=true

# The help message

usage ()
  {
    echo "
$(basename $0) Version $VERSION ($DATE)

Looks up a word in a file with language-to-language translations
(field separator should be \" :: \") and maintains local dictionaries.

USAGE:
$(basename $0) [-niwCvh] [-l languages] [words to translate]

OPTIONS:
 -n	non-interactive (don't prompt if no matches found)
 -i	inverse lookup (from second to first language)
 -w     search only complete words
 -l	languages to translate between
 -C     disable color output on terminal
 -v	display version and exit
 -h or --help
	display this message

EXAMPLE: $(basename $0) -l de-en -i simplest
"
  }

invert_lang ()
{
  if [ $INVERS = true ]
  then
    INVERS=false
  else
    INVERS=true
  fi
}

wholeword ()
{
 if [ $WHOLEWORD = true ]
 then
    WHOLEWORD = false
 else
    WHOLEWORD = true
 fi
}

display_no_color ()
{
  if [ "$2" = "-n" ]; then
    OPT="-n"
  fi
  if [ $UTF8 -eq 1 ]; then
    echo $OPT "$1"
  else
    echo $OPT "$1" | iconv -f UTF-8 -t $CHARSET
  fi
}

display ()
{
  if [ $COLOR = true ]
  then
    display_no_color "$@" | if
      [ $WHOLEWORD = false ]
    then
      grep -Eihs --color=auto "$SEARCH"
    else
      grep -Eihs --color=auto '\b'"$SEARCH"'\b'
    fi
  else
    display_no_color "$@"
  fi
}

# Don't create $LOCDIR automatically to prevent homedir polution
#if [ ! -d $LOCDIR ]
#  then
#    echo -n "creating private folder $LOCDIR... "
#    mkdir $LOCDIR && echo "OK."
#fi

# Read configuration file, if any
if [ -r $GLOBALCONF ]
then
  . $GLOBALCONF
fi
if [ -r $LOCALCONF ]
then
  . $LOCALCONF
fi

# Command line processing
set -- $(getopt -u -l "help" "vhniwCl:" "$@")
for i
do
  case "$i"
    in
    -v) echo "$(basename $0) Version $VERSION"; exit 0 ;;
    -w) WHOLEWORD=true ; shift;;
    -h) usage; exit 0 ;;
    --help) usage; exit 0 ;;
    -n) DONT_ASK=true; shift;;
    -C) COLOR=false; shift;;
    -i) invert_lang; shift;;
    -l) LANGUAGE=$2; shift; shift;;
    --) shift; break;;
  esac
done
if
  [ $# = 0 ]
  then
    usage; exit 1
fi

# Finally we should know what language to use
GLOBDIC=$GLOBDIR/$LANGUAGE
LOCDIC=$LOCDIR/$LANGUAGE

# Is there already a global dictionary for these languages
# or should we rely on our own, private one?
if [ -r $GLOBDIC ]
then
  if [ -r $LOCDIC ]
  then
    DIC="$GLOBDIC $LOCDIC"
  else
    DIC="$GLOBDIC"
  fi
else
  echo "$(basename $0): Global dictionary $GLOBDIC does not exist or is not readable!"
  if [ -r $LOCDIC ]
  then
    echo "$(basename $0): Fallback to $LOCDIC"
    DIC=$LOCDIC
  else
    echo "$(basename $0): Fallback to $LOCDIC is not possible."
    echo "No dictionaries exist!"
    exit 0
  fi
fi

if [ ! "$CHARSET" ] ; then
    # ISO-8859-1 is just an assumption (a bad one) but I've no idea how to
    # find out the appropriate charmap.  This way, please can at least
    # overwrite it through their configuration files.
    CHARSET="ISO-8859-1"
fi

UTF8=0
if locale 2>&1 | grep -E -q "UTF-8?$"; then
    UTF8=1
fi
SEARCH=$*
[ $UTF8 -eq 0 ] && SEARCH=$(echo "$SEARCH" | iconv -f $CHARSET -t UTF-8)
# now get the real work done
if [ 1 ] ; then
    if
        [ $WHOLEWORD = false ]
    then
        if
            [ $INVERS = false ]
        then
            result=$(grep -Eihs  "$SEARCH".*' :: ' $DIC)
        else
            result=$(grep -Eihs  ' :: '.*"$SEARCH" $DIC)
        fi
    else
        if
            [ $INVERS = false ]
        then
            result=$(grep -Eihs  '\b'"$SEARCH"'\b'.*' :: ' $DIC)
        else
            result=$(grep -Eihs  ' :: '.*'\b'"$SEARCH"'\b' $DIC)
        fi
    fi
fi

if [ "$result" ]; then
  display "$result"
  exit 0
else # Fallback to human intelligence (unless "-n" is present)
  if
    [ $DONT_ASK = true ]
  then
    display "Sorry \"$SEARCH\" not found"
  else
    echo "No matches. Teach me or press Return."
    display_no_color "$SEARCH :: " -n
    read NEW_ENTRY
    if [ -z "$NEW_ENTRY" ]
    then # User is lazy
      exit 0
    else # We've got a new entry
      mkdir -p "$(dirname "$LOCDIC")"
      if
         [ $INVERS = false ]
      # FIXME: handle UTF-8 (?)
      then
        echo "$SEARCH :: $NEW_ENTRY" >> $LOCDIC
      else
        echo "$NEW_ENTRY :: $SEARCH" >> $LOCDIC
      fi
        exit 0
    fi
  fi
fi
exit 0
