#!/bin/sh
###############################################################################
#
# Netscape Wrapper -- written by H. Peter Anvin <hpa@transmeta.com>
#
# (97.06.28)	Modified for Debian by Brian White <bcwhite@pobox.com>
#
# (1998.10.02)	Modified to support the new netscape4x packages.
#		Adam Heath <doogie@debian.org>
#
# (1999.04.19)	Modified to support Japanese environment.
#		UNO Takeshi <uno@sysplan.co.jp>
#
# (2000/04/05)	Modified to support Japanese
#		Takuo KITAME <kitame@debian.org>
#
# (2000.07.04)  Merged code from Debian JP and Debian-KR to provide more
#		generic, unified CJK support - Anthony Fok <foka@debian.org>
#
# (2000.11.24)  Modified to make session management work
#
###############################################################################

#
# Defaults
#
#set -x
ALLOW_ROOT=no
TRACKING=no
SET_DEFAULTS=yes
ALLOW_USERCONFIG=yes
ALLOW_BINOVERRIDE=no
STALE_LOCK=yes
USE_NS3_SETTINGS=no
NFS_HOME=no
#netscape=BIN:VER:FLA
#navigator=BIN:VER:FLA
#communicator=BIN:VER:FLA
use_ns_remote=yes
DEBUG=no
NS_FIX=no

base_loc=/usr/lib/netscape/base-4
nsremote=${base_loc}/netscape-remote
prefdir=${HOME}/.netscape
pref=$prefdir/preferences.js
lockfile=$prefdir/lock
pwd=$(pwd)
config=/etc/netscape4/config
usercf=${HOME}/.netscape/config
environ=/etc/netscape4/environment
userenv=${HOME}/.netscape/environment
uri_escape=${base_loc}/uri_escape
# defaults

[ -e "$config" ] && . $config
[ "$ALLOW_USERCONFIG" = "yes" -a  -e $usercf ] && . $usercf
[ "$DEBUG" = "yes" ] && set -x

# Override some global only options.
s=$(grep '^TRACKING' $config)
[ "$s" ] && eval $s || TRACKING='no'
s=$(egrep '^ALLOW_BINOVERRIDE' $config)
[ "$s" ] && eval $s || ALLOW_BINOVERRIDE='no'

uri_encode() {
	$uri_escape "ABCDEFGHHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890:?+.!~\/=-_#\$%&'(*;<>@[]^\`{}|" "$1"
}
#
# Find out what we are symlinked to.
#
file="$0"
last="$file"
while [ "$(basename "$file")" != "wrapper" ];do
	last="$file"
	file="$(readlink "$file")"
	[ "x$file" = "x" ] && echo "Can't determine my name.  Aborting. " && exit 1
done
if [ "$(basename "$last")" = "wrapper" ]; then
	if [ "x$NS_W_NAME" = "x" ]; then
		echo "Don't run this wrapper directly!"
		exit 1
	else
		last="$NS_W_NAME"
	fi
fi
if [ "$(basename "$last")" = "netscape-remote" ]; then
	NSREMOTE="$nsremote"
else
	dir="$(dirname "$last")"
	BASEN=$(basename "$last")
	FLA=$(echo $BASEN | cut -f 2 -d -)
	BIN=$(echo $BASEN | cut -f 1 -d -)
	case $last in
		*ator-?motif-*)	VER=$(echo $BASEN | cut -f 3 -d -);;
		*)		VER=$(basename $(dirname "$dir"));;
	esac

#

#
# parameters
# --allow-root
#

#
# Process any private cmdline options.
# this has been changed to *only* parse them at the start of the commandline,
# so as to not break parameters with spaces in them.
#
	while [ $# -gt 0 ];do
		case $1 in
			--allow-root)	ALLOW_ROOT=yes;shift;;
			--no-remote)	nsremote='';shift;;
			--old-window)	OLD_WINDOW=yes;shift;;
			--stale-lock)   STALE_LOCK=yes;shift;;
			*)		break;;
		esac
		shift
	done

	if [ "$STALE_LOCK" = "yes" ]; then
		if [ -L $lockfile ]; then
			ip=$(readlink $lockfile | sed 's/:.*$//')
			pid=$(readlink $lockfile | sed 's/.*://')
			if [ "$NFS_HOME" = "yes" ]; then
				/sbin/ifconfig |  grep -q "addr:$ip" && kill -0 $pid 2>/dev/null || rm $lockfile
			else
				kill -0 $pid 2>/dev/null || rm $lockfile
			fi
		fi

	fi
	if [ "$OLD_WINDOW" = "yes" ]; then
		new_window=""
	else
		new_window=",new-window"
	fi
	if [ "$use_ns_remote" = "no" ]; then
		nsremote=''
	fi
	if [ "$ALLOW_BINOVERRIDE" = "yes" ]; then
		base=$(basename "$0" | sed -e 's/-/_/g')
		eval var=\$$base
		if [ "$var" ]; then
			q=$(echo $var | cut -f 1 -d :)
			[ $q ] && BIN=$q
			q=$(echo $var | cut -f 2 -d :)
			[ $q ] && VER=$q
			q=$(echo $var | cut -f 3 -d :)
			[ $q ] && FLA=$q
		else
			base=${base}_$VER
			eval var=\$$base
			if [ "$var" ];then
				q=$(echo $var | cut -f 1 -d :)
				[ $q ] && BIN=$q
				q=$(echo $var | cut -f 2 -d :)
				[ $q ] && VER=$q
				q=$(echo $var | cut -f 3 -d :)
				[ $q ] && FLA=$q
			fi
		fi
	fi

	based="/usr/lib/netscape/$VER/$BIN"
	netscape="$based/$BIN-$FLA.real"
	netscape_call="$based/$BIN-$FLA"
	if [ "x$NS_W_CALLED" = "x(dns helper)" ]; then
		netscape_name="(dns helper)"
	else
		if [ "x$NS_W_CALLED" = "x" ]; then
			netscape_name="$netscape"
		else
			netscape_name="$based/$BIN-$FLA"
		fi
	fi
	LIBC="$based/libc"
	if [ -e "$LIBC" ]; then
		LIBC=$(cat $LIBC)
	else
		LIBC=$(ldd $netscape|awk 'BEGIN{FS="\."}/ *libc\.so/{print substr($3,1,1)}')
	fi

	#
	# Don't allow running netscape as root
	#
	if [ "$ALLOW_ROOT" != "yes" ]; then
		uid=`id -u`
		if [ $uid -eq 0 ]; then
			echo "$0: Cannot be run as root (for security reasons)"
			exit 1
		fi
	fi
fi

tracking() {
	[ "$TRACKING" != :yes: ] && return
	LOG=log
	[ "$USER" ] && LOG="$USER.log"
	echo $LOGDIR/$LOG
	echo -n "$netscape date=\"$(date)\" args=\"$@\" msg=\"" >> $LOGDIR/$LOG
	echo -n "$@" >> $LOGDIR/$LOG
	echo "'" >> $LOGDIR/$LOG
}

#
# Try calling existing netscape with URL, if fail the start new one.
#

showurl () {
	case "$1" in
		*:*)	url="$1" ;;
		/*)	url="file:$1" ;;
		*)	
			if [ -e "$1" ]; then
				url=`echo "file:$pwd/$1" | sed -e 's:///*:/:g'`
			else
				if [ -n "$1" ]; then
					url="http://$1"
				else
					url=""
				fi
			fi
			;;
	esac
	tracking nsremote start
	if [ $NSREMOTE ]; then
		$NSREMOTE -noraise -remote "openURL($(uri_encode $url)$new_window)" \
			2>/dev/null || run_netscape "$@"
		mv=$?
		if [ $retval -eq 0 ]; then
			retval=$mv
		fi
	elif [ "$nsremote" ]; then
		$nsremote -noraise -remote "openURL($(uri_encode $url)$new_window)" \
			2>/dev/null \ || run_netscape "$url"
		mv=$?
		if [ $retval -eq 0 ]; then
			retval=$mv
		fi
	else
		run_netscape "$url"
	fi
}

run_netscape() {
#
# Set some env vars to make things work better
#
	MALLOC_CHECK_=0
	display=$DISPLAY

	[ -e $environ ] && . $environ
	if [ ! -d ${HOME}/.netscape ]; then
		install -d ${HOME}/.netscape
		if [ "$SET_DEFAULTS" = "yes" -a -d "/etc/netscape4/defaults" ]; then
			cp /etc/netscape4/defaults/*  ${HOME}/.netscape 2>&1 > /dev/null
			chmod 600 ${HOME}/.netscape/*
		fi
		install -d ${HOME}/.netscape/archive ${HOME}/.netscape/cache
	fi
	tracking start
	[ $NPX_PLUGIN_PATH ] && NPX_PLUGIN_PATH=$NPX_PLUGIN_PATH:
	for a in /usr/local/lib/netscape/$VER/plugins \
/usr/local/lib/netscape/plugins \
/usr/lib/netscape/plugins \
/usr/lib/netscape/$VER/plugins \
/usr/lib/netscape/$VER/$BIN/plugins \
${HOME}/.netscape/plugins; do
		if [ -d $a ]; then
			NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}$a:
		fi
		if [ -d $a-libc$LIBC ]; then
			NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}$a-libc$LIBC:
		fi
	done

	[ $NPX_NETHELP_PATH ] && NPX_NETHELP_PATH=$NPX_NETHELP_PATH:
	NPX_NETHELP_PATH=${NPX_NETHELP_PATH}:${HOME}/.netscape/nethelp:\
/usr/lib/netscape/nethelp:\
/usr/lib/netscape/$VER/nethelp:\
/usr/lib/netscape/$VER/$BIN/nethelp

	[ $CLASSPATH ] && CLASSPATH=$CLASSPATH:
	CLASSPATH=${CLASSPATH}:${HOME}/.netscape/java:\
/usr/lib/netscape/java:\
/usr/lib/netscape/$VER/java:\
/usr/lib/netscape/$VER/$BIN/java

	MOZILLA_HOME=$(dirname $netscape)

	for d in \
/usr/lib/netscape/base-4/wrapper.d/* \
/usr/lib/netscape/$VER/wrapper.d/* \
/usr/lib/netscape/$VER/$BIN/wrapper.d/* ; do
		case "$d" in
			*\*)	;;
			*)	. "$d" ;;
		esac
	done

	export MALLOC_CHECK_ NPX_PLUGIN_PATH NPX_NETHELP_PATH display MOZILLA_HOME CLASSPATH
	if [ "$REMOVE_LOCK" = "yes" ]; then
		EXEC=""
	else
		EXEC=exec
	fi
	if [ "$USE_NS3_SETTINGS" = "yes" -a -r ${pref} -a -r ${prefdir}/preferences -a ${prefdir}/preferences -nt ${pref} ];then
		# This will be regenerated from preferences by netscape.
		rm -f $HOME/.netscape/preferences.js
	fi
	if [ "$NS_FIX" = "yes" ]; then
		NSFIX_LIB=/lib/libc.so.$LIBC
		if [ -e $NSFIX_LIB ]; then
			export NSFIX_LIB
		fi
		LIBFIX=/usr/lib/netscape/base-4/libnsfix-libc$LIBC.so
		if [ -e $LIBFIX ]; then
			[ "$LD_PRELOAD" ] && LD_PRELOAD="$LD_PRELOAD:"
			LD_PRELOAD="$LD_PRELOAD$LIBFIX"
		fi
	fi

	if [ "$LD_PRELOAD" ]; then
		if [ "x$NS_W_NAME" = "x" ]; then
			LD_PRELOAD="$LD_PRELOAD" $EXEC $netscape "$@"
		else
			unset LD_PRELOAD
			NS_W_PRELOAD="$LD_PRELOAD" $EXEC $netscape_call "$netscape" "$netscape_name" "$@"
		fi
	else
		if [ "x$NS_W_NAME" = "x" ]; then
			$EXEC $netscape "$@"
		else
			$EXEC $netscape_call "$netscape" "$netscape_name" "$@"
		fi
	fi
	if [ "$REMOVE_LOCK" = "yes" ]; then
		rm $lockfile
	fi
	retval=$?
	tracking stop
}

#
# Check the locale
#
eval `locale`
LOC=`echo $LANG | cut -b1,2 | tr '[A-Z]' '[a-z]'`

#
# Mechanism to run things like the old wrapper.ja and wrapper.ko
# for special locale settings
#
nswrapper=/usr/lib/netscape/base-4/wrapper
if [ X != X${LOC} -a -f ${nswrapper}.${LOC} ]; then
	. ${nswrapper}.${LOC}
fi

#
# Fix "locale" problems when printing to postscript
#
# If the locale uses a decimal separator other than a point printf 
# will complain and it will not return 1.0
#

pnt=$(/usr/bin/printf "%1.1f" 1 2>/dev/null)

if [ "$pnt" != 1.0 ]; then
	# Perhaps we have a "dangerous" value for LANG or LC_NUMERIC. Let's
	# try a "safe" value for LC_NUMERIC.
	LC_NUMERIC=C
	export LC_NUMERIC
	pnt=$(/usr/bin/printf "%1.1f" 1 2> /dev/null)

	if [ "$pnt" != 1.0 ]; then
		# No, it is LC_ALL which is bad. Set LC_*=$LC_ALL for every category 
		# (as expected) except LC_NUMERIC, and then unset LC_ALL.
		LC_COLLATE=$LC_ALL
		LC_CTYPE=$LC_ALL
		LC_MESSAGES=$LC_ALL
		LC_MONETARY=$LC_ALL
		LC_TIME=$LC_ALL
		unset LC_ALL
		export LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_CTIME
		pnt=$(/usr/bin/printf "%1.1f" 1 2> /dev/null)

		if [ "$pnt" != 1.0 ]; then
			# running out of ideas.
			unset LANG
			export LANG
			pnt=$(/usr/bin/printf "%1.1f" 1 2> /dev/null)

			if [ "$pnt" != 1.0 ]; then
				echo "Warning: lang/locale settings will cause printing problems"
			fi
		fi
	fi
fi

#
# Decide what to do...
#

retval=0
if [ "$NSREMOTE" ]; then
	$NSREMOTE "$@"
	retval=$?
elif [ $# -gt 1 ]; then
	if [ $1 = -remote ]; then
		$nsremote "$@"
	else
		run_netscape "$@"
	fi
	retval=$?
elif [ $# -eq 1 ]; then
	if [ "$(echo $1 | cut -c1)" != "-" ]; then
		showurl "$1"
	else
		run_netscape "$@"
	fi
	retval=$?
elif [ -f "$pref" ]; then
# The next line has embedded 'tabs' in it
	homepage=$(grep '"browser.startup.homepage"' $pref |
		sed -e 's/.*, *"//' -e 's/");$//')
	startuppage=$(grep '"browser.startup.page"' $pref |
		sed -e 's/.*, *//' -e 's/);$//')

#	homepage=$(egrep '^HOME_DOCUMENT:[ 	]' $pref |
#		sed -e 's/^HOME_DOCUMENT:[ 	]*//')
	if [ "$homepage" -a -z "$startuppage" ]; then
		showurl "$homepage"
	else
		run_netscape
	fi
	retval=$?
else
	run_netscape
	retval=$?
fi
exit $retval
