#!/sbin/sh
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#pragma ident	"@(#)install-du.sh	1.1	05/09/06 SMI"

#
# This script extracts content of ITU media and add to the
# miniroot. It also stores the content in /tmp/DU. 
# At the end of install, it is invoked (with rootdir arg) to
# copy the saved media to target OS.

install_to_target()
{
	#
	# If ITU packages were installed on the miniroot, put it
	# on the target disk as well.
	#
	VERS="`uname -r | sed -e 's/5\./2/'`"
	MACH=`uname -m`
	for i in $ITUDIR/*; do
		ICMD=$i/DU/sol_${VERS}/$MACH/Tools/install.sh
		if [ -x "${ICMD}" ]; then
			${ICMD} -R ${OSROOT}
		fi
	done
}

install_to_miniroot()
{
	if [ -d $ITUDIR/$media_count ]; then
		VERS="`uname -r | sed -e 's/5\./2/'`"
		MACH=`uname -m`
		ICMD=$ITUDIR/$media_count/DU/sol_${VERS}/$MACH/Tools/install.sh
		if [ -x "${ICMD}" ]; then
			echo "installing driver update in the miniroot"
			${ICMD} << EOF > /dev/null 2>&1
y
y
y
EOF
			# run devfsadm to make sure any new disks
			# are enumerated
			/usr/sbin/devfsadm
		fi
	fi
}

extract_floppy()
{
	/sbin/mount -F pcfs -o ro /dev/diskette /mnt >/dev/null 2>&1
	status=$?

	if [ ${status} -eq 0 ]; then
		if [ -d /mnt/DU ]; then
			echo "extracting software on floppy"
			mkdir -p $ITUDIR/$media_count
			/usr/bin/cp -p -r /mnt/DU $ITUDIR/$media_count
			install_to_miniroot
		else
			echo "floppy contains no driver updates"
		fi
		/sbin/umount /dev/diskette
	else
		echo "floppy failed to mount"
	fi
}

extract_cdrom()
{
	cdrom_found=0
	for Dev in /dev/dsk/*p0; do
		Typ=`/usr/sbin/fstyp $Dev 2> /dev/null`
		if [ "X${Typ}" = Xhsfs ]; then
			/sbin/mount -o ro -F hsfs $Dev /mnt
			status=$?

			if [ ${status} != 0 ]; then
				continue;
			fi

			cdrom_found=1
			if [ -d /mnt/DU ]; then
				echo extracting software in $Dev
				mkdir -p $ITUDIR/$media_count
				/usr/bin/cp -p -r /mnt/DU $ITUDIR/$media_count
				install_to_miniroot
				itu_cdrom=`expr $itu_cdrom + 1`
				/sbin/umount $Dev 2> /dev/null
				break
			else
				echo "CD in $Dev does not contain driver updates"
				/sbin/umount $Dev 2> /dev/null
			fi
		fi
	done
	if [ $cdrom_found = 0 ]; then
		echo "CD/DVD not found"
	fi
}

ITUDIR=/tmp/ITUs
if [ $# = 1 ]; then
	# we are invoked to install ITU media to target OS
	OSROOT=$1
	install_to_target
	exit 0
fi

#
# We are at the beginning of install.
# remount root rw so install.sh will succeed
#
/sbin/mount -o remount,rw /
#
# make add_drv -b / load and attach driver
#
touch /ADD_DRV_IGNORE_ROOT_BASEDIR
/usr/bin/mkdir -p $ITUDIR
media_count=0
itu_cdrom=0

while true; do
	echo " "
	echo "Insert media and enter media type:"
	echo "	Floppy [f], CD/DVD [c] or End [e]"
	read x
	if [ X$x = Xe ]; then
                if [ $itu_cdrom != 0 ]; then
                        echo "Reinsert Solaris media now and press enter."
                        read x
                fi
		exit 0
	fi

	if [ X$x = Xf ]; then
		extract_floppy
	elif [ X$x = Xc ]; then
		extract_cdrom
	fi
	media_count=`expr $media_count + 1`
done
