:
#	@(#) hd.sh 25.1.1.1 93/03/09 
#
#	Copyright (C) 1987-1993 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	mkdev/hd - Hard Disk Configuration
#

. /usr/lib/sh/std_funcs

# set umask
umask 0

#Hooks for IDA Driver
#disk type 'I' indicates an IDA driver
IDASCRIPT=/usr/lib/mkdev/ida

#pick up 'mkdev hd' back-end functions
. /usr/lib/mkdev/.hdfuncs 

# 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 	trap 'error "Installation interrupted."
		cleanup $FAIL'	1 2 3 15
	else	trap 'cleanup $FAIL' 1 2 3 15
	fi
}

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

get_args () { 

	case ${rootdisk}$arch in

	WAT|WMC|IAT)
	   case ${rootdisk} in
	   W)
	   	echo "\nYour root hard disk is attached to an ST506 controller."
		;;
	   I)
		echo "\nYour root hard disk is attached to an IDA controller."
		;;
	   esac

	   mesg="Pick one of the choices below or you may quit and 
invoke mkdev hd -u for a detailed usage message.

		1) Add a hard disk to ST506 controller
		2) Add a hard disk to SCSI controller
		3) Add a hard disk to an IDA controller (EISA).\n
Enter 1, 2, 3 "

	   while :
	   do prompt || { 
		        cleanup $FAIL
	        	}
		case $cmd in
			1) newtype=W
			   case $arch in
			   AT)	get_drivenum
				get_ctrlnum	;;
			   MC)	drivenum=1
				ctrlnum=0
				major=1		;;
			   esac
			   break;;
			2) newtype=S
			   break;;
			3) exec $IDASCRIPT 
			   echo "\nPlease enter 1 or 2 (or 'q' to quit)";;
			*) echo "\nPlease enter 1 or 2 (or 'q' to quit)";;
		esac
	   done
	   ;;

	EAT)
	   echo "\nYour root hard disk is attached to an OMTI controller."
	   if getyn "The only available choice is to add one other hard disk.\n\
Enter 'y' to add another disk.  If you enter 'n' you will exit\nthis \
script.  You may then invoke mkdev hd -u for a detailed\nusage message."
	   then
		newtype=E
		drivenum=1
		ctrlnum=0
		major=1
	   else
		cleanup $FAIL
	   fi
	   ;;

	EMC)
	   echo "\nYour root hard disk is attached to an ESDI controller."
	   mesg="Pick one of the choices below or you may quit and 
invoke mkdev hd -u for a detailed usage message.

		1) Add a hard disk to ESDI controller
		2) Add a hard disk to SCSI controller

Enter 1, 2 "

	   while :
	   do prompt || { 
		        cleanup $FAIL
	        	}
		case $cmd in
			1) newtype=E
			   drivenum=1
			   ctrlnum=0
			   major=1
			   break;;
			2) newtype=S
			   break;;
			*) echo "\nPlease enter 1 or 2 (or 'q' to quit)";;
		esac
	   done
	   ;;

	SAT|SMC)
	   echo "\nYour rootdisk is attached to a SCSI controller."
	   if getyn "The only available choice is to add another SCSI disk.\n\
Do you want to add another SCSI disk?"
	   then
		newtype=S
	   else
		cleanup $FAIL
	   fi
	   ;;
	esac
}

# 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) ctrlnum=`expr $cnum - 1`
		     major=1  		      ;;
		  2) ctrlnum=`expr $cnum - 1`
		     major=54  		      ;;
		q|Q) cleanup $FAIL	      ;;
		  *) echo "Error. Not a valid number. Try again"  ;;
	   esac
	done
}


scsi_config () {
	#check that disk if configured and add if necessary to mscsi
	#.scsi writes to filedes 3: 1) SCSI index to use and 2) 'a' if added
/bin/sh /usr/lib/mkdev/.scsi -n -a -C -d $HA $ID $LUN $HATYPE 3>${tmp}4.$$
	scsi_ret=$?
	set -- `cat ${tmp}4.$$`
	scsi_index=$1
	[ "$scsi_index" -eq -1 ] && error scsi_noconfig
	#was it already configured?
	[ "$2" != "added" ] && return 0
	[ $scsi_ret = 0 ] && {
			relink=`expr $relink + 1 `
			return 0
	}
	rm -f ${tmp}4.$$
	cleanup $FAIL	
}

# get the major number associated with the scsi entry 
scsi_major () {
	if [ "$rootdisk" = "S" ]
	then
		driver=hd
	else
		driver=Sdsk
	fi
	driver_to_major
}

# usage and help
usage () {
	echo "
Usage: $0 drivenum ctrlnum [ lun [ hatype ] ] [-nhu ]

	-u	display this usage message

	-h	display usage message for combining disk interfaces

	-n	means initialize the disk non-interactively using 
		system defaults. This may only be done when installing 
		the root hard disk (when drivenum is 0 and ctrlnum is 0; 
		for SCSI disks, lun is not specified and defaults to 0)

The arguments 'drivenum', 'ctrlnum' and 'lun' are dependent on the type 
of hard disk being installed.  The arguments 'lun' and 'hatype' apply
to SCSI disks only.

SCO System V supports four different hard disk interfaces --
ST506 (standard), ESDI, IDA (EISA), and SCSI.  
Following is a section describing installation of each of these.
Please note: ST506 and SCSI interfaces may be combined on the same system.  
             ESDI and SCSI interfaces may also be combined on an MCA system.
To view a guide to selecting mkdev hd arguments when combining these
interfaces, enter \"mkdev hd -h\" at a shell prompt."

echo "\nPress return to continue: \c"
read x

echo "\n
Installation of a disk on to an ST506 controller 
------------------------------------------------
drivenum	is the number of the disk drive.  The first disk on a 
		controller is disk 0; the second disk is disk 1.

ctrlnum		is the controller number. Two controllers are supported.
		The first controller is ctrlnum 0. It may also be referred
		to as \"ST506-0\".  The second controller is ctrlnum 1.
		It may be also be referred to as \"ST506-1\".  On an MCA
		system, only the first controller \"ST506-0\" is supported.

ST506 disks will install with one of the following commands:

mkdev hd 0 0 (or ST506-0)  for the first disk on first controller (root)
mkdev hd 1 0 (or ST506-0)  for the second disk on first controller
mkdev hd 0 1 (or ST506-1)  for the first disk on second controller (not MCA)
mkdev hd 1 1 (or ST506-1)  for the second disk on second controller (not MCA)

NOTE:  Some ST506 controllers may support ESDI interface disks."

echo "\nPress return to continue: \c"
read x

echo "\n
Installation of a disk on to an OMTI or ESDI controller
-------------------------------------------------------
drivenum	is the number of the disk drive.  The first disk on the 
		controller is disk 0. The second disk is disk 1.

ctrlnum		is the controller number. One controller is supported.
		The ctrlnum is always 0. It may be referred to as \"OMTI-0\",
		or as \"ESDI-0\".

ESDI disks will install with one of the following commands:

mkdev hd 0 0 (or OMTI-0, or ESDI-0) for the first disk on the controller (root)
mkdev hd 1 0 (or OMTI-0, or ESDI-0) for the second disk on the controller

NOTE:  Some OMTI controllers may support standard interface disks."

echo "\nPress return to continue: \c"
read x

echo "
Installation of a SCSI disk on to a SCSI bus
--------------------------------------------
drivenum	is the ID number of the controller for this disk.
		The ID number is determined by jumper settings.
		Valid ID numbers are 0-7.

ctrlnum		is the host adapter number. Multiple host adapters are 
		supported.  The first host adapter is \"SCSI-0\". 
		The second host adapter is \"SCSI-1\", etc.

lun		Most SCSI disks are manufactured with a controller
		embedded in the same physical unit as the disk.
		These controllers support only one disk. In this case
		the lun (logical unit number) is 0.  The lun argument
		is optional.  If not specified, it will default to 0.

		If you have a non-embedded controller, you must set the
		lun for each device, via jumper settings. Valid lun
		numbers are 0-7.

hatype		is the type of SCSI host adapter referenced by ctrlnum."

echo "\nPress return to continue: \c"
read x

echo "
		Invoke mkdev hd with the correct numbers as follows:

		mkdev hd [0-7] [SCSI-x] [0-7] [hatype]

		Please note: This program 'mkdev hd' must be invoked twice
		to install a SCSI disk.  The first time, the kernel will be
		reconfigured to support the new disk.  The second time, the
		disk will be initialized.  Use the same mkdev hd arguments
		both times.\n"
	
echo "\nPress return to continue: \c"
read x

echo "\n
Installation of a disk on to a IDA (Intelligent Disk Array) controller 
----------------------------------------------------------------------
drivenum	is the logical number of the disk drive.  The first disk array
		on a controller is disk 0; the second disk is disk 1.

ctrlnum		is the controller number. Six controllers are supported.
		The first controller is ctrlnum 0, the second controller is
		ctrlnum 1 and so on. These may also be referred to as
		\"IDA-0\", \"IDA-1\" and so on.

IDA disks will install with one of the following commands:

mkdev hd 0 0 (or IDA-0)  for the first disk on first controller
mkdev hd 1 0 (or IDA-0)  for the second disk on first controller
mkdev hd 0 1 (or IDA-1)  for the first disk on second controller
mkdev hd 1 1 (or IDA-1)  for the second disk on second controller \n"
        cleanup $OK
}

combo_help () {
echo "
	Combining Hard Disk Interfaces
	------------------------------

	SCSI hardware may be added on to a system that has a root hard
	disk attached to an ST506 controller.  On an MCA system, SCSI
	hardware may also be added when the root hard disk is attached
	to an ESDI controller.  The addition of ST506 or ESDI disks
	on to a system with a SCSI root disk is not supported.

	Adding ST506 or ESDI disks to a combination system is identical
	to adding them to a simple system.  Invoke 'mkdev hd -u' for more
	information.

	Adding SCSI disks to a combination system is accomplished by
	invoking mkdev hd with the arguments 'drivenum', 'ctrlnum', 'lun'
	and 'hatype' 

	The first argument, 'drivenum', is the disk number.  Choices for
	'drivenum' are identical to the choices on a SCSI-only system.
	Invoke 'mkdev hd -u' for further information."

echo "\nPress return to continue: \c"
read x

echo "
	The second argument, 'ctrlnum', must be in the following format: 
	     \"SCSI-0\"        --  for the first SCSI Host Adapter
	     \"SCSI-1\"        --  for the second SCSI Host Adapter
	     \"SCSI-n\"        --  for the nth SCSI Host Adapter

	The third argument 'lun', is optional.  Selection of this argument 
	is identical to selection of it when installing on a SCSI-only system.
	Invoke 'mkdev hd -u' for more information.

	The fourth argument 'hatype', is optional. This is the prefix of the
	SCSI host adapter driver used to control 'ctrlnum'. To specify a 
	hatype, you must give a complete command line including id, ctrlnum, 
	id, and hatype.

	Example:
	Adding a SCSI disk to the first SCSI host adapter on a combination
	system.  If disk ID is 0, and the disk is being attached to the first
	SCSI host adapter, and the lun is 0, enter:

	mkdev hd 0 SCSI-0 0\n"

	cleanup $OK
}

do_relink()
{
	[ $relink -lt 1 ] && return
	if getyn "A new kernel must be built and rebooted before disk \
configuration can continue. Would you like to relink at this time?"
	then
		( cd /etc/conf/cf.d; ./link_unix )
	else
		echo "\nYou must build a new kernel before proceeding with \
further configuration\nof this disk."
	fi
	echo "\nAfter the system is rebooted with the new kernel,
reinvoke mkdev hd to initialize the new hard disk.\n"
	exit $?
}

makerootdisk_iqm () {

	ctrlnum=0
	drivenum=0
	disknum=0
	rootinstall=TRUE
	[ "$IQM_DIVVY_INTERACTIVE" = "yes" ] || {
		interactive=
	}
	get_root_type || term_echo "Couldn't determine root disk type"
	rundparam || term_echo "Couldn't write out disk parameters"
	fdisk_badtrk_iqm || {
		term_echo "Couldn't partition/badtrk the disk"
		exit 1
	}
	divdisk_iqm || {
		term_echo "Couldn't divvy the active UNIX partition"
		exit 1
	}

#
# If interactive divvy then we're done.
#
	[ "$IQM_DIVVY_INTERACTIVE" = "yes" ] && {
		return $OK
	}

	term_echo "Making file systems..."
	set `echo $IQM_ROOT_DIV`
	fs_size=`expr $3 - $2 + 1`
	mkfs_iqm y EAFS hd0root `expr $fs_size \* 2` 1 $IQM_HD_BPC || {
		term_echo "Couldn't make the root filesystem"
		exit 1
	}
	[ "$IQM_U_DIV" != "NOT" ] && {
		set `echo $IQM_U_DIV`
		fs_size=`expr $3 - $2 + 1`
		mkfs_iqm y EAFS bAFC `expr $fs_size \* 2` 1 $IQM_HD_BPC || {
			term_echo "Couldn't make the u filesystem"
			exit 1
		}
	}
	/etc/mount /dev/hd0root /mnt || {
		term_echo "Couldn't mount the root disk"
		exit 1
	}
	term_echo "Creating devices..."
	makedir_iqm /mnt/dev 755 || {
		term_echo "Couldn't make the /dev directory"
		exit 1
	}
	set $IQM_ROOT_DIV
	mknode_iqm root b 1 4$1 640 || {
		term_echo "Couldn't create /dev/root"
		exit 1
	}
	mknode_iqm rroot c 1 4$1 640 || {
		term_echo "Couldn't create /dev/rroot"
		exit 1
	}
	set $IQM_SWAP_DIV
	mknode_iqm swap b 1 4$1 640 || {
		term_echo "Couldn't create /dev/swap"
		exit 1
	}
	mknode_iqm rswap c 1 4$1 640 || {
		term_echo "Couldn't create /dev/rswap"
		exit 1
	}
	[ "$IQM_U_DIV" != "NOT" ] && {
		set $IQM_U_DIV
		mknode_iqm u b 1 4$1 640 || {
			term_echo "Couldn't create /dev/u"
			exit 1
		}
		mknode_iqm ru c 1 4$1 640 || {
			term_echo "Couldn't create /dev/ru"
			exit 1
		}
		makedir_iqm /mnt/u 755 || {
			term_ech "Couldn't make the /u directory"
			exit 1
		}
	}
	set $IQM_RECOVER_DIV
	mknode_iqm recover b 1 4$1 640 || {
		term_echo "Couldn't create /dev/recover"
		exit 1
	}
	mknode_iqm rrecover c 1 4$1 640 || {
		term_echo "Couldn't create /dev/rrecover"
		exit 1
	}
	/etc/umount /mnt
	term_echo "Hard disk initialization is complete."
}

makeroothd() {

	# figure out hard disk type
	  get_root_type
	# 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 
}

makenonroothd() {

	# if SCSI, verify that SCSI kernel configuration includes this disk
	  [ "$newtype" = "S" ] && {
		scsi_config
		# get major number of SCSI driver from mdevice
	  	scsi_major
		}
	# set minor numbers
	  set_minors
	# determine disk number for use in perms list
	  set_disknum
	# make sure NDISK is big enough
	  conf_ndisk
	# do we need a relink/reboot before we can talk to the device?
	  do_relink
	# last chance to bail out
	  quitnow
	# create devices 
	  fix
	# get disk parameters; if ST506/ESDI disk, call dkinit
	  rundparam
	# fdisk for all, and badtrack for ST506/ESDI disk
	  diskprep
	# divide the disk into file systems
	  rundivvy
	  echo "\nHard disk initialization procedure completed.\n"

}

#### begin main ####

if cat /dev/mcapos 1>/dev/null 2>/dev/null
then	arch=MC
else	arch=AT
fi

# ROOT INSTALL CASE

if [ -n "$IQM_FILE" ]
then
	makerootdisk_iqm
	cleanup $OK
elif [ "$1" = "0" -a "$2" = "0" ]
then
	if [ "$3" = "-n" ]
	then	interactive=
	elif [ "$3" ]
	then	error bad_arg
	fi
	ctrlnum=0
	drivenum=0
	disknum=0
	major=1
	rootinstall=TRUE
	makeroothd
	cleanup $OK
fi

# NON-ROOT INSTALL CASE

case "$#" in
	  0) no_args=true
	     # Get root hd type to prompt them for valid choices
	     get_root_type
	     get_args
	     ;;

	  1) case $1 in
		-u) usage 		;;
		-h) combo_help		;; 
		 *) error bad_arg	;;
	     esac
	     ;;

	2|3|4) # make sure sed is installed for use by this script
	     [ -x "/bin/sed" ] || error no_sed
	     # Save command line args
	     argv=$*
	     # Find out root hd type to make sure their input is valid
	     get_root_type
	     set $argv

	     case "$2" in
		ST506-0|ST506-1)  newtype=W	;;
		OMTI-0|ESDI-0)    newtype=E	;;
		SCSI-[0-9])       newtype=S	;;
		IDA-[0-9])	  newtype=I	;;
		[0-9])	newtype=$rootdisk	;;
		*)	error bad_arg		;;
	     esac
		
	     case "$2" in
		*[0-9])	ctrlnum=`expr "$2" : '.*\(.\)$'` ;; 
	     esac
	     

	     case ${newtype}$3 in
	     S)		LUN=0			;;
	     S[0-7])	LUN=$3			;;
	     E|W|I)				;;
	     *)		error bad_arg		;;
	     esac
	
	     [ $# -eq 4 ] && {
		case $newtype in
		S) HATYPE=$4 ;;
		*) error bad_arg;;
		esac
	     }

	     case ${newtype}$1 in
		S[0-7])	ID=$1
			HA=$ctrlnum		;;
		E0|W0)	drivenum=0		;;
		E1|W1)	drivenum=1		;;
		E*)	error esdi_disk		;;
		W*)	error st506_disk	;;
 		I*)	drivenum=$1		;;
		*)	error bad_arg		;;
	     esac
	
	     major=1

	     case ${rootdisk}${newtype} in
		SS)				;;
		SE)	error esdi_add		;;
		SW)	error st506_add		;;
		ES)	[ $arch = AT ] && \
				error scsi_omti	;;
		EE)	[ $ctrlnum -eq 0 ] || \
				error esdi_one	;;
		EW)	error st506_add		;;
		WS)				;;
		WE)	error esdi_add		;;
		WW)	[ $ctrlnum -eq 0 ] || {
			    if [ $arch = AT ]
			    then major=54
			    else error st506_mca
			    fi
			}			;;
		# IDA disks
		?I)	exec $IDASCRIPT  $drivenum $ctrlnum;;
		IE)	;; #ok?
		IS)	;;
		IW)	[ $ctrlnum -eq 0 ] && error root_ida
	     esac

	     # set the traps
	     settrap
	     ;;

	*)   error bad_arg  
	     ;;	
esac

#need link-kit for node.d, idmknod and mscsi files
check_link
makenonroothd
cleanup $OK
