:
#ident "@(#)hd.sh	1.14 91/02/07"
#
#	Copyright (C) The Santa Cruz Operation, 1987, 1988, 1989.
#	Portions Copyright (C) Altos Computer Systems 1989, 1990.
#	This Module contains Proprietary Information of
#	Altos Computer Systems, The Santa Cruz Operation, 
#	and should be treated as Confidential.
#

#
# HDINIT	- UNIX System V Hard Disk Initialization
#
# Version 2.4   - 11/28/88      -2.3 GT: ST506, ESDI & SCSI support
# Version 2.5   - 12/01/88      -3.2 modularization and no-prompt
#		- 05/04/89	-new 3.2 SCSI/hd driver support
# Version 2.6	- 11/13/89	-integrate Altos multiple major numbers
#

# set the standard exit values
: ${OK=0} ${FAIL=1} ${STOP=10}

# set the environment variables
PATH=/etc:/bin:/usr/bin:/altos/bin:/mountram/usr/bin:/mountram/altos/bin:\
/mountram/bin
export PATH
umask 0

SADAPTER=${ROOT}/etc/conf/cf.d/Sadapter
TMPFILE=/tmp/_Sadapter
TMPFILE2=/tmp/_Sadapter2
TMPFILE3=/tmp/__scsinfo
devdscr="Hard Disk"
q=q

if [ -x /bin/clear ]
then
	CLEAR=`clear`
else
	CLEAR=`echo "\n\n"`
fi

# cd to root
cd /

# define some numbers and flags here
progname=$0		# program name
dmin=0			# minimum number of non scsi drives
dmax=1			# maximum number of non scsi drives
cmin=0			# minimum number of controllers (0=ST506, 1=OMTI)
cmax=1			# maximum number of controller
interactive=yes		# interactive or not
diskpercent=120		# nice round number used to calculate max badtrks (8%)
scsioffset=64		# starting device number for first SCSI hd
smin=0			# mininmum number of scsi drives
smax=6			# max number of scsi drives
maxTIdsks=7		# max number of disks for TI driver
maxHPFPdsks=28		# max number of disks for HPFP driver
rootinstall=		# flag to say whether we're installing a root disk
mirror_install=		# flag to indicate installation of a root mirror
mirror_dev=		# which root mirror device ("b", "c", etc...)
firstroot=		# flag to say we're installing a root disk from floppy
scsiflg=		# flag for installing scsi disks
nondestruct=		# flag for adding hard disks nondestructively 
tmpdir=			# used for floppy installs to access utilities
tmpdev=			# used for floppy installs to access devices
tmp=/tmp/hd_
no_args=
TRUE=1
diskset="b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I"
			# SCSI disk index

# Standard functions

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n${mesg}\n\tor enter '$q' to quit:  \c" >&2
	do	read cmd
		case $cmd in
		+x|-x)	set $cmd					;;
		$q)	return $FAIL					;;
		!*)	eval `expr "$cmd" : "!\(.*\)"`			;;
		"")	# If there is an argument use it as the default
			# else loop until 'cmd' is set
			[ "$1" ] && { 
				cmd=$1
				return $OK
			}
			: continue
			;;
		*)	return $OK					;;
		esac
	done
}

# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	echo "\n\t$* (y/n):  \c">&2
	do	read yn rest
		case $yn in
		[yY])	return $OK 			;;
		[nN])	return $FAIL			;;
		*)	echo "Please answer 'y' or 'n'" >&2	;;
		esac
	done
}

# mkdev hd functions

# set a trap so that if the user dels out, a value of $STOP
# is passed to the calling program
settrap() {
	if [ -n "$interactive" ]
	then 	
		if [ -n "$no_args" ]
		then
			trap 'echo "\nInterrupted: enter \"$progname $ID $HA $LUN $HATYPE\" to restart" >&2
			cleanup $FAIL'	1 2 3 15
		else
			trap 'echo "\nInterrupted: enter \"$progname $id $ctrlnum $lun $hatype\" to restart" >&2
			cleanup $FAIL'	1 2 3 15
		fi
	else	trap 'cleanup $FAIL' 1 2 3 15
	fi
}

# unset the traps
unsettrap() {
	trap '' 1 2 3 15
}

# called with $OK (0) if successful; $FAIL (1) or other args on failure
cleanup () {
	[ "$firstroot" = "TRUE" ] &&  {
		if [ "$1" != "0" ]
		then
			case $1 in
				fdisk_err) echo "$tmpdir/etc/fdisk failed." ;;
				batch_fdisk) echo "Cannot create a single, active UNIX virtual drive." ;;
			esac
			exit $FAIL
		fi
		exit $OK
	}

	# clean up perms file and remove unused disk devices
	if [ "$1" != "0" ]
	then
		# new disk wasn't successfully accessed
		[ "$fixdone" = "TRUE" ] && {

		unsettrap
		# HD perms list was modified, restore previous one
		mv /usr/lib/mkdev/perms/HD.00 /usr/lib/mkdev/perms/HD
		settrap

		# remove newly created devices
		rm -f /dev/rdsk/${disknum}s* 2> /dev/null      
		rm -f /dev/dsk/${disknum}s* 2> /dev/null      
		rm -f /dev/rhd${disknum}* 2> /dev/null     
		rm -f /dev/hd${disknum}* 2> /dev/null     
		rm -f /dev/hd${disknum}root 2> /dev/null     
		rm -f /dev/rhd${disknum}root 2> /dev/null     
		rm -f /dev/hd${disknum}usr 2> /dev/null     
		rm -f /dev/rhd${disknum}usr 2> /dev/null     

		}

		[ "$1" = "fdisk_err" ] &&  echo "/etc/fdisk failed." 
		rm -f ${tmp}?.$$ 2> /dev/null
		exit $FAIL
	fi

	# non-root install successful
	rm -f /usr/lib/mkdev/perms/HD.00 ${tmp}?.$$ 2> /dev/null
	rm -f $TMPFILE $TMPFILE2 $TMPFILE3
	exit $OK
}

error () {
      case $1 in
 	  bad_arg) a="Command line argument is invalid.\nInvoke mkdev hd -u\
 for a usage message."							    ;;
	   bad_ti) a="TI controllers support only host adapter 0."	    ;;
	   bad_ad) a="AD controllers support only host adapters 0 and 1."   ;;
	 bad_hpfp) a="HPFP controllers support only host adapters 0 - 3."   ;;
     bad_ctlr_arg) a="Invalid controller argument.\nInvoke mkdev hd -u\
 for a usage message."			   				    ;;
      nonint_ctrl) a="Invalid command line argument.\nNoninteractive \
installation valid only on controller 0."			            ;;
       nonint_drv) a="Invalid command line argument.\nNoninteractive \
installation valid only on drive 0."			            	    ;;
       nonint_lun) a="Invalid command line argument.\nNoninteractive \
installation valid only on lun 0."			            	    ;;
         esdicont) a="Only one controller allowed on an ESDI system"	    ;;
       scsi_root1) a="An ST506 controller can't be added to a system with \
a SCSI root disk." 							    ;;
       scsi_root2) a="An ESDI controller can't be added to a system with \
a SCSI root disk." 							    ;;
        esdi_root) a="Additional controllers can't be added to a system with \
an ESDI root disk."							   ;; 	
         esdi_add) a="Can't add an OMTI controller to a ST506 or SCSI system\
		   yet"							    ;;
       st506_disk) a="ST506 controllers support only disks 0 and 1." 	    ;;
        esdi_disk) a="ESDI controllers support only disks 0 and 1." 	    ;;
           no_sed) a="/bin/sed is not installed. Install it and \
 reinvoke mkdev."  							    ;;
        divvy_err) a="divvy could not divide $cdev."			    ;;
         no_mount) a="Cannot mount the read only pipe."			    ;;
      mnt_mounted) a="Cannot mount /mnt. Please umount any devices from /mnt.";;
	  no_mkfs) a="Cannot mkfs read only pipe."			    ;;
	root_fail) a="Cannot determine root hard disk type."		    ;;
     esac
     echo "\nError: $a\nInstallation exiting." 
     cleanup $FAIL
}

get_newtype () { 
	cmd=
	rootmenu=

	if [ "$1" ] 	# special case for ROOT determination
	then
		rootmenu=TRUE
		mesg="What type of disk controller supports the root disk?

	1. ST506 (standard disk support)
	2. OMTI 8620 or OMTI 8627  (ESDI support)
	3. SCSI

Enter 1, 2, 3 "

	else
		echo "\nSystem V Hard Disk Initialization"
		mesg="What type of disk controller will be supporting this disk?

	1. ST506 (standard disk support)
	2. OMTI 8620 or OMTI 8627  (ESDI support)
	3. SCSI

Enter 1, 2, 3 "
	fi

	while [ "$cmd" -ne 1 -a "$cmd" -ne 2 -a "$cmd" -ne 3 ]
	do prompt || { 
			echo "Installation interrupted."
		        cleanup $FAIL
	        }
		case $cmd in
			1|2|3)	:	;;
			*) echo "\nPlease enter 1, 2, 3 (or 'q' to quit)" ;;
		esac
	done
	cont_type=$cmd

	case $cont_type in
		1) newtype=W 	;;
		2) newtype=E	;;
		3) newtype=S    ;;
	esac

	[ "$rootmenu" ] && {
		rootdisk=$newtype
		newtype=
	}
}

# determine the root hard disk type
get_root_type () {
	rootdisk=
	cdev=

	[ -n "$rootinstall" ] && {
		# need to tickle the disk so that driver prints config info
		dparam /dev/rdsk/0s0 > /dev/null 2>&1
		getfromerror
		newtype=$rootdisk	
		return $OK
	}

	# common case: look in /usr/adm/hwconfig for root disk type
	[ -s "/usr/adm/hwconfig" ] && {
	if grep "disk" /usr/adm/hwconfig > /dev/null 2>&1
	then
		d=`sed -n "/disk/s/.*\(type=.\).*/\1/p" /usr/adm/hwconfig`

		case $d in
			type=S*) rootdisk=S  	 ;;
			type=E*) rootdisk=E 	 ;;
			type=W*) rootdisk=W 	 ;;
		esac
		return $OK
	fi
	}

	# adding a hard disk before ever going multiuser.
	dparam /dev/rdsk/0s0 > /dev/null 2>&1
	getfromerror
	return $OK
}

# during root install /dev/error has not yet been written out 
# so we need to look there directly
getfromerror() {
	while :
	do	disktype=`line < /dev/error`
		case $disktype in
			*disk*)	break  ;;
		esac
	done

	case $disktype in
		*type=S*)	rootdisk=S ;;
		*type=E*)	rootdisk=E ;;
		*type=W*)	rootdisk=W ;;
	esac
}

	
# OMTI 862[07] controllers support both ESDI and ST506 interface
omtimenu () {
	mesg="What interface will this hard disk use? 

	1) ESDI						 
	2) ST506					 

Enter 1 or 2 "

	while :
	do prompt || { 
			echo "Installation interrupted."
		        cleanup $FAIL
	        }
		case $cmd in
			1) newtype=E				         ;;
			2) newtype=W 				         ;;
			*) echo "\nPlease enter 1 or 2 (or 'q' to quit)" ;;
		esac
	return $OK
	done
}

# get host adapter driver, host adapter number, ID number, and LUN
get_numbers () {

    grep -v "^#" ${SADAPTER} | \
		pr -n | \
		grep "^ " > ${TMPFILE}
    awk -F \" '{print "\t\t"NR".  " $2}' ${TMPFILE} > ${TMPFILE2}

    while echo "${CLEAR}

	SCSI ${devdscr} Configuration
"
    do
   	echo " 
	The following host adapter types are currently 
	supported on this system:
"
	cat ${TMPFILE2}
	if [ "${action}" = "add" ]
	then
		echo "
	Enter the number corresponding to the host adapter
	type to which you wish to attach the hard disk, or 
	type 'q' to quit:  \c"
	else
		echo "
	Enter the number corresponding to the host adapter
	type from which you wish to detach the hard disk, or 
	type 'q' to quit:  \c"
	fi

	read x
		
	case ${x} in
		q|Q)	cleanup $FAIL 
			break
			;;
		*)	ha_entry=`grep "^[ ]*${x}" ${TMPFILE}` 
			if [ $? -ne 0 -o "${x}" = "" ]
			then
				echo "
	Invalid selection.  Please try again."
		 		sleep 2
		 		${CLEAR}
			else
				set ${ha_entry}
				HATYPE=$2
				CHANS_PER_BOARD=$3
				BRDS_PER_SYSTEM=$4
				shift 4
				DESCRIPTION=`echo $* | sed "s/\"//g"`
				HA_MAX=`expr ${CHANS_PER_BOARD} \* ${BRDS_PER_SYSTEM}`
				break
			fi
			;;
	esac
    done

    # Don't prompt for LUN number, it is always 0 for now.
    LUN=0

    HA=0
    if [ ${HA_MAX} -gt 1 ]
    then
    	while echo "${CLEAR}

	SCSI ${devdscr} Configuration


	Logical Unit Number (LUN):	${LUN}
	Host Adapter Type:		`eval echo ${DESCRIPTION}`




	The ${DESCRIPTION} SCSI interface supports ${HA_MAX} host adapters,
	numbered 0-`expr ${HA_MAX} \- 1`.
"
	if [ "${action}" = "add" ]
	then
		echo "
	Enter the SCSI host adapter number to which you 
	wish to attach the hard disk or type 'q' to quit:  \c"
	else
		echo "
	Enter the SCSI host adapter number from which you 
	wish to detach the hard disk or type 'q' to quit:  \c"
	fi
	do
		read HA
		case ${HA} in
			q|Q)	cleanup $FAIL
				;;
		      [0-9])	if [ "${HA}" != "" -a ${HA} -ge 0 -a \
					${HA} -lt ${HA_MAX} ]
				then
					break
				else
					echo "
	Invalid selection.  Please try again."
		 			sleep 2
				fi
				;;
			  *)	echo "
	Invalid selection.  Please try again."
		 		sleep 2
				;;
		esac
	done
    fi

    while echo "${CLEAR}

	SCSI ${devdscr} Configuration


	Logical Unit Number (LUN):	${LUN}
	Host Adapter Type:		`eval echo ${DESCRIPTION}`
	Host Adapter Number:		${HA}



	Each host adapter supports 7 SCSI controllers,
	numbered 0-6.
"
	if [ "${action}" = "add" ]
	then
		echo "
	Enter the SCSI controller ID to which you wish
	to attach the hard disk or type 'q' to quit:  \c"
	else
		echo "
	Enter the SCSI controller ID to which you wish to
	detach the hard disk from or type 'q' to quit:  \c"
	fi
    do
	read ID
	case ${ID} in
		q|Q)	cleanup $FAIL
			;;
	      [0-9]) 	if [ "${ID}" != "" -a "${ID}" -ge 0 \
				-a "${ID}" -le 6 ]
			then
				break
			else
				echo "
	Invalid selection.  Please try again."
			 	sleep 2
			fi
			;;
		*)	echo "
	Invalid selection.  Please try again."
			sleep 2
			;;
	esac
    done

}


# this function used when adding ST506 or ESDI disks
get_drivenum () {
	while [ "$drivenum" != "0" -a "$drivenum" != "1" ]
	do
	   echo "\nWill this disk be the first or second disk on this controller?"
	   echo "Enter 1 (first) or 2 (second): \c"
	 	 
	   read num
	   case $num in
		1|2) drivenum=`expr $num - 1`	;;
		q|Q) cleanup $FAIL		;;
		  *) echo "Error. Not a valid number. Try again"  ;;
	   esac
	done
}

# this function used only when adding ST506 disks
get_ctrlnum () {
	while [ "$ctrlnum" != "0" -a "$ctrlnum" != "1" ]
	do
	   echo "\nWill this disk attach to the first or second ST506 controller?"
	   echo "Enter 1 (first) or 2 (second): \c"
	 	 
	   read cnum
	   case $cnum in
		1|2) ctrlnum=`expr $cnum - 1` ;;
		q|Q) cleanup $FAIL	      ;;
		  *) echo "Error. Not a valid number. Try again"  ;;
	   esac
	done
}


# convert controller-type args to numerical values
convert_arg () {
	 case $rootdisk in
		E) [ "$ctrlnum" = "OMTI-0" ] && ctrlnum=0 || error esdi_root
		   ;;
		W) case $ctrlnum in
	  		ST506-0)  ctrlnum=0      ;;
	   		ST506-1)  ctrlnum=1      ;;
	       		 SCSI-0)  ctrlnum=2      ;;
	       		 SCSI-1)  ctrlnum=3      ;;
		 	 OMTI-0)  error esdi_add ;;
		   esac
		   ;;
		S) case $ctrlnum in
			         SCSI-0)  ctrlnum=0        ;;
			         SCSI-1)  ctrlnum=1        ;;
		        ST506-0|ST506-1)  error scsi_root1 ;;
			         OMTI-0)  error esdi_add   ;;
		   esac
		   ;;
	esac
}

valid_combo () {
	case $rootdisk in
	       E) [ "$newtype" = "W" -o "$newtype" = "S" ] && error esdi_root ;;
	       W) [ "$newtype" = "E" ] && error esdi_add  ;;
	       S) case $newtype in
			E) error scsi_root1  ;;
			W) error scsi_root2  ;;
			S) :		     ;;
	          esac
		  ;;
        esac
}
	
check_disktype () {
	case $ctrlnum in
		ST506-0|ST506-1|OMTI-0|SCSI-0|SCSI-1) convert_arg ;;
					    *)    ;;
	esac

	case $ctrlnum in
	0) case $drivenum in
		1)	case $rootdisk in
			E)	omtimenu	
				newtype=E
				major=1
				physical=1		         ;;
			W)	newtype=W	
				major=1
				physical=1			 ;;
			S)	case $newtype in
 					W) error scsi_root1 ;;
					E) error scsi_root2 ;;
					S) newtype=S	    ;;
				esac				;;
			esac 						;;
	 [234567])	case $rootdisk in
				E)	error esdi_disk		;;
				W)	error st506_disk	;;
				S)	newtype=S		;;
			esac					        ;;
	    esac
	    ;;

	# controller number one
	1)  	case $rootdisk in 
		E) error esdi_root			;;
		W) major=54 
		   newtype=W
		   case $drivenum in
		   	0)	physical=0	 ;;
		   	1)	physical=1	 ;;
		   	*)	error st506_disk ;;
		   esac					;;
		S) newtype=S	;;
	    	esac
	    	;;

	2) 	case $rootdisk in
		S|E) error bad_ctlr_arg			    ;;
		  W) # adding a SCSI disk to 1st host adapter
		     newtype=S 				    ;;
		esac
		;;

	3) 	case $rootdisk in
		S|E) error bad_ctlr_arg			    ;;
		  W) # adding a SCSI disk to 2nd host adapter
		     newtype=S 				    ;;
		esac
		;;
	esac
}

add_link () {
	echo "
	Error: The link kit is not completely installed.
	The link kit must be installed to add a SCSI disk."

	getyn "Do you wish to install it now?" || {
		# answered no
		echo "
	SCSI devices cannot be installed without the Link Kit.
	Re-invoke 'mkdev hd' after installing the Link Kit.">&2
			cleanup $FAIL
		}

	# answered yes, so install link kit
	/etc/custom -o -i LINK || {
		# custom exited unsuccessfully
		error "custom failed to install Link Kit successfully.  Please try again."
		cleanup $FAIL
	}
}



# check to see if the kernel is configured for the SCSI disk specified
scsi_config () {

	# adjust HA value to allow for adding SCSI to ST506 system
	if [ ! -n "$no_args" ]
	then
		ID=$id
		LUN=$lun
		HA=$ctrlnum
		HATYPE=$hatype
	fi
	thisentry="$HATYPE	Sdsk	$HA	$ID	$LUN"

	set `scsinfo -q`
	while true
	do
		if [ "$1" = "" ]
		then
			break
		fi
		devname=$1
		scsindex=$2
		major=$3
		minor=$4
		slot=$5
		hatype=$6
		ha=$7
		id=$8
		lun=$9
		if [ ${hatype} = ${HATYPE}	-a \
		     ${ha} = ${HA}		-a \
		     ${id} = ${ID}		-a \
		     ${lun} = ${LUN} ]
		then
			# device is in kernel, but make sure it
			# is not used already
			if [ "${devname}" = "unknown" ]
			then
			    # new entry configured, do dsk initializ.
			    major=`expr 64 + ${scsindex} / 4`
			    drivenum=`expr ${scsindex} \% 4`
			    return 0
			else
			    echo "
	This device is used already by ${devname}.
"
			    cleanup $FAIL
			fi
		fi

		shift 9
	done

	# add new entry to /etc/conf/cf.d/mscsi, via mkdev scsi

	echo "
	Updating SCSI configuration file...
	"
	sleep 2
	export HA ID LUN HATYPE

	/usr/lib/mkdev/.scsi -a -d 

	if [ $? -eq 0 ]
	then
		echo "${CLEAR}

	After the system is rebooted with the new kernel,
	reinvoke 'mkdev hd' to initialize the new SCSI hard 
	disk.  Use the same values for Host Adapter Type, 
	Host Adapter Number, ID, and LUN.  

	If other changes are made to the SCSI devices prior 
	to the rebooting of the kernel, then any previous 
	SCSI device changes may be lost.
"
		cleanup $OK
	fi
	cleanup $FAIL
}


# set minor numbers for use by fix ()
set_minors () {
	physical=$drivenum
	minor_seed=`expr $physical \* 64`

	minor_0=$minor_seed  
	minor_1=`expr $minor_seed + 15` 
	minor_2=`expr $minor_seed + 23` 
	minor_3=`expr $minor_seed + 31` 
	minor_4=`expr $minor_seed + 39` 
	minor_a=`expr $minor_seed + 47` 
	minor_d=`expr $minor_seed + 55` 
	minor_r=`expr $minor_seed + 40` 
	minor_u=`expr $minor_seed + 42` 
}

# set next available disknum from /usr/lib/mkdev/perms/HD
set_disknum () {
	disknum=`expr \`grep -c "/.*sa" /usr/lib/mkdev/perms/HD\` / 2`

	# if this is the first scsi disk, start with "a"
	# Assume that if root disk is SCSI, /dev/rdsk/as0 will exist
	if [ -c /dev/rdsk/as0 ]	
	then
		for i in $diskset
		do
			# Check for root mirror disk which is not in perms file
			if [ -n "$mirror_install" ]
			then
				if [ "$i" = "$mirror_dev" && \
				     -b /dev/hd${mirror_dev}0 ]
				then
					continue  
				fi
			fi

			grep "/rdsk/${i}sa" /usr/lib/mkdev/perms/HD > \
								/dev/null 2>&1
			[ $? -eq 0 ] || {	# letter not used
				disknum=$i
				break
			}
		done
		[ -n "$disknum" ] || {
			echo "\nError! The maximum of 35 SCSI disks are already"
			echo "configured in /usr/lib/mkdev/perms/HD."
			cleanup $FAIL
		}
	else
		disknum=a		# non scsi root, and first scsi disk
	fi
        cdev=/dev/rdsk/${disknum}s0
}


quitnow () {
	echo "${CLEAR}

	During installation you may choose to overwrite 
	all or part of the present contents of your hard 
	disk."

	while	echo "
	Do you wish to continue? (y/n):  \c"
	do
		read x
		case $x in
		[Yy]*)	return $OK	 ;;
		[Nn]*)	echo "
	Aborting initialization procedure.
" 
		        cleanup $FAIL	 ;;
		*)	echo "
	Please enter either 'y' or 'n'." ;;
		esac
	done
}

# add entries to permlist and run fixperm
fix ()  {
	cp /usr/lib/mkdev/perms/HD /usr/lib/mkdev/perms/HD.00
	fixdone=TRUE

	if [ -n "$rootinstall" ]
	then echotext="#	
#
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s0	${major}/${minor_0} 
						./dev/hd${disknum}0
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s1	${major}/${minor_1}
						./dev/hd${disknum}1
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s2	${major}/${minor_2} 
						./dev/hd${disknum}2
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s3	${major}/${minor_3}
						./dev/hd${disknum}3
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s4	${major}/${minor_4} 
						./dev/hd${disknum}4
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}sa	${major}/${minor_a} 
						./dev/hd${disknum}a
${newtype}$disknum	b640	dos/sysinfo	2	./dev/dsk/${disknum}sd	${major}/${minor_d} 
						./dev/hd${disknum}d
${newtype}$disknum	b600	sysinfo/sysinfo	1	./dev/hd${disknum}root	${major}/${minor_r} 
${newtype}$disknum	b600	sysinfo/sysinfo	1	./dev/hd${disknum}usr	${major}/${minor_u} 
#
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s0	${major}/${minor_0} 
						./dev/rhd${disknum}0
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s1	${major}/${minor_1} 
						./dev/rhd${disknum}1
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s2	${major}/${minor_2} 
						./dev/rhd${disknum}2
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s3	${major}/${minor_3} 
						./dev/rhd${disknum}3
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s4	${major}/${minor_4} 
						./dev/rhd${disknum}4
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}sa	${major}/${minor_a} 
						./dev/rhd${disknum}a
${newtype}$disknum	c640	dos/sysinfo	2	./dev/rdsk/${disknum}sd	${major}/${minor_d} 
						./dev/rhd${disknum}d
${newtype}$disknum	c600	sysinfo/sysinfo	1	./dev/rhd${disknum}root	${major}/${minor_r} 
${newtype}$disknum	c600	sysinfo/sysinfo	1	./dev/rhd${disknum}usr	${major}/${minor_u} 
#"

	else echotext="#	
#
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s0	${major}/${minor_0} 
						./dev/hd${disknum}0
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s1	${major}/${minor_1}
						./dev/hd${disknum}1
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s2	${major}/${minor_2} 
						./dev/hd${disknum}2
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s3	${major}/${minor_3}
						./dev/hd${disknum}3
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}s4	${major}/${minor_4} 
						./dev/hd${disknum}4
${newtype}$disknum	b600	sysinfo/sysinfo	2	./dev/dsk/${disknum}sa	${major}/${minor_a} 
						./dev/hd${disknum}a
${newtype}$disknum	b640	dos/sysinfo	2	./dev/dsk/${disknum}sd	${major}/${minor_d} 
						./dev/hd${disknum}d
#
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s0	${major}/${minor_0} 
						./dev/rhd${disknum}0
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s1	${major}/${minor_1} 
						./dev/rhd${disknum}1
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s2	${major}/${minor_2} 
						./dev/rhd${disknum}2
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s3	${major}/${minor_3} 
						./dev/rhd${disknum}3
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}s4	${major}/${minor_4} 
						./dev/rhd${disknum}4
${newtype}$disknum	c600	sysinfo/sysinfo	2	./dev/rdsk/${disknum}sa	${major}/${minor_a} 
						./dev/rhd${disknum}a
${newtype}$disknum	c640	dos/sysinfo	2	./dev/rdsk/${disknum}sd	${major}/${minor_d} 
						./dev/rhd${disknum}d
#"
	fi

	echo "$echotext" >> /usr/lib/mkdev/perms/HD
	fixperm -c -d${newtype}${disknum} /usr/lib/mkdev/perms/HD
}

# prevent confusion over selecting 'q' in the middle of the disk installation
utilmsg () {

	echo "${CLEAR}

	The hard disk installation program will now 
	invoke $1.  Entering 'q' at the following menu 
	will exit $1, and the hard disk installation 
	will continue.  If you wish to exit the entire 
	installation at this menu, press the <DEL> key.

	Press return to continue:  \c"
	read a
	echo "${CLEAR}"
}

rundparam () {

	# if SCSI, only root hard disks needs dparam -w
	if [ "$newtype" = "S" ]		
	then
		if [ -n "$firstroot" ]       # first root on system
		then
			if [ -z "$mirror_install" ]
			then
				dparam -w /dev/dsk/as0
			else
				dparam -w /dev/dsk/${mirror_dev}s0
  			fi
		elif [ -n "$rootinstall" ]   # root installation not first one
		then
			dparam -w /dev/dsk/${disknum}s0
		fi
	fi

	# don't run dkinit OMTI; can't change parameters
	[ "$newtype" = "E" ] && [ -n "$rootinstall" ] && dparam -w
		
	# invoke dkinit for ST506 disks
	if [ "$newtype" = "W" ]
	then
		if [ -n "$interactive" ]
		then 	# use dkinit if interactive
			utilmsg "/etc/dkinit"
			dkinit $drivenum $ctrlnum 
		else	# write masterboot out only if noninteractive
			dparam -w
		fi
	fi
}

# run fdisk non-interactively
# WARNING: this will delete every existing NON-DOS virtual drive, 
# then create a single active UNIX virtual drive. 
# Don't try this at home, kids.
batchfdisk() {
	# delete all the pre-existing UNIX virtual drives.
	# This used to delete only partition 1 since fdisk was sorted.
	# However, fdisk now deletes the partition and does not sort.
	for x in 1 2 3 4
	do 	
		if fdisk -p -f $cdev | grep "^$x" | grep UNIX > /dev/null
		then
			fdisk -d $x -f $cdev > /dev/null 2>&1
		fi
	done

	# now create the single, active UNIX virtual drive
	echo "
	Creating UNIX partition..."
	if [ -z "$mirror_install" ]
	then
		# Create the UNIX partition
		fdisk -c 1 - - -f $cdev || return $FAIL

		# Now make the single partition active
		fdisk -a 1 -f $cdev || return $FAIL
		activepart=1
	else
		# Root mirror:
		# Find out the root disk partition configuration
		# and configure the mirror accordingly
		# This assumes that /dev/rhda0 has been pre-selected
		# to contain the smallest UNIX partition those on
		# the mirror components.  Also, an assumption is made
		# that there will be at most one non-UNIX partition
		# on the mirror.

		PRIROOT=/dev/rhda0	# "primary" disk
		PARTNO=1		# default UNIX partition number
		START=1

		# Create the UNIX partition on the new disk based
		# on the root drive active UNIX partition offset and
		# size.

		fdisk -p -f $PRIROOT | grep Active | grep UNIX | \
		while read PPART PSTART PSIZE JUNK 2>/dev/null
		do
			# Set primary partition number
			PRIPART="$PPART"

			# See if non-UNIX partitions exist on the new disk
			# and if so, whether we should create the new UNIX
			# partition with a number of other than 1 to avoid
			# conflict with the existing non-UNIX partition.
			# Also, compute location to lay down the new
			# partition.

			NUM_NUNIX=`fdisk -p -f $cdev | grep -v UNIX | \
				wc -l 2>/dev/null`
			if [ $NUM_NUNIX -gt 1 ]
			then
				echo '
	Cannot install on a mirror root disk with more than one
	pre-configured non-UNIX partition.  You may want to use
	the fdisk utility under the appropriate non-UNIX OS to
	delete these partitions before trying to install the disk
	as a mirror root disk again.\n'
				return $FAIL
			fi

			fdisk -p -f $cdev | grep -v UNIX | \
			while read MPART MSTART MSIZE JUNK 2>/dev/null
			do
				if [ "$MPART" -eq $PARTNO ]
				then
					PARTNO=`expr $PARTNO + 1`
					if [ $PARTNO -gt 4 ]
					then
						return $FAIL
					fi
				fi

				if [ "$MSTART" -gt "$PSIZE" ]
				then
					# UNIX partition can fit before
					# the non-UNIX partition.
					START=1
				else
					# Put UNIX partition after non-DOS
					# partition.
					START="$MSIZE"
				fi

			done
			# Create the partition
			fdisk -c $PARTNO $START $PSIZE -f $cdev
			if [ $? -ne 0 ]
			then
				return $FAIL
			fi

			# Now make the single partition active
			fdisk -a $PARTNO -f $cdev
			if [ $? -ne 0 ]
			then
				return $FAIL
			fi
		done

		activepart=`fdisk -p -f $cdev | \
				grep Active | awk '{ print $1 }' 2>/dev/null`
		if [ -z "$activepart" ]
		then
			activepart=$PARTNO
		fi
	fi

	# return and let the calling statement know everything is OK
	return $OK
}

# run badtrk non-interactively
# This will use a destructive, thorough scan by default, and
# will reserve N tracks for future badtracks
# argument indicates that actual badtracking is to be done 
# (not just make space for future badtracks)
batchbadtrk() {
	badtrkargs="-e"
	[ "$1" = "FULL" ] && {
	    badtrkargs="-e -s td"
	    echo "Scanning disk..."
	}

	# get the number of tracks in the virtual drive
	# we will use roughly 8 percent of the total disk
	set `fdisk -p`
	maxtracks=`expr $4 / $diskpercent`

	# do the non-interactive scan
	badtrk $badtrkargs -m $maxtracks -f $actdev || return $FAIL

	return $OK
}




# Goes through all fdisk partitions and executes divvy to create only
# the names for the valid division.  
add_nondestr() {
	echo "${CLEAR}

	SCSI ${devdscr} Configuration


	Logical Unit Number (LUN):	${LUN}
	Host Adapter Type:		`eval echo ${DESCRIPTION}`
	Host Adapter Number:		${HA}
	SCSI Controller ID:		${ID}
"
	# First time accessing disk.  Check to see if its there.
	fdisk -p -f $cdev > /dev/null 2>&1 || cleanup fdisk_err

	numrun=`fdisk -p -f $cdev | grep -c "UNIX"`
	if [ $numrun -le 1 ]
	then
		partition_str="partition exists"
		time_str="time"
	else
		partition_str="partitions exist"
		time_str="times"
	fi
	echo "
	$numrun UNIX $partition_str on this disk so
	'divvy' will be executed $numrun $time_str."

	div_list=`fdisk -p -f $cdev | awk '$5 == "UNIX" { print $1 }'`
	for i in $div_list
	do
		echo "
	Creating device names for partition $i ...
"
		if [ -n "$scsiflg" ] 
		then
			divvy -s $scsindex -l $disknum -v $i -u
		else	# ST506 + esdi
			divvy -b $major -c $major -p $physical -v $i -u
		fi

		# If divvy fails, then no valid division table
		[ $? -ne 0 ] && {
			getyn "Do you wish to create any mountable filesystems\n\ton partition $i?" && {
			    if [ -n "$scsiflg" ] 
			    then
				divvy -s $scsindex -l $disknum -v $i -m
			    else	# ST506 + esdi
				divvy -b $major -c $major -p $physical -v $i -m
			    fi
			}
		}
	done
}




# Does the fdisk and, if necessary, does the badtrk as well
diskprep () {

	# if a SCSI disk, only run fdisk
	[ "$newtype" = "S" ] &&  {
		if [ -z "$mirror_install" ]
		then
			cdev=${cdev:-/dev/rdsk/as0}
		elif [ -z "$cdev" ]
		then
			cdev=/dev/rdsk/${mirror_dev}s0
		fi
		if [ -n "$interactive" ]
		then	# do the interactive fdisk
			utilmsg "$tmpdir/etc/fdisk"
			$tmpdir/etc/fdisk -f $cdev || cleanup fdisk_err
		    	getactive=`fdisk -p -f $cdev`
			while [ 0 -eq `expr "$getactive" : '.*UNIX[ 	]*Active.*'` ] && getyn "An active UNIX partition must be specified.\n\tDo you wish to continue?"
			do	$tmpdir/etc/fdisk -f $cdev || cleanup fdisk_err
			    	getactive=`fdisk -p -f $cdev`
			done
			# Check if active given
			getactive=`fdisk -p -f $cdev`
			[ 0 -eq `expr "$getactive" : '.*UNIX[ 	]*Active.*'` ] && cleanup $FAIL
			set `fdisk -p -f $cdev | grep Active`
			activepart=$1
		else	# do the non interactive fdisk
			batchfdisk || cleanup batch_fdisk
		fi
		return $OK
	}

	# set the character device of the disk we are interested in
	cdev=${cdev:-/dev/rdsk/0s0}

	# $disknum is set in check_disktype()
	actdev=/dev/rdsk/${disknum:-0}sa

	# if on an OMTI controller, run fdisk and create space for badtrks
	[ "$newtype" = "E" ] &&  {
		if [ -n "$interactive" ]
		then	# do the interactive fdisk
			utilmsg "/etc/fdisk"
			/etc/fdisk -f $cdev || cleanup fdisk_err
		    	getactive=`fdisk -p -f $cdev`
			while [ 0 -eq `expr "$getactive" : '.*UNIX[ 	]*Active.*'` ] && getyn "Do you wish to specify an active UNIX partition?"
			do	/etc/fdisk -f $cdev || cleanup fdisk_err
			    	getactive=`fdisk -p -f $cdev`
			done
		else	# do the non interactive fdisk
			batchfdisk || cleanup batch_fdisk
		fi
		batchbadtrk	# no args = don't do it, just make space
		return $OK
	}

	# Badtrack ST506 disks. 

	# give this message on interactive use only
	[ -n "$interactive" ] && {
		echo "
	The hard disk installation program will now invoke 
	two disk preparation utilities: fdisk and badtrk. 
	Selecting 'q' at the main menu for each utility will 
	exit that utility and continue with the hard disk 
	installation.

	To exit the entire installation at these menus, 
	press the <DEL> key.
"
	}

	# Partition the disk and badtrk the active virtual drive
	if [ -n "$interactive" ] 
	then 	# Loop until the user has partitioned around bad tracks
		until	
			/etc/fdisk -f $cdev || cleanup fdisk_err
		    	getactive=`fdisk -p -f $cdev`
			while [ 0 -eq `expr "$getactive" : '.*UNIX[ 	]*Active.*'` ] && getyn "Do you wish to specify an active UNIX partition?"
			do	/etc/fdisk -f $cdev || cleanup fdisk_err
			    	getactive=`fdisk -p -f $cdev`
			done
			/etc/badtrk -e -f $actdev
		do
			echo "
	Please re-partition your disk, creating the active
	UNIX partition after the bad area on the disk.
"
		done
	else	# do the non-interactive fdisk and badtrk
		batchfdisk || cleanup batch_fdisk
		batchbadtrk FULL || {
			echo "
	Reboot the system and restart the installation. 
	At the prompt, choose the interactive hard disk 
	initialization.

	Please re-partition your disk, creating the 
	active UNIX partition after the bad area on the 
	disk.

	Exiting...
" >&2
			cleanup $FAIL
		}
	fi
}

rundivvy () {
	divflags="-v $activepart -m"
	divopts="-b $major -c $major -p $physical"	# for nonscsi

	if [ -n "$rootinstall" ] 
	then
		# set scsindex on first install: assigned on
		# a chronological basis.
		[ -n "$firstroot" ] && {
			if [ -z "$mirror_install" ]
			then
				scsindex=0
			else
				index=1
				for i in $diskset
				do
					if [ "$i" = "$mirror_dev" ]
					then
						scsindex=$index
						break
					fi
					index=`expr $index + 1`
				done
				if [ -z "$scsindex" ]
				then
					echo "Invalid mirror device \"$mirror_dev\""
					cleanup $FAIL
				fi
			fi
		}
		# set the divvy root install flag
		divflags="-i"
		# set the divvy non-interactive flag here
		[ -n "$interactive" ] || divflags="$divflags -n"
		if [ -n "$scsiflg" ] 
		then
			if [ -z "$mirror_install" ]
			then
				divopts="-s $scsindex -l a"
			else
				divopts="-s ${scsindex} -l ${mirror_dev}"
			fi
		fi
	elif [ -n "$scsiflg" ]
	then
		divopts="-s $scsindex -l $disknum"
	fi

	[ -n "$interactive" ] || echo "
Dividing UNIX partition into filesystem and swap divisions..."
	
	# invoke divvy
	if [ -z "$mirror_install" ]
	then
		$tmpdir/etc/divvy $divopts $divflags
		if [ $? -ne 0 ]
		then
			error divvy_err
		fi
	else
		DIVFILE=$tmpdev/mirror.divvy

		dlayout -hd "$PRIPART" "$PRIROOT" | \
			awk '{ printf("%d\t%d\t%d\n", $2, $3, $4) }' >$DIVFILE

		$tmpdir/etc/divvy $divopts $divflags -f $DIVFILE
		rm -f $DIVFILE
	fi
}

# usage and help
usage () {
	echo "
Usage: $progname scsi_id ha_num lun ha_type [-n] [-i] [-u]
		-n initialize the disk non-interactively using system defaults.
		-i install a bootable root disk
		-u add a disk nondestructively
"
        cleanup $OK
}


boothd () {
# copy out the partition boot programs
    if [ -n "$firstroot" ]
    then
	if [ -z "$mirror_install" ]
	then
		dd if=$tmpdir/etc/hdboot0 of=/dev/hdaa 2>/dev/null
		dd if=$tmpdir/etc/hdboot1 of=/dev/hdaa bs=1k seek=1 2>/dev/null
	else
		dd if=$tmpdir/etc/hdboot0 of=/dev/hd${mirror_dev}a 2>/dev/null
		dd if=$tmpdir/etc/hdboot1 of=/dev/hd${mirror_dev}a \
			bs=1k seek=1 2>/dev/null
	fi
    else
	dd if=/etc/hdboot0 of=$tmpdev/dev/hd${disknum}a 2>/dev/null
	dd if=/etc/hdboot1 of=$tmpdev/dev/hd${disknum}a bs=1k seek=1 \
		2>/dev/null
    fi
}




removedsk() {
# Prompt for disk to remove and then call hdutil
# Warning banner
echo "${CLEAR}

			W A R N I N G

	Once a hard disk is removed, it will not be possible to 
	access the disk without running \"mkdev hd\" to add the 
	disk again.  If the data on the disk is to be used again, 
	choose option 2 of \"mkdev hd\" to non-destructively add 
	the hard disk; choosing option 1 will re-initialize the 
	hard disk, causing all data on the disk to be lost!

	This routine will remove all device nodes referring to 
	this disk and will remove all references to this disk in 
	the following files:

		/etc/default/filesys
		/etc/checklist
		/usr/lib/mkdev/perms/HD"

	getyn "Do you wish to continue?" || return $FAIL

	echo "${CLEAR}

	If the device nodes exist for the hard disk to be
	removed, you can simply enter the letter corresponding 
	to the hard disk (i.e. 'c' for /dev/hdc0); otherwise, 
	you will be required to enter the host adapter type, 
	host adapter number, and SCSI controller ID.
"


	while true
	do
		echo "
	Enter the disk to remove (b-z, A-I) if the device
	nodes exist, or press return otherwise:  \c"

		read dsk

		case "$dsk" in

		    [b-z,A-I])	break
				;;

		    "")		/usr/lib/mkdev/.scsi -r -d
				return $OK
				;;

		    "exit")	return $OK
				;;

		    *)		echo "
	Invalid drive!
"
				sleep 2
				continue
				;;
		esac
	done

	HATYPE=
	HA=
	ID=
	LUN=
	# Get host adapter type, host adapter number, and SCSI id
	scsinfo -q | grep hd${cmd}0 > ${TMPFILE3}
	if [ $? -eq 0 ]
	then
		set `cat ${TMPFILE3}`
		HATYPE=$6
		HA=$7
		ID=$8
		LUN=$9
		export HATYPE HA ID LUN
	fi

	q=q
	if [ -b /dev/hd${dsk}0 ]
	then
		echo "${CLEAR}

	These are the devices to be removed:"
		hdutil -vs $dsk || {
			echo "
	Disk '${dsk}' not removed.
"
			return $FAIL
		}
		getyn "Do you wish to remove these devices?" || {
			echo "
	Disk '${dsk}' not removed.
"
			return $FAIL
		}
		echo "${CLEAR}"
		hdutil -vd $dsk || return $FAIL
	else
		echo "${CLEAR}

	Device nodes for disk '${dsk}' does not exist.
"
	fi

	if [ "$HATYPE" != "" ]
	then
		# Remove the hard disk entry from mscsi
		echo "

	Removing the hard disk entry from the SCSI 
	Configuration file...
	"
		/usr/lib/mkdev/.scsi -r -d 
	fi

	return $OK
}





#### begin main ####

# parse command line args
case "$#" in
	  0) no_args=true
	     ;;
	  1) case $1 in
		-u) usage 		;;
		 *) error bad_arg	;;
	     esac
	     ;;
	2|3) echo "Non-SCSI disks not supported at this time."
	     error bad_arg

	     # make sure the drivenum and ctrlnum args are legal
	     [ $1 -ge $dmin -a $1 -le $dmax ] && drivenum=$1 || error bad_arg
	     if [ "$2" -ge $cmin -a "$2" -le $cmax ] 
	     then
	           ctrlnum=$2  
	     else
	           case $2 in
			ST506-0|ST506-1|SCSI-0|SCSI-1|OMTI-0) ctrlnum=$2    ;;
					             *) error bad_arg ;;
		   esac
	     fi

	     # process third argument
	     # check if there is a -n flag passed in
	     if [ -n "$3" ]
	     then 

	     case $3 in
		   -n)  case $ctrlnum in
			ST506-0|OMTI-0|SCSI-0|SCSI-1|0) ctrlnum=0 ;;
			                      *) error nonint_ctrl ;;
		        esac

			if [ "$drivenum" = "$dmin" ] 
			then
    		        	# unset the interactive variable
				interactive=
			else
				error nonint_drv
			fi
		        ;;
	        [0-7])  # logical unit number for SCSI disks
		        lun=$3		
			;;
		    *)  error bad_arg
	 	        ;;
	     esac
	     fi

	     # set the traps
	     settrap
	     ;;
    4|5|6|7)   newtype=S
	     scsiflg=TRUE
	     argcount=1
	     while [ $argcount -le 7 ]
	     do
		case $argcount in
		    1) # get scsi id
		       [ $1 -ge $smin -a $1 -le $smax ]&& id=$1 || error bad_arg
		       ;;
		    2) # get host adapter
		       ctrlnum=$1 
		       ;;
		    3) # get logical unit
		       [ $1 -ge 0 -a $1 -le 7 ] && lun=$1 || error bad_arg
		       ;;
		    4) # get scsi driver
		       hatype=$1
		       ;;
		  5|6|7) # get options
		       [ -z "$1" ] && break	# no more arguments
		       case $1 in
			   -n) interactive= 	;;
			   -u) nondestruct=TRUE	;;	# mknodes only for hd
			   -i) rootinstall=TRUE	;;	# not first root
			   -I) rootinstall=TRUE		# install from floppy
			       firstroot=TRUE
			       tmpdir="/mountram"
			       tmpdev="/tmp"
			       ;;
			   -M*) mirror_install=TRUE
				mirror_dev=`echo $1 | awk '{
					printf("%s", substr($1, 3, 1))
				}' - 2>/dev/null`
				if [ -z "$mirror_dev" ]
				then
					error bad_arg
				fi
			       ;;
			    *) error bad_arg	;;
			esac
			;;
		esac
		shift
		argcount=`expr $argcount + 1`
	     done
	     [ -n "$nondestruct" -a -n "$rootinstall" ] && error bad_arg

	     # check host adapter driver and ha number
	     case $hatype in
		  ti) [ $ctrlnum -eq 0 ] || error bad_ti	;;
 	        hpfp) [ $ctrlnum -ge 0 -a $ctrlnum -le 3 ] || error bad_ad  ;;
	          ad) [ $ctrlnum -ge 0 -a $ctrlnum -le 1 ] || error bad_ad  ;;
		   *) error bad_arg
	     esac
	     # set the traps
	     settrap
	     ;;
	*)   error bad_arg  
	     ;;	
esac

scsiflg=TRUE		# Only Scsi disks supported now

# program flow for installation of root hard disk from floppy
[ -n "$firstroot" ] && {
	# set path to include extra files
	  PATH=$PATH:/mountram/etc:/mountram/bin:/mountram/usr/bin
	# figure out hard disk type
	# get_root_type
	  [ -n "$scsiflg" ] && newtype=S	# Scsi only right now
	# chance to bail out
	  [ "$interactive" ] && quitnow
	# run dparam; invoke dkinit if it's an ST506 disk
	  rundparam
	# fdisk for all, and badtrack for ST506 disks
	  diskprep

	# divide the disk into file systems
	  rundivvy 

	# copy boot sector out to active partition 
	  boothd

	  cleanup $OK
}

# program flow for installation of all disks except first root hard disk
	# if no command line args given, prompt whether to remove a disk
	# 	or to add a disk by continuing.
	  [ "$no_args" ] && {
		
		while mesg="${CLEAR}

	SCSI Hard Disk Configuration


	1.  Install a Hard Disk
	2.  Create Interface to Existing Hard Disk
	3.  Remove a Hard Disk

	Choose '1' if your hard disk has never been used or 
	you wish to change the layout of the disk.  This 
	option will write over the content of the hard disk.

	Choose '2' if your hard disk has previously been 
	installed and you wish to set up the system to
	recognize the hard disk and recreate its device 
	nodes.  This option will non-destructively add your
	hard disk.

	Choose '3' if you want to remove the system
	interface to the hard disk.

	Enter your selection or type 'q' to quit:  \c"
		do prompt || exit $FAIL
			select=$cmd
			case $select in
			1) action=add		# Proceed to adding disk
			   break
			   ;;
			2) nondestruct=TRUE	# Non-destructive add
			   action=add
			   break		
			   ;;
			3) removedsk || exit $FAIL
			   exit $OK 		
			   ;;
			*) echo "
	Invalid selection.  Please try again."
		 	   sleep 2
	   		   continue		;;
			esac
		done
	  }
	# make sure sed is installed for use by this script
	  [ -x "/bin/sed" ] || error no_sed
	# check that /mnt is not mounted for use in divvy
	if `mount 2> /dev/null | grep "/mnt " > /dev/null 2>&1`
	then
		error mnt_mounted
	fi
	# if no command line args given, prompt for disk type
	  [ "$no_args" ] && cdev=	# get_newtype
	# determine root hard disk type
	# get_root_type
	  [ -n "$scsiflg" ] && newtype=S	# Scsi only right now
	# if no command line args given, get configuration info
	  [ "$no_args" ] && {
	# Future releases need protection against ST506 and OMTI?
	# valid_combo
	  case $newtype in
		W) get_drivenum
		   get_ctrlnum    ;;
		E) get_drivenum
		   ctrlnum=0 	  ;;
		S) get_numbers    ;;
	  esac
	  }
	# verify that adding this type of disk to this system is supported
	# check_disktype 
	# set traps, no matter what
	  settrap

	# if SCSI, verify that SCSI kernel configuration includes this disk
	[ "$newtype" = "S" ] && scsi_config   # get major number of SCSI disk 

	# set minor numbers
	  set_minors

	# determine disk number for use in perms list
	  set_disknum

	# last chance to bail out
	  [ -n "$nondestruct" ] || quitnow

	# create devices using fixperm & /usr/lib/mkdev/perms/HD
	  fix

	# get disk parameters; if ST506/ESDI disk, call dkinit
	  rundparam

	if [ -n "$nondestruct" ]
	then
		# go through fdisk partitions and execute divvy on all unix ones
		add_nondestr
	else
		# fdisk for all, and badtrack for ST506/ESDI disk
	  	diskprep
		# divide the disk into file systems
	  	rundivvy
	fi

	# copy boot sector out to active partition if installing root
	  [ -n "$rootinstall" ] && {
		boothd
		echo "
	To access the new:

	\"root\" filesystem, use /dev/hd${disknum}root
	\"usr\" filesystem, use /dev/hd${disknum}usr
"
	  }
	  echo "${CLEAR}

	Hard disk initialization for /dev/hd${disknum}0 completed.
"
	  cleanup $OK
