#!/sbin/sh
#
#ident	"@(#)startup.sh	1.90	03/06/11 SMI"
#
#
# ident	"@(#)startup.sh	1.90	03/06/11 SMI"
#
# Copyright 1992-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Sun considers its source code as an unpublished, proprietary trade
# secret, and it is available only under strict license provisions.
# This copyright notice is placed here only to protect Sun in the
# event the source is deemed a published work.  Dissassembly,
# decompilation, or other means of reducing the object code to human
# readable form is prohibited by the license agreement under which
# this code is provided to the user or company in possession of this
# copy.
#
# RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the Government
# is subject to restrictions as set forth in subparagraph (c)(1)(ii) of the
# Rights in Technical Data and Computer Software clause at DFARS 52.227-7013
# and in similar clauses in the FAR and NASA FAR Supplement.
#
# NOTE:	run as the runlevel 234 process; invoked from the inittab; invokes
#	sysconfig with all necessary environment setup
#

SHELL=/sbin/sh
TEXTDOMAIN=SUNW_INSTALL_SCRIPTS
PATH=/sbin:/usr/sbin/install.d:${PATH}
YES=yes
NO=no
PLATFORM=`/sbin/uname -p`

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

# Local Variables
KDMCONFIG_NOGUI=/tmp/root/var/tmp/kdmconfig.nogui
OWCONFIG_FILE=/tmp/root/etc/openwin/server/etc/OWconfig
RUN_WIN=1
RUN_KDM=0

# where to store locale variables
LOCFILE=/tmp/.localeFile

NETBOOTINFO=/usr/lib/inet/wanboot/netbootinfo

#
# Read the LANG and LC_MESSAGES from /etc/default/init and use it
#
locale_from_init()
{

	# where to source variables from
	SRCFILE=$1

	#
	# Source in all LANG, LC_*, and TZ settings and export them
	# Make sure LANG doesn't already have a value (it is initially C).
	#
	LANG=""
	. $SRCFILE
	export TZ
	export LANG LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME

	#
	# If LANG was set but LC_MESSAGES was not, explicitly set LC_* values
	#
	if [ "${LANG}" ] && [ ! "${LC_MESSAGES}" ]; then
		LC_COLLATE=${LANG}
		LC_CTYPE=${LANG}
		LC_MESSAGES=${LANG}
		LC_MONETARY=${LANG}
		LC_NUMERIC=${LANG}
		LC_TIME=${LANG}
	fi
}

# determines if we are on the locale console, or
# on a serial console (i.e. tip'd into ttya)
# if we are, we know what the terminal type should
# be.
if [ -x /sbin/getconsole ] ; then
    CONSOLE_TERM=`/sbin/getconsole`
    if [ $? = 0 ] ; then
	ON_CONSOLE=$YES
	TERM=$CONSOLE_TERM;export TERM
    else 
	ON_CONSOLE=$NO
    fi
else
    # assume we aren't on the console,
    # so termtype questions get asked if needed.
    # 
    ON_CONSOLE=$NO
fi

#
# Main shell starts here
#

# Export net boot configuration strategy. _INIT_NET_IF is set to the
# interface name of the netbooted interface if this is a net boot.
# _INIT_NET_STRATEGY is set to the network configuration strategy.
# First use netbootinfo and if that fails use the legacy netstrategy
_INIT_NET_STRATEGY=`${NETBOOTINFO} net-config-strategy 2>/dev/null`
if [ $? -eq 0 -a ${_INIT_NET_STRATEGY}X != "noneX" ]; then
	Roottype=`${NETBOOTINFO} rootfs-type`
	if [ $? -ne 0 ]; then
		Roottype=""
	fi

	_INIT_NET_IF=`${NETBOOTINFO} interface-name`
	if [ $? -ne 0 ]; then
		_INIT_NET_IF=""
	fi
	if [ "X${_INIT_NET_IF}" != "X" ] ; then
	    # we must have net booted
	    Network=yes
	fi
else
	_INIT_NET_STRATEGY=""
        set -- `/sbin/netstrategy`
        if [ $? -eq 0 ]; then
                if [ "$1" = "nfs" -o "$1" = "cachefs" ]; then
                        _INIT_NET_IF="$2"
                fi
                _INIT_NET_STRATEGY="$3"
                export _INIT_NET_IF _INIT_NET_STRATEGY 
        fi
fi

export _INIT_NET_IF _INIT_NET_STRATEGY


cd ${HOME}

#
# If the run level is changed after invocation, shell should
# be run
#
if [ -f /tmp/.sh ]; then
	exec ${SHELL}
else
	touch /tmp/.sh
fi

##########
# Make sure all configuration necessary is completed in order
# to run the window system

# first try and find the sysidcfg file
echo "Searching for configuration file(s)..."
/sbin/sysidfind
echo "Search complete."

#
# Configure the frame buffer links
#
fbdev=`ls /devices\`/usr/sbin/prtconf -F 2>&1\`* 2>/dev/null`

if [ $? -ne 0 ]; then
	RUN_WIN=0
else
	set `echo ${fbdev}`
	rm -f /dev/fb
	ln -s $1 /dev/fb

	# XXX temporary
	# NOTE: Why is this temporary and what is its purpose?
	#
	cd /dev/fbs
	for nm in *
	do
		if [ ! -h /dev/$nm ]; then
			ln -s /dev/fbs/$nm /dev/$nm
		fi
	done


	for i in /sbin/install.d/fbinit/*; do
		if [ -x "$i" ] ; then
			"$i" "$fbdev"
		fi
	done


	echo ${fbdev} | grep "PFU\,aga" >/dev/null 2>&1
	if [ $? -eq 0 ]; then
		if [ -x /etc/rc2.d/S91agaconfig ]; then
			/etc/rc2.d/S91agaconfig start
		fi
	fi

	cd ${HOME}

	case "${PLATFORM}" in
	ppc|i386)
		RUN_KDM=1
		;;
	esac
fi

# asks user for locale to run in, if we haven't already.
if [ ! -f /.localeAsked ] ; then
    /sbin/getLocale $LOCFILE
else
    # locale already asked.  Locale settings can be found in /etc/default/init.
    LOCFILE=/etc/default/init
fi

#
# If they just picked a multibyte locale, we don't want to start using
# it until the window system has started.
#
LOC=`grep "^LC_MESSAGES=" $LOCFILE 2>/dev/null \
			| sed -e '/^#/d' -e 's/.*=//'`
if [ -z "${LOC}" ] ; then
	LOC=`grep "^LANG=" $LOCFILE 2>/dev/null \
			| sed -e '/^#/d' -e 's/.*=//'`
fi

CTYPE=`grep LC_CTYPE /etc/default/init 2>/dev/null \
			| sed -e '/^#/d' -e 's/.*=//'`
if [ -z "${CTYPE}" ] ; then
	CTYPE=`grep LANG /etc/default/init 2>/dev/null \
			| sed -e '/^#/d' -e 's/.*=//'`
fi

MBLOC=`(cd /usr/lib/locale; ls */LC_MESSAGES/openwin-defaultfont 2> /dev/null |
           sed -e 's:^\([^/]*\)/.*$:\1:') | nawk -v "LOC=$LOC" '
		{
			if (substr(LOC, 1, length($0)) == $1) { 
				if (length(foundloc) < length($0)) { 
					foundloc=$0; 
				} 
			} 
		} 

		END {print foundloc;}'`

if [ -n "${MBLOC}" -a "$ON_CONSOLE" = "$YES" ] ; then
	# They picked a multibyte locale.  Save it, but stay in C for now.
	# Unfortunately, if we don't start the windowing system later, then
	# locale_from_init will never get run, and the locale will stay
	# as "C".  This will be a problem if, for example, we run pfinstall
	# without a windowing system.  In this case, pfinstall would not
	# know to select the correct locale software, since the locale would
	# be "C".  So, we set LC_CTYPE here to signal applications that run
	# outside a windowing system what the locale is, without trying
	# to translate any messages, so they can take appropriate steps.
	LC_CTYPE=${CTYPE}; export LC_CTYPE
	echo "${MBLOC}" >/tmp/.mb_locale
else
	# They picked a single-byte locale, or they picked a multibyte locale
	# they're not using a local console.  Enable the chosen locale.
	locale_from_init $LOCFILE
fi

#
# non-SPARC systems require the keyboard/mouse/display hardware be configured
# for the sake of creating the OWconfig file.
# To do this, we try to set the correct locale (the Intel and PPC console
# display's are 8-bit clean so this may work.
# Run kdmconfig to configure the OWconfig file.
#
# In 2.6, the existence of the OWconfig file is not enough to
# guarantee that we want to try and bring up a GUI environment.
# kdmconfig may write out an OWconfig file with values of "Unknown"
# for some of the attributes.  So, the OWconfig file check has been
# enhanced with a check for the KDMCONFIG_NOGUI file in the case that
# the OWconfig exists.
# sysidconfig runs kdmconfig, and kdmconfig will create this file
# in the case that the GUI environment should not be set up.
#
if [ "${RUN_KDM}" -eq 1 ]; then

	#
	# This routine calls kdmconfig, which creates OWconfig
	# and KDMCONFIG_NOGUI.
	#
	/usr/sbin/sysidconfig

	# kdmconfig will destroy symlinks if it is run on a
	# writable miniroot (think dvd).  re-create it here
	# if that happens.
	if [ ! -h /etc/openwin/server/etc/OWconfig ];then
	    if [ -f ${OWCONFIG_FILE} ];then
		cd /etc/openwin/server/etc
		#kdmconfig killed the symlink
		mv OWconfig $OWCONFIG_FILE
		ln -s  ../../../../tmp/root/etc/openwin/server/etc/OWconfig .
		cd /tmp/root
		### Bugid's 4270374 and 4270445, 
		### when 4270445 is fixed this can be deleted.
		DEPTH=`grep defdepth ${OWCONFIG_FILE} | sed -e 's:defdepth=::g' -e 's:\"::g'`
		if [ $DEPTH -lt 8 ];then
		    RUN_WIN=0
		fi
	    fi
	fi

	#
	# Now decide, based on which files kdmconfig created
	# whether to bring up the GUI or CUI.
	#
	if [ ! -f ${OWCONFIG_FILE} ]; then
		RUN_WIN=0
	elif [ -f ${KDMCONFIG_NOGUI} ]; then
		RUN_WIN=0
	fi
fi

#
# Remove un-needed /dev symlinks, since each one uses a page in tmpfs.
#
rm -f /tmp/dev/pts/4? /tmp/dev/pts/3? /tmp/dev/pts/2? /tmp/dev/pts/1?
rm -f /tmp/dev/win1?? /tmp/dev/win[1-9]? /tmp/dev/win[5-9]
rm -f /tmp/dev/ptyp[5-9] /tmp/dev/ptyp[a-f]
rm -f /tmp/dev/ttyp[5-9] /tmp/dev/ttyp[a-f]
rm -f /tmp/dev/ptyq* /tmp/dev/ptyr* /tmp/dev/ttyq* /tmp/dev/ttyr*


# Start up the window system unless the display device cannot be
# determined or the install process with explicitly booted with
# the "-nowin" option.
#
if [ -f /tmp/.nowin ] ; then
    RUN_WIN=0
fi

#
# If in a multi-byte locale and we're going to use a window system,
# run locale_from_init now just prior 
# to starting the window system. It was not run earlier since 
# local consoles do not support multi-byte locales.
#
if [ -f /tmp/.mb_locale -a "${RUN_WIN}" -eq 1 ]; then
	cp -p \
/usr/lib/locale/`cat /tmp/.mb_locale`/LC_MESSAGES/openwin-defaultfont \
	${HOME}/.openwin-defaultfont 2>/dev/console
	chmod 755 ${HOME}/.openwin-defaultfont

	locale_from_init $LOCFILE
fi
exec startinstall ${RUN_WIN}
