#!/bin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)install-begin.sh	1.4	06/03/10 SMI"
#
#
# 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: At this point on non-Intel systems, the locale has
#	not yet been configured
#
# NOTE:	This script may be called from outside the window
#	system (boot with "nowin" or kdmconfig/prtconf 
#	configuration failed) or within the window system
#
. /sbin/install-common
if install_debug_isset scripts ; then
    set -x
fi

install_debug scripts "$0 begins `date`"

WIZDIR=/usr/lib/install/data
TMP_ROOT_INSTALL_DATA=/tmp/root/install/data
PLATFORM=`/sbin/uname -p`
JS_FILE=/tmp/.install_boot
SI_LOCK=/tmp/.install-solaris
PREINSTALL_FILE=/tmp/.preinstall
PLATFORM=`uname -p`
WIZARDFLAGS=
WIZARDPROPS=
RESTART=0
CLASSPATH=${WIZDIR}/wizards/apps
export CLASSPATH
CONSOLE_PID=0

# args: $1 - file to display in console
# $2 - optional geometry spec
#
# global args set: $CONSOLE_PID is set to the
# process id of the console
display_console() {
    CONSOLE_PID=0
    if [ -z "$2" ] ; then
	GEOMETRY=100x8-0-0
    else
	GEOMETRY=$2
    fi
   
    TITLE=`gettext "Solaris Install Console"`
 
    /usr/dt/bin/dtterm -C -geometry $GEOMETRY \
	-title "${TITLE}" \
	-xrm "*menuBar: False" \
	-sl 5000 \
	-e /usr/bin/tail -f $1 &
    CONSOLE_PID=$!
}


#################################
#################################
# Main Script Body
#
# Parse arguments
while getopts rc: c ; do
        case $c in
            r)
                RESTART=1
                ;;
	    c)
		display_console ${OPTARG}
		exit 0
		;;
	    \?)
		echo "usage: $0 [-r] [-c <console]"
		exit 1
		;;
        esac
done
shift `expr $OPTIND - 1`

if [ -x /usr/sbin/install.d/find_device ] ; then
    CD_ID=`/usr/sbin/install.d/find_device -c`
elif [ -x $WIZDIR/wizards/bin/find_device ] ; then
    CD_ID=`$WIZDIR/wizards/bin/find_device -c`
fi

### Beef up the ulimit
### Helps with the limitation of files (processes) open
### from within the browser
/usr/bin/ulimit -n 256

# user is attempting a GUI install, so start up a console if this
# is the first time thru
if [ "$RESTART" -eq 0 -a -n "$DISPLAY" ] ; then
    rm -f $INSTALL_CONSOLE > /dev/null 2>&1
    touch $INSTALL_CONSOLE > /dev/null 2>&1
    display_console $INSTALL_CONSOLE
fi
#
# Run the sysid programs
#
if [ "$RESTART" -eq 0 ] ; then
    if [ -n "$DISPLAY" ] ; then
	/sbin/sysid-finish >> $INSTALL_CONSOLE 2>&1
	echo `gettext "System identification complete."` >> $INSTALL_CONSOLE
    else
	/sbin/sysid-finish
	echo `gettext "System identification complete."`
    fi
fi

#
# Source in any new TZ settings and export them
#
TZ=""
. /etc/default/init
export TZ

# 
# Re-set the netmask and broadcast addr for all non-DHCP IP interfaces.  This
# ifconfig is run here, after NIS has been started, so that "netmask
# +" will find the netmask if it lives in a NIS map.
#

if [ "$RESTART" -eq 0 ] ; then
    /usr/sbin/ifconfig -auD netmask + broadcast + > /dev/null
fi

install_debug scripts "DISPLAY:<$DISPLAY> JS_FILE: <$JSFILE> \
    PREINSTALL_FILE: <$PREINSTALL_FILE>"


# set wizard flags for debug, display
if [ -z "$DISPLAY" ] ; then
  WIZARDFLAGS=" -nodisplay "
  WIZARDPROPS="${WIZARDPROPS} -Djava.awt.headless=true "
fi

if install_debug_isset wizard ; then
	WIZARDFLAGS=" -debug $WIZARDFLAGS "
fi

# save find_device results for launcher
# intel OS cd's use p0 as the slice that contains the Solaris products.
# bugid 4239378
if [ "$PLATFORM" = "i386" ];then
  CD_ID=`echo $CD_ID | sed -e 's:s:p:g'`
  install_debug wizard "initial CD_ID found: $CD_ID"
fi

cdid=

# If there is a /.cdroot because this is a cd0 install, use it. Otherwise
# use /tmp/.cdroot written by install-discovery
#
CDROOT=/tmp/.cdroot
if [ -f /.cdroot ]; then
    CDROOT=/.cdroot
fi

# If a .cdroot file exists, the Solaris Wizard will select Solaris CD
# by default on SolarisMediaPanel
#
if [ -f $CDROOT ];then          # This is a cdrom installation
  if [ ! -z $CD_ID ];then
    for i in $CD_ID;do
      if [ -b /dev/dsk/$i ];then
	if [ -L /dev/dsk/$i ];then
	  ls -l /dev/dsk/$i | grep `cat $CDROOT` \
	   >> /dev/null 2>&1
	  if [ $? -eq 0 ];then
	    cdid=$i
	    break
	  fi
	fi
      fi
    done
  fi
fi

if [ -z "$cdid" ];then
        for i in $CD_ID;do
                if [ -b /dev/dsk/$i ];then
                        cdid=$i
                        break
                fi
        done
fi

echo $cdid > /tmp/find_device.out
install_debug wizard "final cd_id found: $cdid"

#
# Setup media_kits basedir in a writable filesystem. Required for any
# later Add Product media scans or downloads.  Starts as symbolic link.
# Will then use /tmp writable space for media_kits tables only if/when
# an Add Product scan or download occurs.
#

if [ ! -d ${TMP_ROOT_INSTALL_DATA} ];then
        /usr/bin/mkdir -p ${TMP_ROOT_INSTALL_DATA} 
fi

if [ "$RESTART" -eq 0 -a ! -d ${TMP_ROOT_INSTALL_DATA}/media_kits ];then
	ln -s /usr/lib/install/data/media_kits \
	    ${TMP_ROOT_INSTALL_DATA}/media_kits
fi

export WIZARDFLAGS WIZARDPROPS

install_debug wizard "WIZARFLAGS: <$WIZARDFLAGS> WIZARDPROPS: <$WIZARDPROPS>"

if [ -n "$DISPLAY" ] ; then
    if [ -f $JS_FILE -o -f $PREINSTALL_FILE ] ; then
	# user is jumpstart/preinstall, so make console somewhat large and controlling
	GEOMETRY=100x40-0-0
	SIEXIT=/tmp/install-solaris.exit
	/usr/dt/bin/dtterm -C -geometry $GEOMETRY \
	    -title "Solaris Install JumpStart" \
	    -sl 10000 -e /sbin/exitwrapper /sbin/install-solaris $SIEXIT
	ESTATUS=`head -1 ${SIEXIT} | awk '{print $1}'`
	rm -f ${SIEXIT}
    else
	GEOMETRY=100x8-0-0
	touch $INSTALL_CONSOLE > /dev/null 2>&1
	# show console if first time through
	if [ $RESTART -eq 0 ] ; then
	    display_console $INSTALL_CONSOLE $GEOMETRY
	fi
	/sbin/install-solaris >> $INSTALL_CONSOLE 2>&1
	ESTATUS=$?
    fi
else
    /sbin/install-solaris
    ESTATUS=$?
fi

install_debug scripts "$0 completes `date`"

if [ "$ESTATUS" = 0 ] ; then
    reboot
else
    if [ -n "$DISPLAY" ] ; then
	if [ "$RESTART" -eq 0 ] ; then
	    # fire up a terminal for them
	    GEOMETRY=80x20+0+0
	    echo `gettext "Opening interactive terminal window..."` >> $INSTALL_CONSOLE
	    /usr/dt/bin/dtterm -geometry $GEOMETRY \
		-title "Terminal" \
		-name "Terminal" \
		-sl 10000
	    if [ -f "$JS_FILE" -o -f "$PREINSTALL_FILE" ] ; then
		echo `gettext "You must reboot the system to restart a JumpStart install."` >> $INSTALL_CONSOLE
	    fi
	    
	    echo `gettext "To start another terminal, select \"Terminal\" from the \"Programs\" Workspace Menu."` >> $INSTALL_CONSOLE

	    exec /sbin/sh
	fi
    else
	exec /sbin/sh
    fi
fi

