#! /bin/bash -e
###############################################################################
#
# Netscape Wrapper -- written by H. Peter Anvin <hpa@transmeta.com>
#
# (97.06.28-98.11.12)  Modified for Debian by Brian White <bcwhite@pobox.com>
#
###############################################################################


#
# Defaults
#
netscape=/usr/lib/netscape/netscape
nsremote=/usr/bin/X11/netscape
prefskel=/usr/lib/netscape/.netscape
prefetc=/etc/skel/.netscape
prefdir=$HOME/.netscape
pref=$prefdir/preferences.js
oldpref=$prefdir/preferences
pwd=`pwd`


#
# Try calling existing netscape with URL, if fail then 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=$1
		fi ;;
    esac
            
    if echo $DISPLAY | egrep -q "^:"; then
	$nsremote -noraise -remote "openURL($url, new_window)" 2>/dev/null \
	    || exec $netscape "$url"
    else
	    exec $netscape "$url"
    fi
}


#
# Don't allow running netscape as root
#
if [ $UID -eq 0 -o $EUID -eq 0 ]; then
    echo "$0: Cannot be run an root (for security reasons)"
    exit 1
fi


#
# Set some env vars to make things work better
#
#LD_LIBRARY_PATH=/usr/lib/netscape:$LD_LIBRARY_PATH
MOZILLA_HOME=/usr/lib/netscape
NPX_PLUGIN_PATH=$HOME/.netscape/plugins:/usr/local/lib/netscape/plugins:/usr/lib/netscape/plugins
display=$DISPLAY
export MOZILLA_HOME NPX_PLUGIN_PATH display 


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

pnt=`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=`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=`printf "%1.1f" 1 2> /dev/null`

	if [ "$pnt" != "1.0"]; then
	    # running out of ideas...
	    unset LANG
	    export LANG
	    pnt=`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


#
# Make user copy of default files unless they already exist
#
if [ -r $oldpref -a ! -r $pref ]; then
    useold=true
else
    useold=false
fi
if [ -d $prefskel ]; then
    if [ ! -d $prefdir ]; then
	mkdir -p $prefdir
    fi
    cd $prefskel
    for i in *; do
	if [ ! -e $prefdir/$i ]; then
	    cp $i $prefdir/$i;
	fi
    done
fi
if [ -d $prefetc ]; then
    if [ ! -d $prefdir ]; then
	mkdir -p $prefdir
    fi
    cd $prefetc
    for i in *; do
	if [ ! -e $prefdir/$i ]; then
	    cp $i $prefdir/$i;
	fi
    done
fi
if $useold; then
    rm -f $pref
    echo "Removing $pref so it can be generated from $oldpref"
fi
cd $pwd


#
# Decide what to do...
#
if [ $# -gt 1 ]; then
    exec $netscape "$@"
elif [ $# -eq 1 ]; then
    if [ `echo $1 | cut -c1` != '-' ]; then
	showurl "$1"
    else
	exec $netscape "$@"
    fi
elif [ -f $pref ]; then
    homepage=`egrep '^user_pref.*"browser.startup.homepage"' $pref | sed -e 's/^.*".*".*"\(.*\)".*$/\1/'`
    strtpage=`egrep '^user_pref.*"browser.startup.page"' $pref | sed -e 's/^.*".*", *\([0-9]*\).*$/\1/'`
    if [ -n "$strtpage" ]; then
	if [ "$strtpage" -ne 1 ]; then
	    homepage=""
	fi
    fi
    if [ -n "$homepage" ]; then
	showurl "$homepage"
    else
	exec $netscape
    fi
else
    exec $netscape
fi
