#!/sbin/sh
#
#ident	"@(#)install-discovery.stub.sh	1.30	05/04/28 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# 2.X stub "install-discovery" file
# run by init(8) to figure where to really boot from to do the install
# replaces normal install-discovery
#
# * init starts
#  *     mount tmpfs onto /tmp
#  *     copy any needed files into /tmp...
#  *     device reconfigure
#  *     figure out where to boot from
#  *     reboot
#

# returns yes if the system is running devfs, nothing otherwise
isdevfs() {
    while read special mount_point fstype options time; do
      if [ "X${special}X${mount_point}X${fstype}" = \
          "X/devicesX/devicesXdevfs" ]
      then
          echo yes
          break
      fi
    done < /etc/mnttab
}

################# main ###################
PATH=/sbin:${PATH}
export PATH

#
# Mount tmpfs and procfs and start accumulating mnttab
# entries for when mnttab is writeable
#
if [ ! -f /tmp/.rcSmnt ]; then
	# mount tmpfs, for now it can't swap so we can't fill it too full
	/sbin/mount -F tmpfs swap /tmp
	if [ $? -ne 0 ]; then
		echo "tmpfs mount failed."
		/sbin/sh
	fi
	cat < /dev/null > /tmp/.rcSmnt
fi

if [ "${RB_NOBOOTRC}" = "YES" ]; then
	RB_NOBOOTRC="no"
	export RB_NOBOOTRC
	/sbin/sh
fi

#
# * copy over any writeable (prototype) files to /tmp from "/root"
# NOTE: canonical paths are /tmp/root, but use cpio to do mkdir
# NOTE: send output to /tmp (not /dev/null) to avoid nfs bug on remote
#       ro mounted fs
#
( cd /.tmp_proto; find . -print -depth | cpio -pdm /tmp 2>/tmp/cpio.out )

################################################
# We now have writable configuration files.
################################################

#
# configure devfs in /tmp/devices
#
echo "Configuring devices."

if [ `isdevfs` ]
then # devfs
    ln -sf /devices /tmp/devices
else # not devfs
    cd /tmp
    mkdir devices
fi

/usr/sbin/devfsadm -r /tmp -p /tmp/path_to_inst
if [ $? -ne 0 ]; then
	/usr/sbin/devfsadm -r /tmp
fi

#
# Get the root device
#
eval `/sbin/get_root -r /tmp/devices -e -b Rootfs -c RootfsRaw /`
echo "${Rootfs} ${RootfsRaw} / ufs - no -" >> /etc/vfstab 

#
# Make sure the root FS is clean
#
/etc/fsck -F ufs -o p ${RootfsRaw} >/dev/null
if [ $? -ne 0 ]; then
	echo "fsck of ${RootfsRaw} failed, re-preinstall needs to be re-run."
	/usr/sbin/halt
fi
 
#
# Remount the root FS R/W
#
mount -o remount / 

#
# If a new path_to_inst was created, put it in place.
#
if [ -f /tmp/path_to_inst ]; then
	cat </tmp/path_to_inst >/etc/.path_to_inst
fi

#
# Move the drvconfig results to /devices
#
cd /tmp/devices
find . -depth -print | cpio -pdm /devices 2> /tmp/cpio.out

#
# Complete the device configuration
#
cd /
/usr/sbin/devfsadm

#
# Configure all network interfaces
#
/sbin/ifconfig -a plumb > /dev/null 2>&1

#
# Get the complete list of network devices
# so that we can revarp them individually
# since the -ad option seems to stop after
# the first failure (unconnected net device)
# that it encounters
#
for i in `ifconfig -a |grep "^[a-z0-9]*:"`
do
	echo $i |grep "^[a-z0-9]*:" >/dev/null 2>&1
	if [ $? -eq 1 ]; then
		continue
	fi
	net_device_list="${i}${net_device_list}"

done

#
# net_device_list contains a ":" delimited list
# of network devices
# do an auto-revarp on each of them with the
# exception of the loopback device
#
old_ifs=$IFS
IFS=":"
set -- $net_device_list
for i 
do
	#
	# skip the auto-revarp for the loopback device
	#
	if [ "$i" = "lo0" ]; then
		continue
	fi
	/sbin/ifconfig $i auto-revarp up >/tmp/dev.$$ 2>&1
done
IFS=$old_ifs

. /sbin/stubboot
 
if [ "${BOOT_STRING}" ]; then 
	reboot "${BOOT_STRING}"
else 
	echo
	echo "No network boot server. Unable to install the system."
	echo "See installation instructions."
	echo
	sync;sync
	/sbin/uadmin 2 0  # force it down 
fi
