#!/bin/bash
#
# translate -- script for translating words
# V0.5: Copyright(C) 1999 by Jochem Huhmann <joh@revier.com>
# Modified by Wolfgang Jhrling <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=0.6
LOCDIR=~/.translate
GLOBDIR=/usr/share/trans
GLOBALCONF=/etc/translate.conf
LOCALCONF=$LOCDIR/translate.conf
LANG=de-en
WHOLEWORD=false
DONT_ASK=false
INVERS=false

# The help message

usage ()
  {
    echo "
`basename $0` Version $VERSION		20/6/1999		joh@revier.com

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

USAGE: 
`basename $0` [-niwvh] [-l languages] [words to translate]

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

EXAMPLE: `basename $0` -l de-en simplest
"
  }

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

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

# If there is no $LOCDIR we should create one
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 "vhniwl:" "$@"`
for i 
do
  case "$i"
    in
    -v) echo "`basename $0` Version $VERSION"; exit 0 ;;
    -w) WHOLEWORD=true ; shift;;
    -h) usage; exit 0 ;;
    -n) DONT_ASK=true; shift;;
    -i) invert_lang; shift;;
    -l) LANG=$2; shift; shift;;
    --) shift; break;;    
  esac
done                                   
if
  [ $# = 0 ] 
  then
    usage; exit 1
fi

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

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

# now get the real work done
if

    if 
	[ $WHOLEWORD = false ]
    then
	if 
	    [ $INVERS = false ]
	then
	    egrep -ihs  "$*".*' :: ' $DIC
	else
	    egrep -ihs  ' :: '.*"$*" $DIC
	fi
    else
	if 
	    [ $INVERS = false ]
	then
	    egrep -wihs  "$*".*' :: ' $DIC
	else
	    egrep -wihs  ' :: '.*"$*" $DIC
	fi
    fi

then # Done
  exit 0
else # Fallback to human intelligence (unless "-n" is present)
  if 
    [ $DONT_ASK = true ] 
  then 
    echo "Sorry \"$*\" not found" 
  else
    echo "No matches. Teach me or press Return."
    echo -n "$* :: "
    read NEW_ENTRY
    if [ -z "$NEW_ENTRY" ]
    then # User is lazy
      exit 0
    else # We've got a new entry
      if
         [ $INVERS = false ]
      then
        echo "$* :: $NEW_ENTRY" >> $LOCDIC
      else 
        echo "$NEW_ENTRY :: $*" >> $LOCDIC
      fi
        exit 0
    fi
  fi
fi
exit 0
