#!/bin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)install-solaris.sh	1.8	06/06/13 SMI"
#

#
# This script invokes the solaris installation programs,
# either in a dtterm (when in GUI mode) or on the console.
# After a successful installation, it also sets up the launcher
# for post-install product installation.
#

. /sbin/install-common
if install_debug_isset scripts ; then
    set -x
fi

install_debug scripts "$0 begins `date`"

# Set the noinuse checking flag
NOINUSE_CHECK=1
export NOINUSE_CHECK

# Set the Sun private install flag
_LIBDISKMGT_INSTALL=1
export _LIBDISKMGT_INSTALL

# Set the text domain
TEXTDOMAIN=SUNW_INSTALL_SCRIPTS
export TEXTDOMAIN

WIZDIR=/usr/lib/install/data
CLASSPATH=${WIZDIR}/wizards/apps:${CLASSPATH}
export CLASSPATH

# Local Variables
#
LOCKFILE=/tmp/.install-solaris
JSLOCKFILE=/tmp/.jumpstart
LOGDIR=/a/var/sadm/system/logs
LAUNCHDIR=/a/var/sadm/launcher
RBLOGDIR=/var/sadm/system/logs
OLDLOGDIR=/a/var/sadm
RELLOGDIR=system/logs
INSTALLBOOT=/tmp/.install_boot
PREINSTALL=/tmp/.preinstall
CD_CONFIG_DIR=/cdrom/.bbi
SMI_INSTALL=/tmp/.smi_boot
SUPPRESS_SOLSTART_FINISH=/tmp/.suppress_solstart_finish
BEGIN=begin
FINISH=finish
BEGINLOG=begin.log
FINISHLOG=finish.log
TMP_INSTALL_APP=/tmp/install_app

MY_VERSION=2	# used to match version in JumpStart rules file
ESTATUS=0	# exit status

NETBOOTINFO=/usr/lib/inet/wanboot/netbootinfo
SYSTEM_CONF=/etc/netboot/system.conf

# Global Variables
#
SI_CONFIG_DIR=/tmp/install_config
SI_CONFIG_PROG=rules.ok
SI_CONFIG_FILE=${SI_CONFIG_DIR}/${SI_CONFIG_PROG}
SI_SYS_STATE=/a/etc/.sysIDtool.state
SI_INSTALL_APP=interactive
export SI_CONFIG_DIR SI_CONFIG_PROG SI_SYS_STATE SI_CONFIG_FILE SI_INSTALL_APP

# create a lock file if one does not already exist; this is to prevent
# the install-solaris script from being invoked more than once
#
si_single_lock() {
	if [ ! -f ${LOCKFILE} ]; then
		touch ${LOCKFILE}
	fi
}

# remove the install-solaris lock if it exists.
#
si_single_unlock() {
	rm -f ${LOCKFILE}
}

# remove the install-solaris lock and execute the /sbin/sh
# NOTE:	If the program being executed is not the shell, but is
#	instead one of the install applications should we really
#	be removing this lock at this point?
#
become_shell() {
	si_single_unlock
	exec /sbin/sh
}

# retrieve the begin, profile, and finish scripts if necessary.
retrieve_support_scripts() {
	PROFETCH="/usr/sbin/install.d/profetch"
	PROFETCH_ARGS=""

	if [ -f ${SYSTEM_CONF} ] ; then
		. /etc/netboot/system.conf
	else
	    SjumpsCF=`/sbin/dhcpinfo SjumpsCF`
	fi

	URL="${SjumpsCF}"
	if [ "${URL}X" = "X" ]; then
	    # no location, so just return and let interactive install begin
	    return
	fi

	PROXY=`${NETBOOTINFO} SHTTPproxy 2> /dev/null`
	if [ "X${PROXY}" != "Xnone" ] ; then
		PROFETCH_ARGS="-p ${PROXY}"
	fi

	case $URL in
	    *://*)
		# It's a URL, so try and download the begin, profile, and finish scripts.
		# in each case, we don't care about the result, since it will be
		# checked before being accessed, and fail with an appropriate error msg.

		# get the begin script
		if [ -n "${SI_BEGIN}" -a "X${SI_BEGIN}" != "X-" -a \
		    ! -f ${SI_CONFIG_DIR}/${SI_BEGIN} ]; then
		    $PROFETCH $PROFETCH_ARGS ${URL}/${SI_BEGIN} > /tmp/${SI_BEGIN}.$$
		    if [ $? = 0 ] ; then
			cat /tmp/${SI_BEGIN}.$$ > ${SI_CONFIG_DIR}/${SI_BEGIN} 2> /dev/null
		    fi
		fi

		# get the profile
		if [ -n "${SI_CLASS}" -a "X${SI_CLASS}" != "X--" \
		     -a "X${SI_CLASS}" != "X-" -a "X${SI_CLASS}" != "X=" -a \
		    ! -f ${SI_CONFIG_DIR}/${SI_CLASS} ]; then

		    $PROFETCH $PROFETCH_ARGS ${URL}/${SI_CLASS} > /tmp/${SI_CLASS}.$$
		    if [ $? = 0 ] ; then
			cat /tmp/${SI_CLASS}.$$ > ${SI_CONFIG_DIR}/${SI_CLASS} 2> /dev/null
		    fi
		fi
		     

		# get the finish script
		if [ -n "${SI_FINISH}" -a "X${SI_FINISH}" != "X-" -a \
		    ! -f ${SI_CONFIG_DIR}/${SI_FINISH} ]; then
		    $PROFETCH $PROFETCH_ARGS ${URL}/${SI_FINISH} > /tmp/${SI_FINISH}.$$
		    if [ $? = 0 ] ; then
			cat /tmp/${SI_FINISH}.$$ > ${SI_CONFIG_DIR}/${SI_FINISH} 2> /dev/null
		    fi
		fi
		;;

	    *)
		# it's not a URL, so for now it must be NFS, and we must have
		# already have access to all the begin, finish scripts, so bail
		return
		;;

	esac
}

# This shell function and the function templates_date_time() found
# in .../src/common/lib/libspmisvc/svc_templates.c are the same.  If a change
# is made to one, the same change has to be made to the other.
#
# $1 ::= filename to use to determine date/time string
# $2 ::= the directory and filename to check while
#        establishing a unique dated file name
#
#	Returns a date string of the form YYYY_MM_DD[_n]
#
solarisinstall_date_time()
{
	old_name=$1 

	# Set the LC_ALL env variable to C and export it
	# This will allow a "dated" filename to be created
	# unset LC_ALL after will reset it to the correct value
	if [ -n "$LC_ALL" ];then
		tmp_lc_all=$LC_ALL
	fi
	LC_ALL=C;export LC_ALL

	mon=`ls -l $old_name | awk ' { printf("%s", $6) } ' `
	day=`ls -l $old_name | awk ' { printf("%2.2d", $7) } ' `
	year=`ls -l $old_name | awk ' { printf("%s", $8) } ' `

	# clear the above LC_ALL setting, which will restore
	# the shell to the original value in /etc/default/init
	unset LC_ALL
	if [ -n "$tmp_lc_all" ];then
		LC_ALL=$tmp_lc_all
		export LC_ALL
	fi

	case $mon in
		Jan) mon_ndx=01 ;;
		Feb) mon_ndx=02 ;;
		Mar) mon_ndx=03 ;;
		Apr) mon_ndx=04 ;;
		May) mon_ndx=05 ;;
		Jun) mon_ndx=06 ;;
		Jul) mon_ndx=07 ;;
		Aug) mon_ndx=08 ;;
		Sep) mon_ndx=09 ;;
		Oct) mon_ndx=10 ;;
		Nov) mon_ndx=11 ;;
		Dec) mon_ndx=12 ;;
	esac
	mon_day="${mon_ndx}_${day}"

	if echo $year | grep ':' >/dev/null 2>&1
	then
		current_mon_ndx=`date "+%m"`
		year=`date "+%Y"`
		if [ "$mon_ndx" -gt "$current_mon_ndx" ] ; then
			year=`expr $year - 1`
		fi
	fi

	new_name="${2}_${year}_${mon_day}"
	ndx="0"
	ndx_str=
	while [ -f ${new_name}${ndx_str} ] ; do
		ndx=`expr $ndx + 1`
		ndx_str="_$ndx"
	done
	echo "${year}_${mon_day}${ndx_str}"
}

# Move the given log (either begin.log or finish.log) into the system 
# log directory.
move_log()
{
    LogName=$1
    #
    # remove any, and all, symbolic links from $LogName
    #
    if [ -h ${OLDLOGDIR}/${LogName} ]
    then
        rm -f ${OLDLOGDIR}/${LogName}
    fi

    if [ -h ${LOGDIR}/${LogName} ]
    then
        rm -f ${LOGDIR}/${LogName}
    fi

    #
    # move $LogName from old directory to new directory with 
    # dated log name.
    #
    if [ -f ${OLDLOGDIR}/${LogName} ]
    then
        date_time=`solarisinstall_date_time ${OLDLOGDIR}/${LogName} ${LOGDIR}/${LogName}`
        mv ${OLDLOGDIR}/${LogName} ${LOGDIR}/${LogName}_${date_time}
    fi

    #
    # rename $LogName, in new directory, to dated log name
    #
    if [ -f ${LOGDIR}/${LogName} ]
    then
        date_time=`solarisinstall_date_time ${LOGDIR}/${LogName} ${LOGDIR}/$LogName`
        mv ${LOGDIR}/${LogName} ${LOGDIR}/${LogName}_${date_time}
    fi

    #
    # If $LogName exists in /tmp, then move it to the new directory,
    # a dated log name, and set up appropriate symbolic links.
    #
    if [ -f /tmp/${LogName} -a -d ${LOGDIR} ]
    then
        date_time=`solarisinstall_date_time /tmp/${LogName} ${LOGDIR}/${LogName}`
        mv /tmp/${LogName} ${LOGDIR}/${LogName}_${date_time}
        ln -s ${LogName}_${date_time} ${LOGDIR}/${LogName}
        chmod 644 ${LOGDIR}/${LogName}_${date_time}
        echo `gettext "The ${2} script log \'${LogName}\'"`
        echo `gettext "\tis located in ${RBLOGDIR} after reboot."`
        echo
    fi
}


##############################################################################
# process rules.ok file, validate the version, make sure the version is valid
# for this version of install-solaris.sh; if so, invoke chkprobe to process the
# rules.ok file looking for a matching entry
#
##############################################################################
validate_and_chkprobe() {
if [ -f ${SI_CONFIG_FILE} ]; then
	ver_line=`egrep "# version=" ${SI_CONFIG_FILE}`
	if [ $? -ne 0 ]; then
		gettext "Error: Could not find rules file "
		echo "(${SI_CONFIG_FILE})"
	else
		VERSION=`expr "${ver_line}" : '.*version=\(.*\) .*'`
		if [ ${VERSION} -gt ${MY_VERSION} ]; then
			gettext "Error: File version is greater than expected: "
			echo "(${SI_CONFIG_FILE})"
			echo
		else
			echo `gettext "Checking rules.ok file..."`
			si_single_unlock
			. /usr/sbin/install.d/chkprobe
			if [ "X${_INIT_NET_STRATEGY}" = "Xdhcp" -o "X${_INIT_NET_STRATEGY}" = "Xmanual" ]; then
			    retrieve_support_scripts
			fi
			si_single_lock
		fi
	fi
fi
}

##############################################################################
# process pre-installation "begin" script if defined in the rules file
#
##############################################################################
process_preinstall() {
if [ -n "${SI_BEGIN}" -a X"${SI_BEGIN}" != "X-" ]; then
	if [ ! -f "${SI_CONFIG_DIR}/${SI_BEGIN}" ]; then
		echo `gettext "Warning: Could not find begin script "` \
			" (${SI_BEGIN})" | tee -a /tmp/${BEGINLOG}
	else
		echo `gettext "Executing begin script \"${SI_BEGIN}\"..."` | \
			tee -a /tmp/${BEGINLOG}
		si_single_unlock
		(/sbin/sh ${SI_CONFIG_DIR}/${SI_BEGIN} 
		echo $? > /tmp/begin_script_exit) 2>&1 | tee -a /tmp/${BEGINLOG}
		if [ -f /tmp/begin_script_exit ]; then
			read BEGINRET < /tmp/begin_script_exit
			rm -f /tmp/begin_script_exit
		else
			BEGINRET=0
		fi
		if [ ${BEGINRET} -eq 0 ]; then
			echo `gettext "Begin script ${SI_BEGIN} execution completed."` | tee -a /tmp/${BEGINLOG}
		else
			echo `gettext "Begin script ${SI_BEGIN} failed with return value \"${BEGINRET}\"."` | tee -a /tmp/${BEGINLOG}
			echo `gettext "Aborting install."` | tee -a /tmp/${BEGINLOG}
			exit 1
		fi
		si_single_lock
	fi
fi
}


##############################################################################
# process post-installation "finish" script if defined in the rules file
#
##############################################################################
process_postinstall() {
if [ -n "${SI_FINISH}" -a X"${SI_FINISH}" != "X-" ]; then
	if [ ! -f "${SI_CONFIG_DIR}/${SI_FINISH}" ]; then
		echo `gettext "Warning: Could not find finish script "` \
			"(${SI_FINISH})" | tee -a /tmp/${FINISHLOG}
	else
		echo `gettext "Executing finish script \"${SI_FINISH}\"..."`
		echo
		si_single_unlock
		/sbin/sh ${SI_CONFIG_DIR}/${SI_FINISH} 2>&1 | tee -a /tmp/${FINISHLOG}
		si_single_lock
		echo
		gettext "Finish script ${SI_FINISH} execution completed."
		echo
	fi
fi
}

##############################################################################
# Save Private 'SolStart' environment variables
##############################################################################
save_solstart_env() {
	# These environment variables are fixed for the installation 
	# environment
	#_HOME=${HOME};	_LANG=${LANG};	_TERM=${TERM};	_TZ=${TZ}
	#_SI_SYS_STATE=${SI_SYS_STATE}

	_CHECK_INPUT=${CHECK_INPUT}
	_PATH=${PATH}
	_SI_ARCH=${SI_ARCH}
	_SI_BEGIN=${SI_BEGIN}
	_SI_CLASS=${SI_CLASS}
	_SI_CONFIG_DIR=${SI_CONFIG_DIR}
	_SI_CONFIG_FILE=${SI_CONFIG_FILE}
	_SI_CONFIG_PROG=${SI_CONFIG_PROG}
	_SI_CUSTOM_PROBES_FILE=${SI_CUSTOM_PROBES_FILE}
	_SI_DISKLIST=${SI_DISKLIST}
	_SI_DISKSIZES=${SI_DISKSIZES}
	_SI_DOMAINNAME=${SI_DOMAINNAME}
	_SI_FINISH=${SI_FINISH}
	_SI_HOSTADDRESS=${SI_HOSTADDRESS}
	_SI_HOSTID=${SI_HOSTID}
	_SI_HOSTNAME=${SI_HOSTNAME}
	_SI_INSTALL_APP=${SI_INSTALL_APP}
	_SI_INSTALLED=${SI_INSTALLED}
	_SI_INST_OS=${SI_INST_OS}
	_SI_INST_VER=${SI_INST_VER}
	_SI_KARCH=${SI_KARCH}
	_SI_MEMSIZE=${SI_MEMSIZE}
	_SI_MODEL=${SI_MODEL}
	_SI_NETWORK=${SI_NETWORK}
	_SI_NUMDISKS=${SI_NUMDISKS}
	_SI_OSNAME=${SI_OSNAME}
	_SI_PROFILE=${SI_PROFILE}
	_SI_ROOTDISK=${SI_ROOTDISK}
	_SI_ROOTDISKSIZE=${SI_ROOTDISKSIZE}
}


##############################################################################
# Restore Private 'SolStart' environment variables
##############################################################################
restore_solstart_env() {
	# These environment variables are fixed for the installation 
	# environment
	#HOME=${_HOME};	LANG=${_LANG};	TERM=${_TERM};	TZ=${_TZ}
	#SI_SYS_STATE=${_SI_SYS_STATE}

	CHECK_INPUT=${_CHECK_INPUT}
	PATH=${_PATH}
	SI_ARCH=${_SI_ARCH}
	SI_BEGIN=${_SI_BEGIN}
	SI_CLASS=${_SI_CLASS}
	SI_CONFIG_DIR=${_SI_CONFIG_DIR}
	SI_CONFIG_FILE=${_SI_CONFIG_FILE}
	SI_CONFIG_PROG=${_SI_CONFIG_PROG}
	SI_CUSTOM_PROBES_FILE=${_SI_CUSTOM_PROBES_FILE}
	SI_DISKLIST=${_SI_DISKLIST}
	SI_DISKSIZES=${_SI_DISKSIZES}
	SI_DOMAINNAME=${_SI_DOMAINNAME}
	SI_FINISH=${_SI_FINISH}
	SI_HOSTADDRESS=${_SI_HOSTADDRESS}
	SI_HOSTID=${_SI_HOSTID}
	SI_HOSTNAME=${_SI_HOSTNAME}
	SI_INSTALL_APP=${_SI_INSTALL_APP}
	SI_INSTALLED=${_SI_INSTALLED}
	SI_INST_OS=${_SI_INST_OS}
	SI_INST_VER=${_SI_INST_VER}
	SI_KARCH=${_SI_KARCH}
	SI_MEMSIZE=${_SI_MEMSIZE}
	SI_MODEL=${_SI_MODEL}
	SI_NETWORK=${_SI_NETWORK}
	SI_NUMDISKS=${_SI_NUMDISKS}
	SI_OSNAME=${_SI_OSNAME}
	SI_PROFILE=${_SI_PROFILE}
	SI_ROOTDISK=${_SI_ROOTDISK}
	SI_ROOTDISKSIZE=${_SI_ROOTDISKSIZE}
}

##############################################################################
# Save JumpStart environment variables
##############################################################################
save_jumpstart_env() {
	# These environment variables are fixed for the installation 
	# environment
	#JS_HOME=${HOME}; JS_LANG=${LANG}; JS_TERM=${TERM}; JS_TZ=${TZ}
	#JS_SI_SYS_STATE=${SI_SYS_STATE}

	#save this for browser based installation 
	JS_CD_CONFIG_DIR=${CD_CONFIG_DIR}
	JS_CHECK_INPUT=${CHECK_INPUT}
	JS_PATH=${PATH}
	JS_SI_ARCH=${SI_ARCH}
	JS_SI_BEGIN=${SI_BEGIN}
	JS_SI_CLASS=${SI_CLASS}
	JS_SI_CONFIG_DIR=${SI_CONFIG_DIR}
	JS_SI_CONFIG_FILE=${SI_CONFIG_FILE}
	JS_SI_CONFIG_PROG=${SI_CONFIG_PROG}
	JS_SI_CUSTOM_PROBES_FILE=${SI_CUSTOM_PROBES_FILE}
	JS_SI_DISKLIST=${SI_DISKLIST}
	JS_SI_DISKSIZES=${SI_DISKSIZES}
	JS_SI_DOMAINNAME=${SI_DOMAINNAME}
	JS_SI_FINISH=${SI_FINISH}
	JS_SI_HOSTADDRESS=${SI_HOSTADDRESS}
	JS_SI_HOSTID=${SI_HOSTID}
	JS_SI_HOSTNAME=${SI_HOSTNAME}
	JS_SI_INSTALL_APP=${SI_INSTALL_APP}
	JS_SI_INSTALLED=${SI_INSTALLED}
	JS_SI_INST_OS=${SI_INST_OS}
	JS_SI_INST_VER=${SI_INST_VER}
	JS_SI_KARCH=${SI_KARCH}
	JS_SI_MEMSIZE=${SI_MEMSIZE}
	JS_SI_MODEL=${SI_MODEL}
	JS_SI_NETWORK=${SI_NETWORK}
	JS_SI_NUMDISKS=${SI_NUMDISKS}
	JS_SI_OSNAME=${SI_OSNAME}
	JS_SI_PROFILE=${SI_PROFILE}
	JS_SI_ROOTDISK=${SI_ROOTDISK}
	JS_SI_ROOTDISKSIZE=${SI_ROOTDISKSIZE}
}

##############################################################################
# Restore JumpStart environment variables
##############################################################################
restore_jumpstart_env() {
	# These environment variables are fixed for the installation 
	# environment
	#HOME=${JS_HOME}; LANG=${JS_LANG}; TERM=${JS_TERM}; TZ=${JS_TZ}
	#SI_SYS_STATE=${SI_SYS_STATE}

	#restore CD_CONFIG_DIR value
	CD_CONFIG_DIR=${JS_CD_CONFIG_DIR}
	CHECK_INPUT=${JS_CHECK_INPUT}
	PATH=${JS_PATH}
	SI_ARCH=${JS_SI_ARCH}
	SI_BEGIN=${JS_SI_BEGIN}
	SI_CLASS=${JS_SI_CLASS}
	SI_CONFIG_DIR=${JS_SI_CONFIG_DIR}
	SI_CONFIG_FILE=${JS_SI_CONFIG_FILE}
	SI_CONFIG_PROG=${JS_SI_CONFIG_PROG}
	SI_CUSTOM_PROBES_FILE=${JS_SI_CUSTOM_PROBES_FILE}
	SI_DISKLIST=${JS_SI_DISKLIST}
	SI_DISKSIZES=${JS_SI_DISKSIZES}
	SI_DOMAINNAME=${JS_SI_DOMAINNAME}
	SI_FINISH=${JS_SI_FINISH}
	SI_HOSTADDRESS=${JS_SI_HOSTADDRESS}
	SI_HOSTID=${JS_SI_HOSTID}
	SI_HOSTNAME=${JS_SI_HOSTNAME}
	SI_INSTALL_APP=${JS_SI_INSTALL_APP}
	SI_INSTALLED=${JS_SI_INSTALLED}
	SI_INST_OS=${JS_SI_INST_OS}
	SI_INST_VER=${JS_SI_INST_VER}
	SI_KARCH=${JS_SI_KARCH}
	SI_MEMSIZE=${JS_SI_MEMSIZE}
	SI_MODEL=${JS_SI_MODEL}
	SI_NETWORK=${JS_SI_NETWORK}
	SI_NUMDISKS=${JS_SI_NUMDISKS}
	SI_OSNAME=${JS_SI_OSNAME}
	SI_PROFILE=${JS_SI_PROFILE}
	SI_ROOTDISK=${JS_SI_ROOTDISK}
	SI_ROOTDISKSIZE=${JS_SI_ROOTDISKSIZE}
}


##############################################################################
# Main Script Body							MAIN
##############################################################################

# check for the install-solaris lock and exit if the lock is present
#
if [ -f ${LOCKFILE} ]; then
	echo `gettext "Solaris installation program is already running."`
	exit 1
else
	echo `gettext "Starting Solaris installation program..."`
	si_single_lock
fi

trap "si_single_unlock" 1 2 15	# ignore traps

# set the terminal type if not already set
#
if [ -z "${TERM}" ]; then
	TERM=`tail -1 /etc/.sysIDtool.state`
	export TERM
fi

# make sure "/" is exported with root access
# NOTE: this relies on useradd have mode 500 and returning error 2
#	for bad args
# NOTE:	there should be a better test here
#
/usr/sbin/useradd >/dev/null 2>&1
if [ $? -eq 1 ]; then
	gettext "Error: Unable to install system."
	echo
	gettext "       The Solaris installation image is not exported with root access."
	echo
	become_shell
fi


#
# Check for SMI Install configuration directories.  If it exists
# and running factory jumpstart or booting with the smi install
# boot parameter start up the SMI Install browser interface.
#
#if  [ -d ${CD_CONFIG_DIR} ]; then
#	if [ -f ${PREINSTALL} -o -f ${SMI_INSTALL} ]; then
if [ -d ${CD_CONFIG_DIR} -a -f ${SMI_INSTALL} ]; then
	/cdrom/.bbi/bin/smi_install.sh
	#
	# Check the return code
	# if it is non-zero then delete
	# the jumpstart directory so that
	# a normal factory or custom jumpstart
	# will take place
	if [ $? -ne 0 ]; then
		rm -rf ${SI_CONFIG_DIR}	
	fi
fi

#############################################################################
## Use the same JumpStart interfaces/mechanisms for initiating 'begin' and ##
## 'finish' scripts for mandatory Solaris installation procedures without  ##
## interfering with the Custom and Factory JumpStart entries.              ##
##                                                                         ##
## Use a private (_SI_*) set of JumpStart environment variables so a to    ##
## not corrupt the external JumpStart environment variables.               ##
#  1) Mount the path to the JumpStart directory (profind)
#  2) Validate the contents of the JumpStart directory (chkprobe)
#  3) Set the application type (jumpstart or interactive)
#  4) JumpStart (Custom or Factory) begin script
#  5) Save the JumpStart environment variables
#  6) Mount the path to the SolStart directory
#  7) Validate the contents of the SolStart directory (chkprobe)
#  8) SolStart (SunSoft and OEM) begin script
#  9) Save the SolStart environment variables
# 10) Restore the JumpStart environment variables
# 11) Interactive install (ttinstall)
#       or
#     JumpStart (Custom or Factory) profile install (pfinstall/pfupgrade)
# 12) Restore the SolStart environment variables
# 13) SolStart (SunSoft and OEM) finish script
# 14) Restore the JumpStart environment variables
# 15) JumpStart (Custom or Factory) finish script
# 16) Cleanup
#############################################################################

# mount the appropriate profile directory and search for the for profile
# for JumpStart and custom JumpStart boots; make sure the use hasn't tried
# to restart JumpStart install
#
if [ -f ${PREINSTALL} -o -f ${INSTALLBOOT} ]; then
	if [ "${SI_INSTALL_APP}" = "jumpstart" -a -f ${JSLOCKFILE} ]; then
		gettext "You must reboot the system to restart a JumpStart install."
		echo
		si_single_unlock
		exit 1
	else
		touch ${JSLOCKFILE}
		/usr/sbin/install.d/profind
	fi
fi

# process rules.ok file, validate the version, make sure the version is valid
# for this version of install-solaris.sh; if so, invoke chkprobe to process the
# rules.ok file looking for a matching entry
#

validate_and_chkprobe

# set the application type for JumpStart and custom JumpStart boots;
# custom JumpStart boots require a profile or else the interactive
# install will run
#
if [ -f ${INSTALLBOOT} ]; then
	# make sure that SI_CLASS is defined; remember it could be
	# '=' for derived classes, so use the 'X' test method
	#
	if [ X"${SI_CLASS}" != "X" ]; then
		SI_INSTALL_APP=jumpstart
	else
		gettext "Warning: Could not find matching rule in "
		echo ${SI_CONFIG_PROG}
		
		# Only prompt for enter key when in console mode, since
		# if we don't, the error message will get overwritten
		if [ -z "$DISPLAY" ] ; then
		    gettext "Press the return key for an interactive Solaris install program..."
		    read input
		fi
	fi
fi

if [ -f ${PREINSTALL} ]; then
	# make sure that SI_CLASS is defined; remember it could be
	# '=' for derived classes, so use the 'X' test method
	#
	if [ X"${SI_CLASS}" != "X" ]; then
		SI_INSTALL_APP=jumpstart
	fi
fi

# Process the JumpStart preinstall actions
# and save off the JumpStart environment variables to preserve them for the
# JumpStart profile and postinstall script processing.
#
echo `gettext "Executing JumpStart preinstall phase..."`
process_preinstall
save_jumpstart_env

# SolStart processing will take place unless a Custom Jumpstart has
# been specified AND a `*' has been specified as the profile.
if [ ! \( "${SI_INSTALL_APP}" = "jumpstart" -a "${SI_PROFILE}" = "--" \) ]; then
    # Do the equivalent of what is done in 'profind' for the SolStart
    # mechanisms.  Note the use of '/tmp/_install_config' for a mountpoint
    # rather than the JumpStart '/tmp/install_config' mountpoint.
    #
    SI_CONFIG_DIR=/tmp/_install_config
    CD_CONFIG_DIR=/usr/sbin/install.d/install_config
    SI_CONFIG_FILE=${SI_CONFIG_DIR}/${SI_CONFIG_PROG}

    # Make the mount point directory used in searching
    # for the SolStart begin and finish scripts
    #
    if [ ! -d ${SI_CONFIG_DIR} ]; then
	mkdir ${SI_CONFIG_DIR} 2>/dev/null
    fi
    
    echo `gettext "Searching for SolStart directory..."`

    mount -o ro -F lofs ${CD_CONFIG_DIR} ${SI_CONFIG_DIR} > /dev/null 2>&1

    # Validate and run chkprobe on the SolStart 'rules.ok' file
    # and save the resulting SolStart environment variables for future use
    #
    validate_and_chkprobe


    # Process the SolStart preinstall actions.  After the preinstall
    # actions have been processed, the SolStart environment variables
    # are saved for SolStart postinstall processing.
    #
    echo `gettext "Executing SolStart preinstall phase..."`
    process_preinstall
    save_solstart_env

    # Restore the JumpStart environment variables for the profile phase

    restore_jumpstart_env
fi

# process a jumpstart installation
#

if install_debug_isset install ; then
    INSTALL_FLAGS="-x 10"
else
    INSTALL_FLAGS=""
fi

if [ "${SI_INSTALL_APP}" = "jumpstart" ]; then
    if [ -n "${SI_PROFILE}" -a "${SI_PROFILE}" != "--" ] ; then
	# User specified a profile
	if [ ! -f "${SI_PROFILE}" ]; then
		# Specified profile file doesn't exist; prompt the
		# user to continue an interactive installation.
		#
		gettext "Error: Could not find the profile "
		echo "(${SI_PROFILE})"
		# Only prompt for enter key when in console mode, since
		# if we don't, the error message will get overwritten
		if [ -z "$DISPLAY" ] ; then
		    gettext "Press the return key for an interactive Solaris install..."
		    read input
		fi
		SI_INSTALL_APP=interactive
	else
		pfinstall ${INSTALL_FLAGS} ${SI_PROFILE}
		ESTATUS=$?
	fi
    #else - User explicitly didn't specify a profile
    fi
fi


# Write the SI_INSTALL_APP to the /tmp/install_app so that it can
# be used by an external program like basic registration
#
echo "SI_INSTALL_APP=${SI_INSTALL_APP}" > ${TMP_INSTALL_APP}

# process an interactive installation
#

if [ "${SI_INSTALL_APP}" = "interactive" ]; then
	if [ -n "${SI_NONINTERACTIVE_EXIT}" ] ; then
	    # whoever called install-solaris doesn't want an interactive
	    # session, so exit with the specified code
	    exit ${SI_NONINTERACTIVE_EXIT}
	fi

	# clear out any files that may have been left around by
	# a previous invocation
	rm -rf ${LAUNCHDIR}/.needShell ${LAUNCHDIR}/.autoreboot

	# select the helpfile directory based on the status of the
	# L10N environment variables
	#
	if [ -n "${LC_MESSAGES}" ]; then
		HELPPATH=/usr/openwin/lib/locale/${LC_MESSAGES}/help
	elif [ -n "${LANG}" ]; then
		HELPPATH=/usr/openwin/lib/locale/${LANG}/help
	else
		HELPPATH=/usr/openwin/lib/locale/C/help
	fi

	#
	# run interactive application 
	#

	ADMINHELPHOME=${HELPPATH}/install.help
	export ADMINHELPHOME

	MEM=`/sbin/mem`
	MEM=`expr $MEM / 1024`

	if [ -n "$DISPLAY" ] ; then
	    echo `gettext "Starting Solaris Installation..."`
	    if [ "$MEM" -ge "$MIN_GUI_INSTALL_VIRTMEM" -a \
		"$PHYSMEM" -ge "$MIN_GUI_INSTALL_PHYSMEM" -a \
		"$RUN_TEXT_INSTALL" = "no" ] ; then
		# run GUI wizard
		java ${WIZARDPROPS} solaris -R /a \
		    -autonext Summary:90 $WIZARDFLAGS >> $INSTALL_LOG 2>&1
		if [ -f /tmp/.instsuccess ] ; then
		    if [ -d ${LAUNCHDIR}/.autoreboot ] ; then
			# GUI wizard success, auto reboot selected
			ESTATUS=0
		    else
			# GUI wizard success, manual reboot selected
			ESTATUS=1
		    fi
		else
		    # GUI wizard failure
		    ESTATUS=2
		fi
	    else
		# run CLI wizard in a terminal
		SOL_INS_WIN_TITLE=`gettext "Solaris Install"`
		SIEXIT=/tmp/install-solaris.exit
		/usr/dt/bin/dtterm -geometry 80x34+200+200 \
		    -title "${SOL_INS_WIN_TITLE}" \
		    -e /sbin/exitwrapper \
		    "/usr/sbin/ttinstall ${INSTALL_FLAGS}" \
		    ${SIEXIT}
		if [ -f ${SIEXIT} ]; then
			ESTATUS=`head -1 ${SIEXIT} | awk '{print $1}'`
			rm -f ${SIEXIT}
		else
			ESTATUS=2
		fi
	    fi
	else
	    /usr/sbin/ttinstall ${INSTALL_FLAGS}
	    ESTATUS=$?
	fi
fi

install_debug scripts "ESTATUS: $ESTATUS"

# if the installation application succeeded, do post-install activities
if [ ${ESTATUS} -eq 0 -o ${ESTATUS} -eq 1 ]; then

    # SolStart processing will take place unless a Custom Jumpstart has
    # been specified AND a `*' has been specified as the profile.
    if [ ! \( "${SI_INSTALL_APP}" = "jumpstart" -a "${SI_PROFILE}" = "--" \) \
	-a ! -f $SUPPRESS_SOLSTART_FINISH ]; then
	# Restore the SolStart environment variables and
	# Process the SolStart preinstall actions

	restore_solstart_env
	echo `gettext "Executing SolStart postinstall phase..."`
	process_postinstall
    fi

    # Restore the JumpStart environment variables and
    # Process the JumpStart preinstall actions.

    restore_jumpstart_env
    echo `gettext "Executing JumpStart postinstall phase..."`
    process_postinstall

    # spacing for final messages
    #
    echo

    # Move the begin log into the system log directory
    #
    move_log $BEGINLOG $BEGIN

    # Move the finish log into the system log directory
    #
    move_log $FINISHLOG $FINISH

    # We *can* run launcher setup, but should we?
    # only if we were doing an interactive install OR
    # we were doing a jumpstart install and solstart
    # wasn't supressed.
    if [ "${SI_INSTALL_APP}" = "interactive" -o \
	\( "${SI_INSTALL_APP}" = "jumpstart" -a \
	"${SI_PROFILE}" != "--" -a \
	! -f $SUPPRESS_SOLSTART_FINISH \) ] ; then
	    if [ -x /sbin/setup-launcher ] ; then

		/sbin/setup-launcher	"${ESTATUS}" "${WIZARDFLAGS}" \
					"${SI_INSTALL_APP}"

		# setup-launcher has the ability to override
		# the reboot status
		ESTATUS=$?
	    fi
    fi	    
fi

# exiting the install routine so remove the lock file
si_single_unlock

install_debug scripts "$0 completes `date`"

if [ "$ESTATUS" -eq 0 ] ; then
    reboot
elif [ "$ESTATUS" -eq 1 ] ; then
    echo `gettext "Solaris installation program completed."`
else
    echo `gettext "Solaris installation program exited."`
fi

if [ "${SI_INSTALL_APP}" = "jumpstart" ] ; then
    # user did a non-reboot jumpstart, so just stop here
    exec /sbin/sh
fi

exit ${ESTATUS}
