#!/bin/sh
# script to run Netscape 3.x or 4.x on Athena platforms where available.

# use athdir method preferentially over ATHENA_SYS.
uname="`uname`"
ATHENA_SYS=${ATHENA_SYS-"@sys"}
archtop=`/usr/athena/bin/athdir /mit/infoagents ""`
case "${archtop}" in
    /mit/infoagents/*)
	# athdir worked; continue to use it
	use_athdir=true;
	;;
    *)
	use_athdir=false;
	ATHENA_SYS=${ATHENA_SYS-"@sys"}
	archtop=/mit/infoagents/arch/${ATHENA_SYS}/
	;;
esac
progname=`basename $0`
NS_HOME=${archtop}MIT-only/${progname}
export NS_HOME

# Determine what version the user asked for

case $progname in
    *3.01*)
	release=301
	release_dot=3.01
	CLASSPATH=$CLASSPATH${CLASSPATH+:}/mit/infoagents/share/Netscape/3.01/java_301
	;;    
    *4.05*)
	release=405
	release_dot=4.05
	;;
    *4.61*)
	release=461
	release_dot=4.61
	;;
    *)
	# Default to 4.61
	release=461
	release_dot=4.61
	;;
esac

# If no netscape for version/platform, tell them.
if [ ! -f ${NS_HOME}/netscape ]; then
	echo Netscape ${release_dot} is unavailable on this `uname` workstation.
	exit 1
fi

# Attach lockers according to platform

case "${uname}" in
    Linux)
	# Java frees freed pointers - gnu malloc package handles this
	if [ -n "${LD_ELF_PRELOAD+x}" ]; then
	    LD_ELF_PRELOAD="${archtop}lib/gnumalloc.so:$LD_ELF_PRELOAD"
	elif [ -n "${LD_PRELOAD+x}" ]; then
	    LD_ELF_PRELOAD="${archtop}lib/gnumalloc.so:$LD_PRELOAD"
	else
	    LD_ELF_PRELOAD="${archtop}lib/gnumalloc.so"
	fi
	export LD_ELF_PRELOAD
	XNLSPATH=/mit/infoagents/share/Netscape/nls.x11r5; export XNLSPATH
	;;
    NetBSD)
	XNLSPATH=/mit/infoagents/share/Netscape/nls.x11r5; export XNLSPATH
	;;
    *)
	;;
esac

# Send any "message of the release"
if [ -x /mit/swtools/bin/send_message ]; then
	for mesg in /mit/infoagents/share/Netscape/${release_dot}/nsmotr*; do
		if [ -r $mesg ]; then
			/mit/swtools/bin/send_message `basename $mesg` $mesg
		fi
	done
fi

# Set environment variables.
export PATH XKEYSYMDB XUSERFILESEARCHPATH WWW_HOME NETSCAPEPATH CLASSPATH NPX_PLUGIN_PATH
XKEYSYMDB=/mit/infoagents/share/Netscape/XKeysymDB
# The next five lines are to allow for a local Netscape X-app-defaults file
# to override the global one in infoagents. Just make sure there's an app-defaults
# file called 'Netscape' (note big N) in your .netscape dir and it will be used instead.
# Warning: This is not a file you should trifle with lightly.
if [ ! -r ${HOME}/.netscape/Netscape ]; then
     XUSERFILESEARCHPATH=${NS_HOME}/%N${XUSERFILESEARCHPATH+:}$XUSERFILESEARCHPATH
else
     XUSERFILESEARCHPATH=${HOME}/.netscape/%N${XUSERFILESEARCHPATH+:}$XUSERFILESEARCHPATH
fi
NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH:-$HOME/.netscape/plugins}:/mit/acro/nsplugins:${archtop}javaplugin-1.2/plugins:${NS_HOME}/plugins:/usr/local/lib/netscape/plugins

# Support for RealPlayer libraries.
LD_LIBRARY_PATH=${archtop}rvplayer5.0:${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH

# Put foreign fonts in the font path
# Note that the font directory is used by the X server, not the client,
# so athdir, etc aren't useful.  Also, the server machine might not have
# the infoagents locker attached.  The fonts are in pcf format, so
# little-endian machines will convert them automatically at load time.
# If you are using a little-endian machine and put the little-endian
# directory in your path before running this script, you'll speed startup
# by at most 1 millisecond on modern computers.

if xset -q | grep -s /infoagents/ > /dev/null; then
	: font path is ok already
else
	fontdir=`cd /mit/infoagents/arch/share/lib/X11/.big_endian; pwd`/
	xset fp+ ${fontdir}
	xset fp rehash
fi

# If not running with -remote, check for lock file from previous session

case x"$@" in
  *-remote*)
	;;
  *)
	case "x`ls $HOME/.netscape/lock 2> /dev/null`" in
	  *lock*)
		# Use wish script to ask user if lock should be removed
		/bin/athena/attach -h -n -q tcl \
		&& /mit/infoagents/arch/share/bin/nslock "$@" \
		&& rm $HOME/.netscape/lock
		;;
	esac
	;;
esac

exec ${NS_HOME}/netscape "$@"
