#!/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
environ=/etc/netscape4/environment
args="$@"

# defaults

usercf=~/.netscape/config
args="$@"


function 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
dir="$(dirname $last)"
BIN=$(basename $last)
FLA=$(echo $BIN | cut -f 2 -d -)
BIN=$(echo $BIN | cut -f 1 -d -)
VER=$(basename $(dirname $dir))

[ -e "$config" ] && . $config
[ "$ALLOW_USERCONFIG" = "yes" -a  -e ~/.netscape/config ] && . $usercf

# 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'

#
# 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;;
		*)		args="$args $1";let argc=$argc+1;;
	esac
	shift
done
set -- $args

if [ "$ALLOW_BINOVERRIDE" = "yes" ];then
	base=$(basename $0 | sed -e 's/-/_/g')
	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
		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
	if [ "$1" != "--allow-root" ];then
		if [ $UID -eq 0 -o $EUID -eq 0 ]; then
			echo "$0: Cannot be run an root (for security reasons)"
			exit 1
		fi
	fi
fi

function 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" ;;
		*)	url=`echo "file:$pwd/$1" | sed -e 's:///*:/:g'` ;;
	esac
	tracking nsremote start
	$nsremote -noraise -remote "openURL($url, new_window)" 2>/dev/null \
		|| run_netscape "$url"
}

function 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:
	NPX_PLUGIN_PATH=${NPX_PLUGIN_PATH}~/.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}~/.netscape/nethelp:\
/usr/lib/netscape/nethelp:\
/usr/lib/netscape/$VER/nethelp:\
/usr/lib/netscape/$VER/$BIN/nethelp

	[ $CLASSPATH ] && CLASSPATH=$CLASSPATH:
	CLASSPATH=${CLASSPATH}~/.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 [ "$NO_LD_PRELOAD" = "yes" ];then
		$netscape "$@"
	else
		LD_PRELOAD=/lib/libBrokenLocale.so.1 $netscape "$@"
	fi
	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...
#

if [ $# -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/");$//')

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