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

# first, check to see that /mit/infoagents exists, else die.
if [ ! -d /mit/infoagents ]; then
    echo You must first 'add infoagents' before using Netscape.
    exit 1
fi    

# second, if the user's home dir is in AFS and has no quota,
# warn that they're going to get weirdness.
if df "$HOME" | grep /afs >/dev/null 2>&1; then
   quota=`fs lq ${HOME} | awk '/user\.'$USER'/ { print $2; }'`
   usage=`fs lq ${HOME} | awk '/user\.'$USER'/ { print $3; }'`

   if [ "$usage" -ge "$quota" ]; then
      echo You are out of AFS disk quota.
      echo Netscape may complain about lock files
      echo and may not be able to save settings properly.
      echo We recommend that you not use Netscape under
      echo these conditions.
   fi
fi

# third, check to see that we're not on a dialup machine.
# if so, gripe (but continue).
if [ -x /etc/athena/dialuptype ]; then
     cat << EOF

*** Please don't run Netscape on the dialups! ***

Netscape is a very large and resource-intensive program. Running it on
x.dialup uses a lot of processing power that could be spent on other
things.

Please run Netscape on your local machine rather than running it on
x.dialup.

Thanks -- the infoagents and dialup maintainers

EOF
fi

# 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

common_lockers="acro graphics gnu outland swtools"
/bin/athena/attach -h -n -q $common_lockers

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
WWW_HOME=${WWW_HOME-"http://web.mit.edu/"}

# Set architecture-dependent variables
case "${use_athdir}" in
    true)
	gnubin=`/usr/athena/bin/athdir /mit/gnu`
	graphicsbin=`/usr/athena/bin/athdir /mit/graphics`
	acrobin=`/usr/athena/bin/athdir /mit/acro`
	;;
    *)
	gnubin=/mit/gnu/arch/${ATHENA_SYS}/bin
	graphicsbin=/mit/graphics/arch/${ATHENA_SYS}/bin
	acrobin=/mit/acro/arch/${ATHENA_SYS}/bin
	;;
esac

PATH=${PATH}:${gnubin}:${graphicsbin}:${acrobin}:${NS_HOME}

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

# Warn if cache in wrong place

if [ -d ${HOME}/.netscape/cache ]; then
    warn=`${gnubin}/gdu -sk ${HOME}/.netscape/cache | \
	awk '{if ($1>5) { print $1 } else { print "nowarn"}}'`
    case "$warn" in
	nowarn)
	    ;;
	*)
	    echo ""
	    echo ' **** Netscape is eating '$warn'K of your quota! ****'
	    echo "To correct, this situation, quit Netscape, and then run"
	    echo "    add infoagents"
	    echo "    netscape-fix"
	    echo "    cd"
	    echo "    rm -rf .netscape/cache"
	    ;;
    esac
fi

# Get default preferences if needed. 3.x and 4.x do it differently.

case $release in
    3*)
        if [ ! -r ${HOME}/.netscape/preferences ]; then
	    if [ ! -d ${HOME}/.netscape ]; then
		mkdir ${HOME}/.netscape
	    fi    
	    if [ ! -d ${HOME}/.netscape/archive ]; then
		mkdir ${HOME}/.netscape/archive
	    fi    
	    sed s/ATHENA_USERNAME/$USER/g \
		/mit/infoagents/share/Netscape/${release_dot}/preferences \
		    > ${HOME}/.netscape/preferences 
	    if [ ! -r ${HOME}/.netscape/bookmarks.html ]; then
		cp /mit/infoagents/www/netscape/bookmarks.html ${HOME}/.netscape \
		    && chmod u+w ${HOME}/.netscape/bookmarks.html
	    fi
	fi
	;;    
    *)
        if [ ! -r ${HOME}/.netscape/preferences.js ]; then
	    # If there's a 3.x prefs file, show the 3.x->4.x warning.
	    # If warning returns nonzero, quit.
	    if [ -r ${HOME}/.netscape/preferences ]; then
		/bin/athena/attach -h -n -q tcl
		if /mit/infoagents/share/Netscape/${release_dot}/from3x "$@"
		then
		    : go for it ...
		else
		    exit
		fi
	    fi
	    if [ ! -d ${HOME}/.netscape ]; then
		mkdir ${HOME}/.netscape
	    fi    
	    # If we create these directories, we avoid warnings about them.
	    # The former generally stays empty, but 4.6 warns about its
	    # creation anyway.
	    if [ ! -d ${HOME}/.netscape/archive ]; then
		mkdir ${HOME}/.netscape/archive
	    fi    
	    if [ ! -d /var/tmp/.netscape-cache-$USER ]; then
		mkdir /var/tmp/.netscape-cache-$USER
	    fi    
	    sed s/ATHENA_USERNAME/$USER/g \
		/mit/infoagents/share/Netscape/${release_dot}/preferences.js \
		    > ${HOME}/.netscape/preferences.js 
	    if [ ! -r ${HOME}/.netscape/bookmarks.html ]; then
		cp /mit/infoagents/www/netscape/bookmarks.html ${HOME}/.netscape \
		    && chmod u+w ${HOME}/.netscape/bookmarks.html
	    fi
	else
	    case $release_dot in
		*4.61*)
		    # If there's a preferences.js but no history.dat,
		    # show the 4.05->4.61 warning. This is not the best way to
		    # do this, and will have to change if we install future 4.x
		    # versions.
		    if [ ! -s ${HOME}/.netscape/history.dat ]; then
			/bin/athena/attach -h -n -q tcl
			/mit/infoagents/share/Netscape/${release_dot}/from4x "$@"
		    fi
		    ;;
	    esac
	fi
	;;
esac


# 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

# On public ws, wait for Mozilla to exit, then remove cache.
# On private ws, just exec.

grep "^PUBLIC=true" /etc/athena/rc.conf >/dev/null 2>&1
case "$?" in
  1)
	# File found and line not found - private ws
	# Leave cache between sessions
	exec ${NS_HOME}/netscape "$@"
	;;
  *)
	# File not found or PUBLIC=true found - assume public ws
	# Remove cache on exit
	trap 'rm -rf /var/tmp/.netscape-cache-$USER' 0
	${NS_HOME}/netscape "$@"
	exit $?
	;;
esac



