# (c) Copyright	Empress Software Inc.	1983, 2000

. $INSTUTIL_DIR/echon.sh
# . $INSTUTIL_DIR/shrc.sh
# . $INSTUTIL_DIR/pkg.sh
. $INSTUTIL_DIR/msg.sh
. $INSTUTIL_DIR/utils.sh
# . $INSTUTIL_DIR/licutil.sh
# . $INSTUTIL_DIR/instpath.sh
# . $INSTUTIL_DIR/usrinput.sh

# clean up temporary files from previous installation if any.
rm -fr $TMPDIR/empinstl_g.* 2> /dev/null

EMPRESSPATH=$TMPDIR/empinstl_g.$$
export EMPRESSPATH

mkdir $EMPRESSPATH

if test ! -d $EMPRESSPATH
then
	echo "$MSG_CANNOT_CREAT_DIR '$EMPRESSPATH'"
	exit 1
fi

. $INSTUTIL_DIR/pkgvar.sh

. $INSTUTIL_DIR/inst_c.sh

clear_screen

echo_message "$MSG_INSTALL_BANNER2"
echo_message "$MSG_CHECK_XWIN"

no_xwin ()
{
	if test "$DISPLAY" = ""
	then
		echo_message "$MSG_DISPLAY_VAR_NOT_SET"
		return 0
	fi

	echo_message "$MSG_DISPLAY_VAR_SET_TO" $DISPLAY
	
	. $INSTUTIL_DIR/lang.sh

	case $INSTALL_LANG in
	  japanese)
		case "$MSSYS_PLATFORM" in
		   SYS_OS_UNIX_LINUX_x86*)
			echo_message "$MSG_SEARCH_FOR" kterm

			PATH="$PATH:/usr/bin/X11:/usr/X11/bin:/usr/openwin/bin"
			XTERM=`where_is kterm`

			if test "$XTERM" = ""
			then
				echo_message "$MSG_INFO_NOT_FOUND"

				# gnome-terminal is not used because
				# it does not inherit any environment variable.
				# gnome-terminal is not spawned as a child
				# process of this shell script.
				echo_message "$MSG_SEARCH_FOR" konsole
				XTERM=`where_is konsole`

				if test "$XTERM" = ""
				then
					echo_message "$MSG_INFO_NOT_FOUND"
					return 0
				else
					echo_message "$MSG_INFO_FOUND"
				fi
			else
				echo_message "$MSG_INFO_FOUND"
			fi

			;;
		   SYS_OS_UNIX_DECUNIX | SYS_OS_UNIX_SOLARIS*)
			echo_message "$MSG_SEARCH_FOR" dtterm

			PATH="$PATH:/usr/bin/X11:/usr/X11/bin:/usr/dt/bin"
			XTERM=`where_is dtterm`
			if test "$XTERM" = ""
			then
				echo_message "$MSG_INFO_NOT_FOUND"
			else
				echo_message "$MSG_INFO_FOUND"
			fi
			;;
		esac
		;;
	  zh_CN)
		case "$MSSYS_PLATFORM" in
		   SYS_OS_UNIX_DECUNIX | SYS_OS_UNIX_SOLARIS*)
			echo_message "$MSG_SEARCH_FOR" dtterm

			PATH="$PATH:/usr/bin/X11:/usr/X11/bin:/usr/dt/bin"
			XTERM=`where_is dtterm`
			if test "$XTERM" = ""
			then
				echo_message "$MSG_INFO_NOT_FOUND"
			else
				echo_message "$MSG_INFO_FOUND"
			fi
			;;
		esac
		;;
	esac

	if test "$XTERM" = ""
	then
		echo_message "$MSG_SEARCH_FOR" xterm

		PATH="$PATH:/usr/bin/X11:/usr/X11/bin:/usr/openwin/bin"
		XTERM=`where_is xterm`
		if test "$XTERM" = ""
		then
			echo_message "$MSG_INFO_NOT_FOUND"
			return 0
		else
			echo_message "$MSG_INFO_FOUND"
		fi
	fi

	PROG=xrdb
	echo_message "$MSG_SEARCH_FOR" $PROG
	
	XPROG=`where_is $PROG`

	if test "$XPROG" = ""
	then
		echo_message "$MSG_INFO_NOT_FOUND"
		return 0
	else
		echo_message "$MSG_INFO_FOUND"
	fi
	
	XPROG="$XPROG -q"

	echo_message "$MSG_CHECK_OPEN_XWIN"

	$XPROG > /dev/null 2>&1
	STAT=$?
	case $STAT in
		0)
			;;
		*)
			echo_message "$MSG_INFO_FAILED"
			return 0
			;;
	esac

	echo_message "$MSG_INFO_OKAY"

	XHOST=`where_is xhost`

	if test "$XHOST" != ""
	then
		# Caldera has a funny setup that if xhost is not set
		# and $HOME does not match the one in /etc/passwd,
		# it refuses to bring up any X application.
		$XHOST +localhost > /dev/null 2>&1
	fi

	return 1
}

BACKUPDIR=$HOME/.netscape.empress.`date "+%y-%m-%d-%T"`

backup_netscape ()
{
	if test ! -d $HOME/.netscape && \
		test ! -f $HOME/.mime.types && \
		test ! -f $HOME/.mailcap
	then
		return
	fi

	test -d $BACKUPDIR && rm -fr $BACKUPDIR

	echo_message "$MSG_INFO_BACKUP_NETSCAPE"
	mkdir $BACKUPDIR

	test -d $HOME/.netscape && mv $HOME/.netscape $BACKUPDIR
	test -f $HOME/.mime.types && mv $HOME/.mime.types $BACKUPDIR
	test -f $HOME/.mailcap && mv $HOME/.mailcap $BACKUPDIR
	echo_message "$MSG_INFO_DONE"
}

restore_netscape ()
{
	test -d $BACKUPDIR || return

	echo_message "$MSG_INFO_RESTORE_NETSCAPE"
	rm -fr $HOME/.netscape $HOME/.mime.types $HOME/.mailcap

	test -d $BACKUPDIR/.netscape && mv $BACKUPDIR/.netscape $HOME
	test -f $BACKUPDIR/.mime.types && mv $BACKUPDIR/.mime.types $HOME
	test -f $BACKUPDIR/.mailcap && mv $BACKUPDIR/.mailcap $HOME
	rm -fr $BACKUPDIR
	echo_message "$MSG_INFO_DONE"
}

if test -f $PKG_NETSCAPE_INFO
then
	. $PKG_NETSCAPE_INFO
	FL=$CDROM_DIR/$NETSCAPE_FILE
fi

if test ! -f $PKG_NETSCAPE_INFO || test ! -f $FL || no_xwin
then
	cd
	installation_cleanup
	X=sh
	test "$QNXSHELL" = "" || X=$QNXSHELL
	exec $X $INSTUTIL_DIR/inst_t.sh
	exit 0		# never come here
fi

echo_message "$MSG_INFO_INSTALL_TEMP_NETSCAPE"
sh $INSTUTIL_DIR/ns-inst.sh $FL $EMPRESSPATH/netscape | $DOTS 4
echo_message "$MSG_INFO_DONE"
NETSCAPE="sh $INSTUTIL_DIR/netscape.sh"

backup_netscape

# if file exists, some process is installing Empress
INSTALL_LOCK=$EMPRESSPATH/install_lock
INSTALL_EMPRESSPATH=$EMPRESSPATH/emppath

export XTERM INSTALL_LOCK INSTALL_EMPRESSPATH

backup_netscape

mkdir -p $HOME/.netscape/archive
mkdir -p $HOME/.netscape/cache

myuid=`$EMPRESSPATH/empinstl/instbin/chkuser -uid`

cat > $HOME/.netscape/preferences.js <<EOM
// Netscape User Preferences
// This is a generated file!  Do not edit.

user_pref("browser.startup.license_accepted", "$myuid $NETSCAPE_VERSION");
 user_pref("custtoolbar.Browser.Location_Toolbar.showing", false);
// user_pref("custtoolbar.Browser.Navigation_Toolbar.showing", false);
user_pref("custtoolbar.Browser.Personal_Toolbar.showing", false);
user_pref("browser.chrome.toolbar_style", 0);
user_pref("browser.startup.homepage", "http://www.empress.com");
EOM

cp $CDROM_DIR/instdocs/install/go $EMPRESSPATH
cp $CDROM_DIR/instdocs/install/runprog $EMPRESSPATH
chmod +x $EMPRESSPATH/go $EMPRESSPATH/runprog

mailcap="$HOME/.mailcap"
mimetypes="$HOME/.mime.types"

empmime="application/x-empress-installation"
empmcap=$empmime";$EMPRESSPATH/go"

cat > $mimetypes <<EOM
#--Netscape Communications Corporation MIME Information
#Do not delete the above line. It is used to identify the file type.
#mime type entry added by Empress
type=application/x-empress-installation \
desc="Empress-Installation"  \
exts="emp"

type=application/x-empress-runprog \
desc="Empress-Installation-runprog" \
exts="run"
EOM

cat >> $mailcap <<EOM
#mailcap entry added by Empress
application/x-empress-installation;$EMPRESSPATH/go
application/x-empress-runprog;$EMPRESSPATH/runprog
EOM

echo_message "$MSG_INFO_RUN_NETSCAPE"

cd $HOME

PATH="$PATH:/opt/SUNWspro/bin:/usr/ccs/bin"
export PATH

#
# For Unixware, the value of SHELL determines which shell system() will
# use. We need to set it to /bin/sh. Otherwise, when the user 'run'
# install, it will pop up dialog with the message like this:
# 	[1] 1234
#
SHELL=/bin/sh
export SHELL

MS_INSTALL=netscape
export MS_INSTALL

# just in case, netscape picks up old Netscape app-defaults setting file.
test "$XFILESEARCHPATH" = "" || unset XFILESEARCHPATH
test "$XUSERFILESEARCHPATH" = "" || unset XUSERFILESEARCHPATH
XAPPLRESDIR=$EMPRESSPATH/netscape
MOZILLA_HOME=$EMPRESSPATH/netscape
export XAPPLRESDIR MOZILLA_HOME

$NETSCAPE file:$CDROM_DIR/instdocs/install/frame.htm 

restore_netscape

cd
installation_cleanup
exit 0
