#!/bin/sh --
# xmyman2 - script to run the MyMan video game under the X Window System
# Copyright 2003, 2006, Benjamin Sittler <bsittler@gmail.com>
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# If you use a different-sized maze or tileset, you
# should probaby change the geometry and font.

xterm=rxvt
geometry=60x34
font="-xos4-terminus-medium-r-normal--12-120-72-72-c-60-iso10646-1"
termName=rxvt
basename="`basename \"$0\"`"

# don't pollute the child terminal's environment
unset LINES
unset COLUMNS
if ! test -f "$TERMCAP"
    then unset TERMCAP
fi

# first, probe for myman
myman --help >/dev/null || exit $?

# second, probe for XCurses myman
if myman --help | fgrep toolkit-options > /dev/null
then
    exec \
        myman "$@" \
        -- \
        -lines "${geometry##*x}" \
        -cols "${geometry%x*}" \
        -normalFont "$font" \
        -colorBlack "#000000" \
        -colorRed "#D89345" \
        -colorGreen "#00B300" \
        -colorYellow "#FAB345" \
        -colorBlue "#0000D8" \
        -colorMagenta "#FA2093" \
        -colorCyan "#00D8D8" \
        -colorWhite "#FAB393" \
        -colorBoldBlack "#646464" \
        -colorBoldRed "#FA0000" \
        -colorBoldGreen "#93FA00" \
        -colorBoldYellow "#FAFA00" \
        -colorBoldBlue "#2020D8" \
        -colorBoldMagenta "#FAB3D8" \
        -colorBoldCyan "#00FAD8" \
        -colorBoldWhite "#D8D8D8" \
        -cursorColor "#0000DB" \
        -pointerForeColor "#FAB393" \
        -pointerBackColor "#000000" \
        -borderWidth 0
        -xrm "*waitForMap: true" \
        -xrm "*iconName: xmyman"
fi

export statusfile
statusfile="${TMPDIR:-/tmp}/${basename}$$.st"

cleanup()
{
    rm -f "${statusfile}"
}

trap cleanup 0

export arglist
arglist=
while test $# -gt 0
do
  arglist="${arglist}"' ${arg'$#'}'
  eval export 'arg'$#
  eval 'arg'$#'="${1}"'
  shift
done

echo 1 > "${statusfile}" || exit $?

"$xterm" \
-geometry "$geometry" \
+sb \
-fn "$font" \
-bg "#000000" \
-fg "#FAB393" \
-cr "#0000DB" \
-fb "$font" \
-tn "$termName" \
-n "xmyman" \
-title "X MyMan" \
--color0  "#000000" \
--color1  "#D89345" \
--color2  "#00B300" \
--color3  "#FAB345" \
--color4  "#0000D8" \
--color5  "#FA2093" \
--color6  "#00D8D8" \
--color7  "#FAB393" \
--color8  "#646464" \
--color9  "#FA0000" \
--color10 "#93FA00" \
--color11 "#FAFA00" \
--color12 "#2020D8" \
--color13 "#FAB3D8" \
--color14 "#00FAD8" \
--color15 "#D8D8D8" \
--colorBD "#FAFA00" \
--colorUL "#2020D8" \
-pr "#FAB393" \
+rv \
-e /bin/sh -c 'myman -o -u '"${arglist}"'; st=$?; echo "${st}" > "${statusfile}"; echo; echo "[ press RETURN to continue ]"; stty -echo; read x; exit "${st}"'
st="${?}"
if test :"${st}" = :"0"
then
    read st < "${statusfile}"
fi
cleanup
exit "${st:-1}"
