#!/bin/sh

netscape=""

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

prefix=$(dirname $(dirname $0))

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

export TZ=:/etc/localtime

#Exterminate zombie netscapes, and wipe stale lock file.
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 () {
	[ $# -gt 0 ] && url=`echo $@ | sed 's/\ -[^ ]*//g; s/\( |	\)*//'`
	if [ -L "$HOME/.netscape/lock" ]; then
		if [ "$url" = "" ]; then
			xalfoff
			$netscape $defsrem "xfeDoCommand($b_opt)" "$@" 2>/dev/null || \
			{ killstale && exec $netscape $defs $cl_opt "$@" ; }
		else
			xalfoff
			$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; xalfoff; $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 () {
          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 "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

# set up home page
HOMEPAGE=/usr/doc/HTML/index.html
if [ -f $pref ]; then
    if grep "\"browser.startup.homepage\"" \
    $pref > /dev/null; then
        HOMEPAGE=""
    fi
fi

# main

case "$1" in
	+*)	newopt "$@"; exit	;;
	-h|-help|--help) usage; exit	;;
	-*)	oldopt "$@"; exit	;;
	*)	[ -z "$*"  -a -n "$HOMEPAGE" ] && newbrowser "$HOMEPAGE" || newbrowser "$@"; exit	;;
esac	

echo "An error occurred running $netscape."





