#!/sbin/sh
#
#ident  "@(#)startinstall.sh 1.2     01/09/03 SMI"
#
# Copyright (c) 2001 Sun Microsystems, Inc.  All Rights Reserved.
#
# This script starts the install process.  It either starts it
# in the same console as being run in, or starts up a windowing
# system if necessary.

SHELL=/sbin/sh
TEXTDOMAIN=SUNW_INSTALL_SCRIPTS
PATH=/sbin:/usr/sbin/install.d:${PATH}

PLATFORM=`/sbin/uname -p`

# make home dir to a writeable place
HOME=/tmp/root
export SHELL TEXTDOMAIN PATH PLATFORM HOME

RUN_WIN=$1;export RUN_WIN

#
# Allow run of window system if at least 32 Meg of free memory is available 
#
MEMSIZE=`/sbin/mem`
echo "startup available memory: ${MEMSIZE}" \
	>> /tmp/root/var/sadm/system/logs/sysidtool.log
if [ "${MEMSIZE}" -lt 32768 ]; then 
	if [ ! -f /tmp/.nowin ]; then
	        touch /tmp/.nowin 
		echo "startup: insufficient memory for window system" \
			>> /tmp/root/var/sadm/system/logs/sysidtool.log
		echo "Warning: Insufficient memory to start window system."
	fi
fi

if [ "${RUN_WIN}" -eq 0 -o -f /tmp/.nowin ]; then
  	if [ ! -f /tmp/.nowin ];then
	  	touch /tmp/.nowin
	fi

	. /sbin/sysconfig
else
	# if the hostname is not yet configured we need a dummy entry
	# in /etc/hosts so the window system will work
	#
	name=`uname -n`
	if [ -z "${name}" ]; then
		uname -S localhost
	fi

	# Set up the customized install user's account directory (/tmp/root);
	# copy the menu file to a writeable area in case localization
	# updates are required by sysconfig; copy the customized openwin-init,
	# Xdefaults, openwin-menu, and Xinitrc files to ${HOME}
	#
        cp -p /usr/lib/locale/C/LC_MESSAGES/install-openwin-menu.gui \
                ${HOME}/.openwin-menu 2>/dev/console
        cp -p /usr/sbin/install.d/openwin-init \
                ${HOME}/.openwin-init 2>/dev/console
        chmod 755 ${HOME}/.openwin-init
	cp -p /usr/sbin/install.d/Xdefaults \
		${HOME}/.Xdefaults 2>/dev/console
	cp -p /usr/sbin/install.d/Xinitrc \
		${HOME}/.xinitrc 2>/dev/console
	chmod 755 ${HOME}/.xinitrc

        echo "Starting OpenWindows..."

	# Start the window system without authentication
	#
        /usr/openwin/bin/openwin -noauth

	# If openwin fails for some reason, start sysconfig without the window
	# system as if the user had booted with the -nowin option
	#
	if [ $? -ne 0 ]; then
		touch /tmp/.nowin
		. /sbin/sysconfig
	fi
fi
