#! /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:-$(which gxmessage)} || XMESSAGE=xmessage

MSG_TITLE=$PROG
MSG_FONT="sans 13"
MSG_FG=
MSG_BG=
MSG_GEOM=
MSG_POS=-center


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


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" != xmessage ]; then
  # the -entry option is gxmessage specific
  word=$($XMESSAGE ${MSG_FONT:+-fn "$MSG_FONT"} \
                   ${MSG_FG:+-fg "$MSG_FG"}     \
                   ${MSG_BG:+-bg "$MSG_BG"}     \
                   -title "$MSG_TITLE"          \
                   $MSG_POS                     \
                   -entry                       \
                   -bu ""                       \
                   "Enter word:")
  [ -z "$word" ] && exit 0
else
  invocationError
fi

dict "$word" 2>&1 | $XMESSAGE ${MSG_GEOM:+-geom "$MSG_GEOM"} \
                              ${MSG_FONT:+-fn "$MSG_FONT"}   \
                              ${MSG_FG:+-fg "$MSG_FG"}       \
                              ${MSG_BG:+-bg "$MSG_BG"}       \
                              -bu "GTK_STOCK_CLOSE:0"        \
                              -title "$MSG_TITLE"            \
                              $MSG_POS                       \
                              -default GTK_STOCK_CLOSE       \
                              -file -

