#! /bin/bash

## 
## Usage: @PROG@ [FILE]
## 
##   Uses gxmessage to view a file, with an option to edit it
## 


PROG=$(basename $0)
XMESSAGE=${XMESSAGE:-gxmessage}
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
}


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


filename="${1:--}"

if [ "$filename" = "-" ]; then
  buttons="Close:102"
else
  buttons="Edit:101,Close:102"
fi


$XMESSAGE -title "$MSG_TITLE"   \
          -geometry "$MSG_GEOM" \
          -font "$MSG_FONT"     \
          -fg "$MSG_FG"         \
          -bg "$MSG_BG"         \
          -buttons "$buttons"   \
          -default Close        \
          -file "$filename"

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


exit 0

