#!/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>
#
###############################################################################

#
# Defaults
#
#set -x
nsremote=/usr/lib/netscape/base-4/netscape-remote
prefdir=$HOME/.netscape
pref=$prefdir/preferences.js
pwd=$(pwd)
config=/etc/netscape4/config
usercf=~/.netscape/config
environ=/etc/netscape4/environment
userenv=~/.netscape/environment
args="$@"

# defaults

args="$@"

[ -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_unesacpe to decode
	perl -MURI::Escape -e \
		'print uri_escape(shift,"^-A-Za-z0-9:?_.!~\\/")."\n"' \
		"$1"
}
readlink() {
	perl -e '$l = shift; exit 1 unless -l $l; $r = readlink $l; exit 1 unless $r; print "$r\n"' $1
	return $?
}
#
# Find out what we are symlinked to.
#
file="$0"
last=$file
while [ "$(basename $file)" != "wrapper" ];do
	last=$file
	file="$(readlink $file)"
done
if [ "$(basename $last)" = "wrapper" ];then
	echo "Don't run this directly!"
	exit 1
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
fi

#
# config file
# ALLOW_ROOT=no
# TRACKING=no
# SET_DEFAULTS=yes
# ALLOW_USERCONFIG=yes
# ALLOW_BINOVERRIDE=no
# NO_LD_PRELOAD=no
# netscape=BIN:VER:FLA
# navigator=BIN:VER:FLA
# communicator=BIN:VER:FLA
#

#
# parameters
# --allow-root
#

#
# Process any private cmdline options.
#
args=''
argc=0
while [ $# -gt 0 ];do
	case $1 in
		--allow-root)	ALLOW_ROOT=yes;;
		--no-remote)	nsremote='';;
		*)		args="$args $1";argc=$(($argc+1));;
	esac
	shift
done
set -- $args

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

netscape="/usr/lib/netscape/$VER/$BIN/$BIN-$FLA.real"


#
# Don't allow running netscape as root
#
if [ "$ALLOW_ROOT" != "yes" ];then
	eval `id|sed -e 's/([^ ,]*)//g'`
	if [ $uid -eq 0 ]; then
		echo "$0: Cannot be run an root (for security reasons)"
		exit 1
	fi
fi

tracking() {
	[ "$TRACKING" != :yes: ] && return
	LOG=log
	[ "$USER" ] && LOG="$USER.log"
	echo $LOGDIR/$LOG
	echo -n "$netscape date=\"$(date)\" args=\"$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
				url="http://$1"
			fi
			;;
	esac
	tracking nsremote start
	if [ $NSREMOTE ];then
		$nsremote$NSREMOTE -noraise -remote "openURL($(uri_encode $url))" \
			2>/dev/null  || netscape "$@"
		mv=$?
		if [ $retval -eq 0 ]; then
			retval=$mv
		fi
	elif [ "$nsremote" ];then
		$nsremote$NSREMOTE -noraise -remote "openURL($(uri_encode $url))" \
			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 ~/.netscape ];then
		install -d ~/.netscape
		if [ "$SET_DEFAULTS" = "yes" -a -d "/etc/netscape4/defaults" ];then
			cp /etc/netscape4/defaults/*  ~/.netscape 2>&1 > /dev/null
			chmod 600 ~/.netscape/*
		fi
		install -d ~/.netscape/archive ~/.netscape/cache
	fi
	tracking start
	[ $NPX_PLUGIN_PATH ] && NPX_PLUGIN_PATH=$NPX_PLUGIN_PATH:
	if [ -d /usr/local/lib/netscape/$VER/plugins ];then
		NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}/usr/local/lib/netscape/$VER/plugins:
	fi
	if [ -d /usr/local/lib/netscape/plugins ];then
		NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}/usr/local/lib/netscape/plugins:
	fi

	NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}:${HOME}/.netscape/plugins:\
/usr/lib/netscape/plugins:\
/usr/lib/netscape/$VER/plugins:\
/usr/lib/netscape/$VER/$BIN/plugins

	[ $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)
	export MALLOC_CHECK_ NPX_PLUGIN_PATH NPX_NETHELP_PATH display MOZILLA_HOME CLASSPATH
	if [ "$NS_FIX" = "yes" ];then
		eval LD_PRELOAD=/usr/lib/netscape/base-4/libnsfix.so $netscape "$@"
	else
		$netscape "$@"
	fi
	retval=$?
	tracking stop
}

#
# 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=$(printf "%1.1f" 1 2>/dev/null)

if [ "$pnt" != "1.0" ]; then
	#   echo "1.0 -> $pnt"
	# 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=$(printf "%1.1f" 1 2> /dev/null)
fi

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
fi


#
# Decide what to do...
#

retval=0
if [ "$NSREMOTE" ];then
	showurl "$@"
elif [ $# -gt 1 ]; then
	run_netscape "$@"
elif [ $# -eq 1 ]; then
	if [ "$(echo $1 | cut -c1)" != "-" ]; then
		showurl "$1"
	else
		run_netscape "$@"
	fi
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
else
	run_netscape
fi
exit $retval
