#! /bin/bash

## 
## Usage: @PROG@ ["WORD"]
## 
##   Look up WORD using the dict program, and display it using gxmessage
## 
# (This is just for fun - gnome-dictionary is much nicer)


PROG=$(basename $0)
XMESSAGE=${XMESSAGE:-gxmessage}

MSG_TITLE=$PROG
MSG_FONT="sans 10"
MSG_FG=black
MSG_BG=white
MSG_GEOM=600x400
MSG_POS=-center


[ "$XMESSAGE" = "xmessage" ] && MSG_FONT=fixed


invocationError ()
{
  echo "Try '$PROG -h'" >&2
  exit 64
}


showUsage ()
{
  sed -n '/^##/s/^## //p' $0 | sed -e "s/@PROG@/${PROG}/g"
  exit 0
}


[ "$1" = "-h" -o "$1" = "--help" ] && showUsage

if [ "$#" -eq 1 ]; then
  word=$1
elif [ "$#" -eq 0 -a "$XMESSAGE" = "gxmessage" ]; then
  # the -entry option is gxmessage specific
  word=$($XMESSAGE -fn "$MSG_FONT"     \
                   -fg "$MSG_FG"       \
                   -bg "$MSG_BG"       \
                   -title "$MSG_TITLE" \
                   $MSG_POS            \
                   -entry              \
                   "Enter word:")
  [ -z "$word" ] && exit 0
else
  invocationError
fi

dict "$word" 2>&1 | $XMESSAGE -geom "$MSG_GEOM"   \
                              -fn "$MSG_FONT"     \
                              -fg "$MSG_FG"       \
                              -bg "$MSG_BG"       \
                              -title "$MSG_TITLE" \
                              $MSG_POS            \
                              -default okay       \
                              -file -

