#! /bin/bash

## 
## Usage: @PROG@ [SECTION] PAGE
## 
##   Display the specified manual page using gxmessage
## 


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

MSG_TITLE=$PROG
MSG_FONT="monospace 10"
MSG_FG=black
MSG_BG=white
MSG_GEOM=640x480
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
}


getPrompt ()
{
cat <<-ENDPROMPT
	What manual page do you want?
	E.g.: [SECTION] PAGE
ENDPROMPT
}


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

if [ "$#" -eq 2 ]; then
  sect=$1
  shift
fi

if [ "$#" -eq 1 ]; then
  page=$1
elif [ "$#" -eq 0 -a "$XMESSAGE" = "gxmessage" ]; then
  # the -entry option here is not xmessage-compatible
  page=$(getPrompt | $XMESSAGE -fn "$MSG_FONT"     \
                               -fg "$MSG_FG"       \
                               -bg "$MSG_BG"       \
                               -title "$MSG_TITLE" \
                               $MSG_POS            \
                               -entry              \
                               -file - )
  [ -z "$page" ] && exit 0
  sect=${page%% *}
  page=${page##* }
  [ "$sect" = "$page" ] && sect=
else
  invocationError
fi

man $sect $page 2>&1 |
    sed -e 's/.[[:cntrl:]]//g' |
    $XMESSAGE -geom "$MSG_GEOM"   \
              -fn "$MSG_FONT"     \
              -fg "$MSG_FG"       \
              -bg "$MSG_BG"       \
              -title "$MSG_TITLE" \
              $MSG_POS            \
              -default okay       \
              -file -

