#!/bin/sh
# NetHack 3.6  nethack.sh	$NHDT-Date: 1552425075 2019/03/12 21:11:15 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.20 $
# Copyright (c) 2015 by Kenneth Lorber, Kensington, Maryland
# NetHack may be freely redistributed.  See license for details.

HACKDIR="/usr/pkg/share/nethackdir360"
export HACKDIR

UILIST="curses tty" 
if [ -n "$DISPLAY" ]
then
    UILIST="qt x11 $UILIST" 
fi

case "$1" in
	qt|x11|curses|tty)
	    echo "Please select the UI by putting 'windowtype:$1' in your \$NETHACKOPTIONS"
	    echo "or 'OPTIONS=windowtype:$1' at the top of your \$HOME/.nethackrc file."
	    exit 1
	    ;;
esac

HACK=""

for x in $UILIST
do
	if [ -x "/usr/pkg/bin/nethack-$x" ]
	then
		HACK="/usr/pkg/bin/nethack-$x"
		break
	fi
done

if [ -z "$HACK" ]
then
	echo "No UI found (nethack-$UILIST)"
	exit 1
fi

# Since app-defaults/NetHack is installed in /usr/pkg/lib/X11,
# add it to XUSERFILESEARCHPATH.
case "x$XUSERFILESEARCHPATH" in
x)	XUSERFILESEARCHPATH="/usr/pkg/lib/X11/app-defaults/%N"
	;;
*)	XUSERFILESEARCHPATH="$XUSERFILESEARCHPATH:/usr/pkg/lib/X11/app-defaults/%N"
	;;
esac
export XUSERFILESEARCHPATH

# Get font dir added, but only once (and only if there's an xset to be found).
test -n "$DISPLAY" -a -f /usr/pkg/share/fonts/X11/misc/fonts.dir && xset p >/dev/null 2>&1 && (
	if ! xset q | grep /usr/pkg/share/fonts/X11/misc >/dev/null 2>&1
	then
	    xset fp+ /usr/pkg/share/fonts/X11/misc
	fi
)

# see if we can find the full path name of PAGER, so help files work properly
# assume that if someone sets up a special variable (HACKPAGER) for NetHack,
# it will already be in a form acceptable to NetHack
# ideas from brian@radio.astro.utoronto.ca
if test \( "xxx$PAGER" != xxx \) -a \( "xxx$HACKPAGER" = xxx \)
then

	HACKPAGER=$PAGER

#	use only the first word of the pager variable
#	this prevents problems when looking for file names with trailing
#	options, but also makes the options unavailable for later use from
#	NetHack
	for i in $HACKPAGER
	do
		HACKPAGER=$i
		break
	done

	if test ! -f $HACKPAGER
	then
		IFS=:
		for i in $PATH
		do
			if test -f $i/$HACKPAGER
			then
				HACKPAGER=$i/$HACKPAGER
				export HACKPAGER
				break
			fi
		done
		IFS=' 	'
	fi
	if test ! -f $HACKPAGER
	then
		echo Cannot find $PAGER -- unsetting PAGER.
		unset HACKPAGER
		unset PAGER
	fi
fi


cd $HACKDIR
exec $HACK "$@"
