#!/bin/sh

netscape=""

prefdir=$HOME/.netscape
prefcpck=$prefdir/nswrapper.copy_defs
pref=$prefdir/preferences.js
defs=""
defsrem="-noraise -remote"
b_opt="openBrowser"

MALLOC_CHECK_=4
export MALLOC_CHECK_

prefix=$(dirname $(dirname $0))
sysname=`uname -m | sed "s|sparc64|sparc|" | sed "s|^i[0-9]|i3|"`

if echo $0 | grep 'navigator' >/dev/null; then
    netscape=$prefix/lib/netscape/netscape-navigator
elif echo $0 | grep 'communicator' >/dev/null ; then
    netscape=$prefix/lib/netscape/netscape-communicator
elif [ -x $prefix/lib/netscape/netscape-communicator ]; then
    netscape=$prefix/lib/netscape/netscape-communicator
elif [ -x $prefix/lib/netscape/netscape-navigator ]; then
    netscape=$prefix/lib/netscape/netscape-navigator
else
    echo "You don't seem to have netscape installed."
    exit 1
fi

# Modified for Linux-Mandrake distribution (Netscape 4.70)
export TZ=:/etc/localtime


# Exterminate zombie netscapes, and wipe stale lock file.
# Modified for Linux-Mandrake distribution (Netscape 4.70)
killstale () {

	user="`whoami`"
	ourpids="`/sbin/pidof $netscape`"
	[ "$ourpids" != "" ] && ourpids=`ps u $ourpids | sed -n "s/^$user[ ]*\([^ ]*\)[ ].*/\1/p"`
	
	for p in $ourpids; do
		[ "$p" = "" ] && continue
		# int, quit, term, kill
		{ kill -2 $p && kill -3 $p && kill -15 $p && sleep 2 && kill -9 $p ; } >/dev/null 2>&1
	done

	[ -L "$HOME/.netscape/lock" ] && rm "$HOME/.netscape/lock"
	
	return 0
}

# Try calling existing netscape process with functions, else start one.
newbrowser () {
#	if [ -n "$LD_PRELOAD" ]; then
#		LD_PRELOAD=$prefix/lib/netscape/libnspipepatch.so:$LD_PRELOAD
#	else
#		LD_PRELOAD=$prefix/lib/netscape/libnspipepatch.so
#	fi
#	export LD_PRELOAD

# David BAUDENS <baudens@mandrakesoft.com> - 20000812
# Fix crash when user insert an empty link in Composer
# Works with Netscape 4.74
MOZILLA_LINUX_INSERT_LINK_FIX=1
export MOZILLA_LINUX_INSERT_LINK_FIX 


	[ $# -gt 0 ] && url=`echo $@ | sed 's/\-[^ ]*//g; s/\( |	\)*//'`
	if [ -L "$HOME/.netscape/lock" ]; then
		if [ "$url" = "" ]; then
			$netscape $defsrem "xfeDoCommand($b_opt)" "$@" 2>/dev/null || \
			{ killstale && exec $netscape $defs $cl_opt "$@" ; }
		else
			$netscape $defsrem "xfeDoCommand(openBrowser)" 2>/dev/null && \
				$netscape $defsrem "openURL($url)"  2>/dev/null || \
			{ killstale && exec $netscape $defs "$@" ; }
		fi
	else
		if [ "$url" = "" ]; then
			[ ${rerun:=0} -eq 1 ] && { sleep 3; $netscape $defsrem "xfeDoCommand($b_opt)" "$@" 2>/dev/null ; } &
			    exec $netscape $defs $cl_opt "$@"
		else
			    exec $netscape $defs "$@"
		fi	
	fi
}

newopt () {
opt="$1"
shift
	case "$opt" in
		"+news"|"+discussions") b_opt="openNewsgroups"	; cl_opt="-discussions" ;;
		"+mail"|"+messenger")	b_opt="openInbox"	; cl_opt="-messenger"	;;
		"+edit"|"+composer")	b_opt="openEditor"	; cl_opt="-composer"	;;
		"+addr"|"+addresses")	b_opt="openAddrBook"	; cl_opt="-messenger -iconic"	; rerun=1	;;
		"+hist"|"+history")	b_opt="openHistory"	; cl_opt="-iconic"		; rerun=1	;;
		"+book"|"+bookmarks")	b_opt="openBookmarks"	; cl_opt="-iconic"		; rerun=1	;;
		"+mailto")		b_opt="composeMessage"	; cl_opt="-messenger -iconic"	; rerun=1	;;
		"+component-bar")	b_opt="toggleTaskbar"	; cl_opt="-component-bar";;
	esac
	newbrowser "$@"
}

oldopt () {
#    if [ -n "$LD_PRELOAD" ]; then
#	LD_PRELOAD=$prefix/lib/netscape/libnspipepatch.so:$LD_PRELOAD
#   else
#	LD_PRELOAD=$prefix/lib/netscape/libnspipepatch.so
#    fi
#    export LD_PRELOAD
    exec $netscape $defs "$@"
}

usage () {
	echo ""
	echo "Netscape Wrapper $ver, $ts"
	echo "        (c) 1998,1999 Dave Cinege, H. Peter Anvin, and Red Hat, Inc."
	echo ""
	echo "		   Modified for Linux-Mandrake by David BAUDENS <baudens@mandrakesoft.com> - 1999, 2000"
	echo ""
	echo "usage: netscape [ option ]"
	echo "  options:"
	echo ""
	echo "    [none]                Open new browser"
	echo "    [URL]                 Open new browser with URL"	 
	echo "    +news | +discussions  Show Collabra Discussions"
	echo "    +mail | +messenger    Show Messenger Mailbox (INBOX)"
	echo "    +edit | +composer     Open Composer"		
	echo "    +addr | +addresses    Show Address Book"
	echo "    +hist | +history      Open History window"
	echo "    +book | +bookmarks    Open Bookmarks windowsBook"
	echo "    +mailto               Open New Message window" 
	echo "    +component-bar        Show (detach) Component Lauch Bar"
	echo "    -[option]             Passed directly to Netscape binary"
	echo ""
	echo "The wrapper first attempts to open the option in a running"
	echo "netscape. If no netscape is running, one is executed with"
	echo "the option as the starting window."
	echo ""
	echo "Edit the top of this file to set default paths."
	echo ""
	echo ""
	echo "Netscape binary help:"
	echo ""
	exec $netscape "-help"
}

# 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
	export LC_NUMERIC=C		# Try a "safe" value for LC_NUMERIC.

	pnt=`printf "%1.1f" 1 2>/dev/null`
	if [ "$pnt" != "1.0" ]; then	#  LC_ALL is bad.
	 	LC_COLLATE=$LC_ALL	#  Set $LC_ALL for every category except
		LC_CTYPE=$LC_ALL	#  LC_NUMERIC, and then unset 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
fi


# Make user copy of default files unless they already exist

if [ ! -f $prefcpck ]; then
	[ ! -d $prefdir ] && mkdir -p $prefdir
	pwd=`pwd`
	
	for pdir in $prefskel; do
		[ ! -d $pdir ] && continue
		cd $pdir
		for i in *; do
			[ ! -f $prefdir/$i ] && cp $i $prefdir/$i
   		done
	done
	
	touch $prefcpck	#safer, slower then this?   #echo -n "" >>$prefcpck 
    	cd $pwd

fi

# David BAUDENS <baudens@mandrakesoft.com> - 20000812
# Creat cache directory if user heve delete it (and don't display
# error messages)
# Works with Netscape 4.74
if [ ! -d $prefdir/cache ] ; then
   mkdir -p $prefdir/cache
fi

# David BAUDENS <baudens@mandrakesoft.com> - 20000812
# Create archive directory if user heve delete it (and don't display
# error messages)
# Works with Netscape 4.74
if [ ! -d $prefdir/archive ] ; then
    mkdir -p $prefdir/archive
fi
	

# Set up home page
# Modified for Linux-Mandrake distribution (Netscape 4.70)
# New PATH: 4.75-6mdk
HOMEPAGE=/usr/share/doc/HTML/index.html
if [ -f $pref ]; then
    if grep "browser.startup.homepage\"" \
    $pref > /dev/null; then
        HOMEPAGE=""
    fi
fi


### David BAUDENS <baudens@mandrakesoft.com>
## Use True Type font if available 
## Works with Netscape 4.72
if [ ! -f $HOME/.netscape/preferences.js ] ; then
	cp /usr/lib/netscape/preferences.js $HOME/.netscape && perl -p -i -e "s|maison|$HOME|" $HOME/.netscape/preferences.js
fi

if [ ! -e $HOME/.netscape/.ttf_done ] ; then
	if [ -r /usr/X11R6/lib/X11/fonts/drakfont/verdanab.ttf ] || [ -L /usr/X11R6/lib/X11/fonts/drakfont/verdanab.ttf ] ; then
		perl -p -i -e "s|user_pref\(\"intl.font_spec_list.*|user_pref(\"intl.font_spec_list\", \"microsoft-verdana-120-noscale-prop-iso-8859-1,\");|" $HOME/.netscape/preferences.js && echo "# This file is use to know if default font should be modified. You can edit it and add what you want but don't delete it" > $HOME/.netscape/.ttf_done
	else perl -p -i -e "s|user_pref\(\"intl.font_spec_list.*|user_pref(\"intl.font_spec_list\", \"mozilla-helvetica-70-noscale-prop-iso-8859-1,\");|" $HOME/.netscape/preferences.js && echo "# This file is use to know if default font should be modified. You can edit it and add what you want but don't delete it" > $HOME/.netscape/.ttf_done 
	fi
fi
### End nice fonts


### David BAUDENS <baudens@mandrakesoft.com>
## Fix Netscape crash when adress book is empty
## Works with Netscape 4.75
if [ ! -e $HOME/.netscape/pab.na2 ] ; then
	cp /usr/lib/netscape/pab.na2 $HOME/.netscape
fi

# main
# Modified for Linux-Mandrake distribution (Netscape 4.72)
if [ "$1" = "" ];then
newbrowser -session-management -no-about-splash $HOMEPAGE
else
case "$1" in
	+*)	newopt "$@"; exit	;;
	-h|-help|--help) usage; exit	;;
	-*)	oldopt "$@"; exit	;;
	*)	newbrowser "$@"; exit	;;
esac	
fi

echo "An error occurred running $netscape."
