#! /bin/bash

## 
## Usage: @PROG@ [OPTIONS] [FILE]
## 
##   Uses gxmessage to view a file, with an option to edit it
## 
## Options:
##   -c   center
##   -h   help
## 


PROG=$(basename $0)
XMESSAGE=${XMESSAGE:-$(which gxmessage)} || XMESSAGE=xmessage
GEDIT=gedit

MSG_TITLE=$PROG
MSG_FG=black
MSG_BG=white
MSG_GEOM="800x600"
# MSG_GEOM="1024x768"
MSG_FONT="mono 11"


[ "$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
}


while getopts ":ch" Option
do
  case $Option in
  c) center=1 ;;
  h) showUsage ;;
  *) invocationError ;;
  esac
done
shift $(($OPTIND - 1))
[ "$#" -gt 1 ] && invocationError


filename="${1:--}"

if [ "$filename" = "-" ]; then
  buttons="GTK_STOCK_CLOSE:102"
else
  MSG_TITLE="$MSG_TITLE: $filename"
  buttons="GTK_STOCK_OPEN:101,GTK_STOCK_CLOSE:102"
fi


$XMESSAGE -title "$MSG_TITLE"       \
          -geometry "$MSG_GEOM"     \
          ${center:+--center}       \
          -font "$MSG_FONT"         \
          -fg "$MSG_FG"             \
          -bg "$MSG_BG"             \
          -buttons "$buttons"       \
          -default GTK_STOCK_CLOSE  \
          -file "$filename"

[ "$?" -eq 101 ] && $GEDIT "$filename"


exit 0

