#!/bin/bash
#
# This small shell script is the starter for the
# Netscape Communicator.
#
# SuSE, 1996-2000
#

trap "echo" 31

prog=""
if [ -x /opt/netscape/netscape ] ; then
	prog="/opt/netscape/netscape"
else
	prog="$prog $(type -p netscape)"
	prog="$prog $(type -p Mosaic)"
	prog="$prog $(type -p mosaic)"
	prog="$prog $(type -p arena)"
	prog="$prog $(type -p amaya)"
fi

preloader ()
{
    #
    # See which binary type we have: libc6 or libc5.
    #
    lddout="$(ldd $1 2>/dev/null)"
    nspipepatch=libnspipepatch.so.0.0	
    while read line; do
	case "$line" in
	    libc.so.5*) lib=libc5  ;;
	    libBrokenLocale.so.*)
			lib=libc6n ;;
	    libc.so.6*) lib=libc6o ;;
	    libXext.so.6*)
		set -- $line
		case "$(find $3 -printf '%l\n')" in
		    libXext.so.6.[0-3])  nspipepatch=libnspipepatch.so.0.0 ;;
		    *)			 nspipepatch=libnspipepatch.so.0.1 ;;
		esac
		;;
	esac
    done < <(exec echo "$lddout")

    #
    # For libc5 programs this should be preloaded
    #
    if test -e /usr/i486-linux-libc5/lib/libgnumalloc.so.5 ; then
	LIBGNUMALLOC=/usr/i486-linux-libc5/lib/libgnumalloc.so.5
    else
	LIBGNUMALLOC=/usr/lib/libgnumalloc.so.5
    fi
    if test $lib = libc5 -a -e $LIBGNUMALLOC ; then
	if test -n "$LD_PRELOAD" ; then
	    LD_PRELOAD="$LIBGNUMALLOC $LD_PRELOAD"
	else
	    LD_PRELOAD="$LIBGNUMALLOC"
	fi
	export LD_PRELOAD
    fi

    #
    # For libc6 only based systems
    #
    if test $lib = libc6o -a -e /lib/libBrokenLocale.so.1 ; then
	#
	# This glibc based netscape isn't linked against libBrokenLocale
	#
	if test -n "$LD_PRELOAD" ; then
	    LD_PRELOAD="/lib/libBrokenLocale.so.1 $LD_PRELOAD"
	else
	    LD_PRELOAD="/lib/libBrokenLocale.so.1"
	fi
	export LD_PRELOAD
    fi

    if test -e $MOZILLA_HOME/$nspipepatch ; then
	NSFIX_LIB=/lib/libc.so.6
        test $lib = "libc5" && NSFIX_LIB=/lib/libc.so.5
	export NSFIX_LIB
	if test -n "$LD_PRELOAD" ; then
	    LD_PRELOAD="$MOZILLA_HOME/$nspipepatch $LD_PRELOAD"
	else
	    LD_PRELOAD="$MOZILLA_HOME/$nspipepatch"
	fi
	export LD_PRELOAD
    fi
}

#
for p in $prog ; do
	case "${p##*/}" in
	netscape*)
		# Switch from alarm(2) to select(2) in
		# netscape's thread handling
		# NSPR_NOCLOCK=1; export NSPR_NOCLOCK
		#
		lang="${LANG%[_@]*}"
		if test ${#lang} -gt 2 -a -r /usr/share/locale/locale.alias ;then
			while read alias real ; do
				case "$alias" in
					\#*|"") ;;
					*)  if test "${lang}" = "${alias%[_@]*}" ; then
						LANG=${real}
						break
					    fi  ;;
				esac
			done < <(exec grep -vE '^#|^$' /usr/share/locale/locale.alias)
		fi
		#
		# Multi-Language is buggy in netscape
		#
		test -z "$LANG" && LANG=POSIX
		export LANG
		enable -n printf >/dev/null 2>&1
		if test "`printf "%1.1f" 1 2>/dev/null`" != "1.0" ; then
			LC_NUMERIC=POSIX
			export LC_NUMERIC
		fi
		if test "`printf "%1.1f" 1 2>/dev/null`" != "1.0" ; then
			LC_COLLATE=$LANG
			LC_CTYPE=$LANG
			LC_MONETARY=$LANG
			LC_MESSAGES=$LANG
			LC_TIME=$LANG
			LC_ALL=""
			export LC_COLLATE LC_CTYPE LC_MONETARY LC_MESSAGES LC_TIME LC_ALL
		fi
		#
		# Find Mozillas home
		#
		for d in /opt/netscape     /usr/local/netscape     \
			 /usr/lib/netscape /usr/local/lib/netscape \
			 /usr/netscape
		do
			if test -d $d ; then
				MOZILLA_HOME=$d
				d=$d/app-defaults
				XFILESEARCHPATH=%D:$d/%L/%N:$d/%l_%t/%N:$d/%l/%N:$d/%N
				unset d
				break
			fi
		done
		test -n "$MOZILLA_HOME"     && export MOZILLA_HOME
		test -n "$XFILESEARCHPATH"  && export XFILESEARCHPATH
		if test -s "$MOZILLA_HOME/registry" ; then
			MOZILLA_SHARED_REGISTRY=$MOZILLA_HOME/registry
			export MOZILLA_SHARED_REGISTRY
		fi
		#
		# netscape should find it's movemail and nethelp
		#
		PATH=$MOZILLA_HOME:$PATH
		export PATH
		#
		# Motif-Datenbank der Tastensymbole
		#
		if [ ! -r /usr/X11R6/lib/X11/XKeysymDB ] ; then
			XKEYSYMDB=${MOZILLA_HOME}/XKeysymDB
			export XKEYSYMDB
		fi
		#
		# Time zone
		#
		TZ=:/etc/localtime
		export TZ
		#
		# Disable X Color Management System
		#
		XCMSDB=/dev/null
		export XCMSDB
		#
		# Workaround for XFree Bug (< XFree86[tm]-4.0) ??
		#
		MOZILLA_LINUX_INSERT_LINK_FIX=1
		export MOZILLA_LINUX_INSERT_LINK_FIX
		#
		# Who needs this?
		#
		test -z "$DISPLAY" && DISPLAY=:0
		display=$DISPLAY
		for o in ${1+"$@"} ; do
			if test "$o" = -display; then
				display=""
				continue
			fi
			test -z "$display" && display="$o"
		done
		export DISPLAY display
		#
		# Acrobat plugin
		#
		adir=""
		for d in /usr/lib/Acrobat[1-9]		\
			 /usr/X11R6/lib/Acrobat[1-9]	\
			 /usr/Acrobat[1-9]		\
			 /usr/local/Acrobat[1-9]	\
			 /opt/Acrobat[1-9]
		do
			test -d $d && { adir=$d/Browsers/intellinux; break; }
		done
		if test -n "$adir" -a -d "$adir" ; then
			adir=$HOME/.netscape/plugins:$adir:$MOZILLA_HOME/plugins
			if test -n "$NPX_PLUGIN_PATH" ; then
				NPX_PLUGIN_PATH=$adir:$NPX_PLUGIN_PATH
			else
				NPX_PLUGIN_PATH=$adir
			fi
			export NPX_PLUGIN_PATH
		fi
		#
		# Class path
		#
		for d in $HOME/.netscape/java/classes	\
			 $HOME/.netscape/java/download	\
			 $HOME/.netscape/java		\
			 $HOME/.netscape/plugins	\
			 $HOME/.netscape
		do
			test -d $d || continue
			if test -n "$CLASSPATH" ; then
				CLASSPATH=$CLASSPATH:$d
			else
				CLASSPATH=$d
			fi
		done
		if test -n "$CLASSPATH" ; then
			jars=""
			rest=""
			for j in $MOZILLA_HOME/java/classes/*.jar	\
				 $MOZILLA_HOME/plugins/*.jar
			do
				case "$j" in
					*/java[0-9]*.jar) jars=$j	;;
					*/*.jar)	  rest=$rest:$j	;;
				esac
			done
			CLASSPATH=$MOZILLA_HOME/java/classes:$CLASSPATH
			CLASSPATH=$MOZILLA_HOME/plugins:$CLASSPATH
			CLASSPATH=$jars$rest:$CLASSPATH
			export CLASSPATH
		fi

		#
		# Real Player
		#
		rdir=""
		for d in /opt/{rv,rvplayer,rvplayer?.?}		\
			 /usr/lib/{rv,rvplayer,rvplayer?.?}	\
			 /usr/local/{rv,rvplayer,rvplayer?.?}	\
			 /usr/local/lib/{rv,rvplayer,rvplayer?.?}
		do
			if test -d $d ; then
				rdir=$d
				break
			fi
		done
		if test -n "$rdir" ; then
			if test -n "$LD_LIBRARY_PATH" ; then
				LD_LIBRARY_PATH=$rdir:$LD_LIBRARY_PATH
			else
				LD_LIBRARY_PATH=$rdir
			fi
			export LD_LIBRARY_PATH
		fi
		#
		# General expansion of Java, Plugins, and shared libraries
		#
		OIFS="$IFS"
		IFS=': ,'
		for d in ${MOZILLA_ADDONS:-""}
		do
			test -d "$d" || continue
			if test -r "$d/mozilla.rc" ; then
				# Hopefully they do not chrash this
				# script or Netscape its self
				. "$d/mozilla.rc"
			else
				if test -d "$d/plugins/" ; then
					if test -n "$NPX_PLUGIN_PATH" ; then
						NPX_PLUGIN_PATH=$d/plugins:$NPX_PLUGIN_PATH
					else
						NPX_PLUGIN_PATH=$d/plugins
					fi
					export NPX_PLUGIN_PATH
				fi
				if test -d "$d/classes/" ; then
					jars=
					for j in $d/classes/*.jar ; do
						test -r "$j" || continue
						jars=$jars:$j
					done
					if test -n "$CLASSPATH" ; then
						CLASSPATH=${CLASSPATH}$jars
					else
						CLASSPATH=$MOZILLA_HOME/java/classes$jars
					fi
					export CLASSPATH
				fi
				if test -d "$d/lib/" ; then
					if test -n "$LD_LIBRARY_PATH" ; then
						LD_LIBRARY_PATH=$d/lib:$LD_LIBRARY_PATH
					else
						LD_LIBRARY_PATH=$d/lib
					fi
					export LD_LIBRARY_PATH
				fi
				test -d "$d/bin/" && PATH=$d/bin:$PATH
			fi
		done
		IFS="$OIFS"
		#
		# Security
		#
		HN=$HOME/.netscape
		if [ ! -d $HN ] ; then
			mkdir -m 0700 $HN
			mkdir -m 0700 $HN/cache
			mkdir -m 0700 $HN/archive
		fi
		if [ ! -e $HN/preferences.js ] ; then
			default='HP="http://www.suse.de/"'
			/sbin/pidof httpd > /dev/null 2>&1 && default='HP="http://localhost/"'
			umask 077
			set -f
			cat > $HN/preferences.js <<-EOF
				// Netscape User Preferences
				// This is a generated file!  Do not edit.

				user_pref("browser.cache.disk_cache_size", 7680);
				user_pref("browser.cache.memory_cache_size", 1024);
				user_pref("browser.enable_style_sheets", false);
				user_pref("browser.fe_java_xt_app_timeout", 1);
				user_pref("browser.startup.homepage", $default);
				user_pref("browser.startup.homepage_override", false);
				user_pref("editor.dont_lock_spell_files", true);
				user_pref("helpers.global_mailcap_file", "/etc/mailcap");
				user_pref("helpers.global_mime_types_file", "/etc/mime.types");
				user_pref("java.applets.stop_on_iconify", true);
				user_pref("javascript.allow.mailnews", false);
				user_pref("javascript.enabled", false);
				user_pref("ldap_2.servers.pab.locale", "C");
				user_pref("li.client.javasecurity", true);
				user_pref("mail.default_html_action", 1);
				user_pref("mail.html_compose", false);
				user_pref("mail.quoted_style", 0);
				user_pref("mail.strictly_mime", true);
				user_pref("network.dnsAttempt", 30);
				user_pref("network.dnsCacheExpiration", 5000);
				user_pref("network.dnsTimout", 30);
				user_pref("network.max_connections", 8);
				user_pref("network.tcpbufsize", 256);
				user_pref("network.tcptimeout", 10);
				user_pref("network.use_async_dns", false);
				user_pref("offline.startup_mode", 1);
				user_pref("security.enable_java", false);
			EOF
			set +f
		fi
		if [ -n "$SECURE_NETSCAPE" -a "$SECURE_NETSCAPE" = "yes" ]; then
			test -L $HN/cookies || rm -rf $HN/cookies
			test -e $HN/cookies || ln -sf /dev/null $HN/cookies
		else
			test -L $HN/cookies && rm -rf $HN/cookies
			touch $HN/cookies
		fi
		chmod -f -R go-rxw $HN &
		#
		# Avoid crash and freezes
		#
		test -e $HN/cert7.db -a ! -s $HN/cert7.db && rm -f $HN/cert7.db
		test -e $HN/pab.na2  -a ! -s $HN/pab.na2  && rm -f $HN/pab.na2
		ln -sf history.dat $HN/history.db
		: ${TMPDIR=/tmp/${USER}-netscape}
		test -d $TMPDIR || { mkdir -m 0700 $TMPDIR || exit 1; }
		export TMPDIR

		#
		# Try to make malloc safety
		#
		MALLOC_CHECK_=4; export MALLOC_CHECK_

		#
		# Start
		#
		dd if=$p of=/dev/null bs=8k conv=sync > /dev/null 2>&1
		preloader $p
		arg0=""
		test -n "$BASH_VERSINFO" && arg0="-a $p"
		exec $arg0 $p ${1+"$@"}
		;;
	*)
		for param in $@ ; do
			if test "$param" = "-remote" ; then
				echo "Parameter \"-remote\" not supported. Exit." 1>&2
				exit 1
			fi
		done
		preloader $p
		exec $p ${1+"$@"}
	esac
done

echo "$0: No WWW browser found in PATH" 1>&2
exit 1