:
#	@(#) hdinit 22.1 90/04/04 
#
#	Copyright (C) The Santa Cruz Operation, 1988.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and should be treated 
#	as Confidential.
#
# hdinit
#
# front end which will call /usr/lib/mkdev/hd
# It does all the disk and mirroring initializations
: ${OK=0} ${FAIL=1} ${STOP=10} ${ERROR=100}

# set up the environment
PATH=/bin:/usr/bin:/etc:/mountram/bin:/mountram/usr/bin:/mountram/etc:/mountram/altos/bin
LANG=english_us.ascii
export PATH LANG

error() {
	echo "\nError: $*" >&2
	return $FAIL
}

cleanup() {
	cd /
	# During an upgrade restore the original .profile and
	# leave it in a sane state.
	if [ $1 -eq "$FAIL" -a -n "$upgrade" -a -f /mnt/.prof.real ]
	then
		mv /mnt/.prof.real /mnt/.profile > /dev/null 2>&1
	fi
	sync
	sync
	umount $mdisk         2> /dev/null
	umount $usrfs         2> /dev/null
	umount /dev/ram1m     2> /dev/null
	rm -f $SIZES_OF_DISKS 2> /dev/null
	rm -f $TMPFILE        2> /dev/null
	rm -f $TMPFILE2       2> /dev/null
	exit $1
}

# Usage: getyn "question"
# Argument is a quoted question
# Notes: getyn returns 0 if the answer is y or Y and 1 if
#	 the answer is n or N. The answer is in global ANS.
getyn() {
	while	echo "\n$* (y/n) \c" >&2
	do	read yn rest
		case $yn in
		[yY])	ANS='Y'
			return 0 				;;
		[nN])	ANS='N'
			return 1				;;
		*)	echo "\nPlease answer y or n" >&2	;;
		esac
	done
}


# get host adapter type & SCSI ID number.
# Values of SCSI id and host adapter type are returned in the global 
# variables tmp_id and tmp_driver

get_hdlocation () {

    if [ ! -f "$SADAPTER" ] 
    then
	echo "hdinit: $SADAPTER does not exist." 
	return $ERROR
    fi	
    grep -v "^#" ${SADAPTER} | \
		pr -n | \
		grep "^ " > ${TMPFILE}
    awk -F \" '{print "\t\t"NR".  " $2}' ${TMPFILE} > ${TMPFILE2}

    while echo 
    do
   	echo " 
The following host adapter types are currently 
supported on this system:
"
	cat ${TMPFILE2}
	echo "
Enter the number corresponding to the host adapter type 
attached to the hard disk, where you wish the Installation 
or Upgrade of the root file system to take place or type 
'q' to quit:  \c"

	read response
	case ${response} in
		q|Q)	return $ERROR
			break
			;;
		*)	ha_entry=`grep "^[ ]*${response}" ${TMPFILE}` 
			if [ $? -ne 0 -o "${response}" = "" ]
			then
				echo "
Invalid selection.  Please try again."
			else
				set ${ha_entry}
				tmp_driver=$2
				echo "\n"
				echo "
Enter the SCSI ID of the hard disk [0-5]:  \c"
				read response
				case ${response} in
					[0-5])	tmp_id=$response
						echo "\n"
						break
						;;
					*)	echo "
Invalid selection.  Please try again."
						;;
				esac
			fi
			;;
	esac
    done
}

# Computes the actual size of the disk available for Unix partitions.
# Obtained by subtracting the size of non-Unix partitions from the
# total size of the disk.

get_disk_size() {

if [ $# -ne 1 ]
then
	echo "get_disk_size: Insufficient arguments"
	return $ERROR
fi
if [ $1 -gt 4 ]
then
	echo "get_disk_size: Invalid argument"
	return $ERROR
fi
disk=$1
dos_size=0
disk_size=0
TMP=/tmp/dlayout.tmp

case $disk in
        1) device=/dev/rhda0 ;;
	2) device=/dev/rhdb0 ;;
	3) device=/dev/rhdc0 ;;
	4) device=/dev/rhdd0 ;;
	*) echo "Error: Invalid argument to get_disk_size" 
	   return $ERROR ;;
esac
if [ ! -c $device ]
then
	echo "get_disk_size: Cannot find device file"
	return  $ERROR
fi
set `dlayout -sh $device` > /dev/null 2>&1
stat1=$?
if [ $stat1 -ne 0 ]
then
	echo "get_disk_size: Failed to get disk size."
	return $ERROR
fi
disk_size=$1 

dlayout -ph  $device | grep -v UNIX > $TMP 2> /dev/null
while read a b c d  SIZE f < $TMP
do
	dos_size=`expr $dos_size + $SIZE`
done
# Convert both to the same units ie: 512 byte blocks
disk_size=`expr $disk_size \* 2048`
case $disk in
	1) sizeof_1=`expr $disk_size - $dos_size`
		;;
	2) sizeof_2=`expr $disk_size - $dos_size`
		;;
	3) sizeof_3=`expr $disk_size - $dos_size`
		;;
	4) sizeof_4=`expr $disk_size - $dos_size`
		;;
	*)  echo "get_disk_size: Invalid parameter"
	    return $ERROR
		;;
esac
rm -f $TMP > /dev/null 2>&1
}


# It returns the position of the smallest disk in the list
# sizeof_1, sizeof_2, sizeof_3, sizeof_4 

get_smallest() {

set `cat $SIZES_OF_DISKS | sort +1n`
smallest_disk=$1
case $smallest_disk in
	sizeof_1) return 1
			;;
	sizeof_2) return 2
			;;
	sizeof_3) return 3
			;;
	sizeof_4) return 4
			;;
	       *) echo "Error: Invalid name  in $SIZES_OF_DISKS"
		  return $ERROR
			;;
esac
}


check_sizes() {

	count=1
	while [ "$count" -le `expr $mirror_count + 1` ]
	do 
	        get_disk_size $count
		if [ $? -eq $ERROR ]
		then
		 	echo "
 check_sizes: get_disk_size failed"
			 return $FAIL
		fi
		case $count in 
			 1) echo "sizeof_1	$sizeof_1" >  $SIZES_OF_DISKS
				 ;;
			 2) echo "sizeof_2	$sizeof_2" >> $SIZES_OF_DISKS
				 ;;
			 3) echo "sizeof_3	$sizeof_3" >> $SIZES_OF_DISKS
				 ;;
			 4) echo "sizeof_4	$sizeof_4" >> $SIZES_OF_DISKS
				 ;;
			*)  echo "check_sizes: Invalid Number of mirror disks"
			    return $FAIL
				;;
		 esac
		 count=`expr $count + 1`
	 done
	 get_smallest 
	 smallest=$?
	 if [ $smallest -eq $ERROR ]
	 then
		 echo "
 check_sizes: get_smallest failed"
		 return $FAIL
	 fi
	# If the size of the root disk is not equal to or less than the size 
	# of the mirror disks, then the installation cannot proceed.
	 if [ $smallest -ne 1 ]
	 then
		return $ERROR
	 fi

}



check_duplicates() {

	duplicate_exists=
	count=1
	while [ "$count" -le "$mirror_count" ]
	do 
		case $count in 
			 1) if [ $root_id -eq $mirror1_id -a \
				 $root_driver = $mirror1_driver ]
			    then
				duplicate_exists=yes
			    fi
				 ;;
			 2) if [ $mirror1_id -eq $mirror2_id -a \
				 $mirror1_driver = $mirror2_driver ]
			    then
				duplicate_exists=yes
			    elif [ $root_id -eq $mirror2_id -a \
				 $root_driver = $mirror2_driver ]
			    then
				duplicate_exists=yes
			    fi
				 ;;
			 3) if [ $root_id -eq $mirror3_id -a \
				 $root_driver = $mirror3_driver ]
			    then
				duplicate_exists=yes
			    elif [ $mirror1_id -eq $mirror3_id -a \
				 $mirror1_driver = $mirror3_driver ]
			    then
				duplicate_exists=yes
			    elif [ $mirror2_id -eq $mirror3_id -a \
				 $mirror2_driver = $mirror3_driver ]
			    then
				duplicate_exists=yes
			    fi
				 ;;
			*)  echo "
check_duplicates: Invalid Number of mirror disks"
			    return $FAIL
				;;
		 esac
		 if [ -n "$duplicate_exists" ]
		 then
			return $ERROR
		 fi
		 count=`expr $count + 1`
	 done
	 return $OK

}



# Update the file /usr/lib/mkdev/perms/HD depending on the number 
# of mirror disks and also depending on the usr file system being
# mirrored. Fixperm cannot be run from here because we are
# booted off the root floppy.
fix ()  {

if [ -n "$mirror_usr" ]
then
	echotext1="# 
${newtype}$disknum	c600	sysinfo/sysinfo	1	./dev/rhd${disknum}usr	${major}/${minor_u} 
${newtype}$disknum	b600	sysinfo/sysinfo	1	./dev/hd${disknum}usr	${major}/${minor_u} 
#"

fi
	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	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} 
#"

	echo "$echotext" >> /usr/lib/mkdev/perms/HD
	if [ -n "$mirror_usr" ]
	then
	echo "$echotext1" >> /usr/lib/mkdev/perms/HD
	fi
	# Run fixperm from rtsinstall.
	# fixperm -c -d${newtype}${disknum} /usr/lib/mkdev/perms/HD
}



# Set up the disknum (b/c/d depending on the number of hard disks) & 
# the minor device numbers before calling fix.
update_HD() {

	# SCSI
	newtype=S
	major=64
	
	# Disk 2 : b
	if [ "$mirror_count" -eq 1 -o "$mirror_count" -eq 2 \
	     -o "$mirror_count" -eq 3 ]
	then
		disknum=b
		minor_0=64
		minor_1=79
		minor_2=87
		minor_3=95
		minor_4=103
		minor_a=111
		minor_d=119
		minor_r=104
		minor_u=106
		fix
	fi	
	# Disk 3 : c
	if [ "$mirror_count" -eq 2 -o "$mirror_count" -eq 3 ]
	then
		disknum=c
		minor_0=128
		minor_1=143
		minor_2=151
		minor_3=159
		minor_4=167
		minor_a=175
		minor_d=183
		minor_r=168
		minor_u=170
		fix
	fi	
	# Disk 4 : d
	if [ "$mirror_count" -eq 3 ]
	then
		disknum=d
		minor_0=192
		minor_1=207
		minor_2=215
		minor_3=223
		minor_4=231
		minor_a=239
		minor_d=247
		minor_r=232
		minor_u=234
		fix
	fi	
}


# main
# set a trap here so if the user dels out, we can unmount the hd0root
trap 'cleanup $STOP' 1 2 3 15

ANS='Y'				# Global variable for function getyn
root_id=0			# SCSI id of root disk
root_driver=			# Host Adapter type & driver of root disk
ha=0				# Host adapter number of the root disk
mdisk=/dev/hdaroot	 	# Primary disk root device file (block)	
rwmdisk=/dev/rhdaroot		# Primary disk root device file (raw)
lun=0				# Logical unit Number
mirror= 			# Flag to indicate if user desires mirroring
mirror_usr=			# Flag : Mirror usr filesystem ?
mirror_ha=0			# Host adapter number of the mirror disk
usrfs="/dev/hdausr"		# Device node for the usr file system
VDINFO=/etc/vdtab		# Virtual disk information 
MIRROR_INFO=/tmp/mirror_info 	# .profile picks up the information written here
mscsi_info=mscsi.tmp 		# .profile picks up the information written here
upgrade_info=upgrade.mscsi	# .profile picks up the information written here
version_info=version.tmp   	# rtsinstall picks up this info.
MAX_MIRRORS=3   		# Maximum number of mirror disks supported
mirror_count=1  		# Number of mirror disks
tmp_id=				# Used by get_hdlocation to return value of id
tmp_driver= 			# Used by get_hdlocation to return driver name
sizeof_1=0 			# Size of Root Disk
sizeof_2=0 			# Sizes of mirror disks
sizeof_3=0      
sizeof_4=0      
TMPFILE=/tmp/_Sadapter 	 	# Used by function get_hdlocation
TMPFILE2=/tmp/_Sadapter2
SADAPTER=/etc/conf/cf.d/Sadapter 
SIZES_OF_DISKS=/tmp/disksizes	# tmp file for storing disk sizes & sorting
newtype=S			# Disk type SCSI
major=64			# Major device number of hard disks.
disknum=			# 'a | b | c | d' 
minor_0=			# Minor device number of hd"$disknum"0
minor_1= 			# Minor device number of hd"$disknum"1
minor_2= 			# Minor device number of hd"$disknum"2
minor_3= 			# Minor device number of hd"$disknum"3
minor_4= 			# Minor device number of hd"$disknum"4
minor_a= 			# Minor device number of hd"$disknum"a
minor_d= 			# Minor device number of hd"$disknum"d
minor_r= 			# Minor device number of hd"$disknum"root
minor_u= 			# Minor device number of hd"$disknum"usr
MAJORDEV_MIRROR=25		# Major device number of /dev/root & /dev/usr
				# when they are mirrored

cd /
while :
do 	echo "

System V Hard Disk Initialization

You can choose a fully interactive disk initialization (1), 
which requires you to set the disk parameters, specify the 
size of the UNIX partition, and control the layout of file-
systems and swap area. If you want to retain any previously 
installed UNIX partition or to delete any non-UNIX partition, 
choose the Fully Configurable Installation.

You can also choose a non-interactive disk initialization (2)
that creates a complete disk layout and configuration using 
system default values. This option will delete ALL UNIX 
partitions.

You can UPGRADE your system (3) if the OS is previously
installed. Please refer to your release notes before 
choosing this option.

	1.  Fully Configurable Installation
	2.  Automatic Installation (use system defaults)
	3.  Upgrade the system
	4.  Exit Installation

Enter your choice: \c"
	read answer
	case $answer in
		1) # unset the flags
	   	   flags=
		   break
		   ;;
		
		2) while :
		   do 	# make sure the user understands what they are doing
		   	echo "


Verify Automatic Disk Initialization

You have chosen to initialize the primary hard disk 
non-interactively using system defaults. 

This option requires that your system was set up to
recognize the hard disk at the factory prior to System
V Operating System installation.

The hard disk initialization will OVERWRITE ANY PRE-
EXISTING UNIX PARTITION. A single, active UNIX partition
will be created and divided into a root filesystem, a
/usr filesystem and a swap area.

Are you sure you wish to do this (y or n): \c"
		   	read noninteryn
		   	case $noninteryn in
				Y|y) 	# okay, set the -n flag
					flags="-n"
					break 2
					;;
				N|n)	# go back to main menu
					flags=
					break
					;;
				*) 	# garbage input
					echo "

	Please answer 'y' or 'n'.
	Press <Return> to continue: \c"
					read answer
					;;
			esac
		   done
		   ;;
		3) # Reinstall without initializing the hard disk.
		   upgrade="yes"
		   break
		   ;;
		4) # user wants to quit
	   	   exit $STOP
		   ;;
		*) # garbage input
	   	   echo "

	Unknown answer: $answer

	Press <Return> to continue: \c"
		   read answer
		   ;;
	esac
done

get_hdlocation
if [ $? -eq $ERROR ]
then
	echo "
Aborting installation..................."
	cleanup $FAIL
fi
root_id=$tmp_id
root_driver=$tmp_driver
# Save this info so that it can be linked into the hard disk kernel.
echo " $root_driver $root_id " > /tmp/"$mscsi_info"
# Update Sdskcfg table with this information.
addhd  -a $root_driver -i $root_id > /dev/null 2>&1
addhd_status=$?
if [ $addhd_status  -eq -1 ]
then
	error " 
Cannot add disk to Sdskcfg table.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
	cleanup $FAIL
fi

mdisk=/dev/hdaroot
rwmdisk=/dev/rhdaroot
usrfs=/dev/hdausr
# check root and user filesystems if re-install without Making Filesystems
[ -n "$upgrade" ] && {
	fsstat $mdisk > /dev/null 2>&1
	mnt=$?
	fsstat $usrfs > /dev/null 2>&1
	usr=$?
	if [ $mnt -eq 3 -o $usr -eq 3 ]
	then
		error "Cannot stat the hard disk filesystems.
The installation cannot proceed until this is possible.
Exiting..."
		cleanup $FAIL
	fi
	if [ $mnt != 0 ]
	then
		fsck -y $mdisk
		mnt=$?
		if [ $mnt != 0 ]
		then
			error "Cannot fsck the root filesystem.
The installation cannot proceed until this is possible.
Exiting..."
			cleanup $FAIL
		fi
	fi

	if [ $usr != 0 ]
	then
		fsck -y $usrfs
		usr=$?
		if [ $usr != 0 ]
		then
		error "Cannot fsck the usr filesystem.
The installation cannot proceed until this is possible.
Exiting..."
			cleanup $FAIL
		fi
	fi
}

if [ -n "$upgrade" ]
then
	/etc/mount $mdisk /mnt > /dev/null 2>&1 || {
	error "Cannot mount the hard disk root filesystem.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
	cleanup $FAIL
	}
	/etc/mount $usrfs /usr > /dev/null 2>&1 || {
	error "Cannot mount the hard disk user filesystem.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
	cleanup $FAIL
	}
fi

if [ -z "$upgrade" ]
then
	echo "
If your system is installed with one or more add-on SCSI hard disks, 
you may choose to use them to mirror the primary disk ROOT filesystem.
If you choose to mirror the ROOT filesystem, then you may also choose
to use the mirror disks to mirror the primary disk USR filesystem.
If you choose to mirror the ROOT filesystem but not the USR filesystem,
then the leftover space on the mirror disks may be used for other
purposes.  The maximum number of mirror disks (excluding the primary
disk) is ${MAX_MIRRORS}.  It is recommended that all hard disks in a
mirror configuration be of the same size.

Answer 'y' to the following question only if you have add-on hard
disks installed on your system AND you choose to mirror your ROOT
filesystem."
	getyn "Do you wish to create a MIRRORED ROOT FILESYSTEM on add-on
disks?"
	case $ANS in
		Y|y)	mirror="yes" 
			while :
			do 
				echo "
How many mirror disks, in addition to the root disk 
do you wish to install ? [1-"$MAX_MIRRORS"] (default = 1) \c"
				read mirror_count
				if [ "$mirror_count" = "" ]
				then
					mirror_count=1
				fi
				if [ $mirror_count -lt 1 -o \
			     	$mirror_count -gt $MAX_MIRRORS ] 
				then
					echo "
Number out of range.  Please try again. \007"
				else
					break
				fi
			done
			while :
			do
				echo "
Would you also like the USR filesystem mirrored? (y/n) \c "
				read response
				case $response in
					y|Y)  mirror_usr=yes
				      	break
						;;
					n|N)  mirror_usr=
				      	break
						;;
					*)    echo 
"Invalid answer. Please answer y or n"
						;;
				esac
			done
			;;
		N|n)	rm -f "$MIRROR_INFO" > /dev/null 2>&1
			;;
	esac
fi

# Upgrade from a mirrored root filesystem.
# Not supported for a pre 2.0 version (/etc/conf/cf.d/version
# doesn't exist in pre 2.0 versions.
if [ -n "$upgrade" -a -f /mnt/etc/conf/cf.d/version ]
then
	if [ ! -b /mnt/dev/root -o ! -b /mnt/dev/usr ]
	then
		echo "
Error: Device nodes /dev/root & /dev/usr do not exist.
Cannot proceed with upgrade"
		cleanup $FAIL
	fi
	# show is a stripped version of ls -l, to conserve 
	# space on the ramdisk.
	# The major device number of /dev/root is 25, if the
	# root filesystem is mirrored.
	majordev_root=`show /mnt/dev/root | awk '{print substr($5,1,2)}'`
	if [ "$majordev_root" -eq "$MAJORDEV_MIRROR" ]
	then
		mirror="yes"	
		echo "
Your root filesystem is mirrored."
		if [ -f /mnt/etc/conf/pack.d/vd/space.c ]
		then
			mirror_count=`grep "^{.M.,0," /mnt/etc/conf/pack.d/vd/space.c | awk '{print substr($1,8,1) }'`
		    	if [ "$mirror_count" -lt 2 -o "$mirror_count" -gt 4 ]
		    	then
				echo "
Error: Couldn't get information about the number of mirror
components from /etc/conf/pack.d/vd/space.c file.
Aborting upgrade."
				cleanup $FAIL
		    	fi
			# mirror_count is the number of mirror disks in
			# addition to the primary disk.
			mirror_count=`expr $mirror_count - 1`
		else
			echo "
Error: /etc/conf/cf.d/pack.d/vd/space.c missing. Cannot
proceed with upgrade."
			cleanup $FAIL
		fi
		# Is the usr file-system mirrored ?
		# The major device number of /dev/usr is 25, if the
		# usr filesystem is mirrored.
		majordev_usr=`show /mnt/dev/usr | awk '{print substr($5,1,2)}'`
		if [ "$majordev_usr" -eq "$MAJORDEV_MIRROR" ]
		then
			mirror_usr=yes
		else
			mirror_usr=
		fi
	fi
fi

# Get the values of Host Adapter, scsi id for all the mirror 
# disks from the user.
if [ -n "$mirror" ] 
then
	count=0
	while [ "$count" -lt "$mirror_count" ]
	do 
		count=`expr $count + 1`
		echo "\n
Configuring Mirror Disk $count.........."
		get_hdlocation
		if [ $? -eq $ERROR ]
		then
			echo "
Aborting installation..................."
			cleanup $FAIL
		fi
		case $count in 
			1)  mirror1_driver=$tmp_driver
			    mirror1_id=$tmp_id
				;;
			2)  mirror2_driver=$tmp_driver
			    mirror2_id=$tmp_id
				;;
			3)  mirror3_driver=$tmp_driver
			    mirror3_id=$tmp_id
				;;
			*)  echo "hdinit: Invalid Number of mirror disks"
			    cleanup $FAIL
				;;
		esac
	done
	check_duplicates
	stat=$?
	if [ $stat -eq $FAIL ]
	then
		echo"
hdinit: check_duplicates failed. Aborting Installation...................."
		cleanup $FAIL
	elif [ $stat -eq $ERROR ]
	then
		echo "
hdinit: Cannot have the mirror disk the same as the primary disk."
		cleanup $FAIL
	fi
 fi

if [ -n "$mirror" ]
then
   count=1
   while [ $count -le $mirror_count ]
   do
	case $count in
		1) echo " $mirror1_driver $mirror1_id " >> /tmp/"$mscsi_info"
		   addhd  -a $mirror1_driver -i $mirror1_id > /dev/null 2>&1
		   addhd_status=$?
			;;
		2) echo " $mirror2_driver $mirror2_id " >> /tmp/"$mscsi_info"
		   addhd  -a $mirror2_driver -i $mirror2_id > /dev/null 2>&1
		   addhd_status=$?
			;;
		3) echo " $mirror3_driver $mirror3_id " >> /tmp/"$mscsi_info"
		   addhd  -a $mirror3_driver -i $mirror3_id > /dev/null 2>&1
		   addhd_status=$?
			;;
		*) echo ".profile: Invalid number of mirror disks"
		   cleanup $FAIL
			;;
	esac
	if [ $addhd_status  -eq -1 ]
	then
		error " 
Cannot add disk to Sdskcfg table.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
		cleanup $FAIL
	fi
	count=`expr $count + 1`
   done
   check_sizes
   stat=$?
   if [ $stat -eq $FAIL ]
   then
   	echo "
   hdinit : Aborting Installation.................."
   	cleanup $FAIL
   elif [ $stat -eq $ERROR ]
   then
   	echo "
   Error: Primary disk has to be the smallest disk for root mirroring."
   	echo "
   Aborting Installation........................"
   	cleanup $FAIL
   fi
fi

rm -f $TMPFILE
rm -f $TMPFILE2
rm -f $SIZES_OF_DISKS

# call mkdev hd

if [ -z "$upgrade" ]
then
	/mountram/usr/lib/mkdev/hd $root_id $ha $lun $root_driver $flags -I
	stat=$?
	[ "$stat" -eq "$FAIL" ] && {
		error "Cannot initialize the Primary Hard Disk.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
		cleanup $FAIL
	}
fi

# Install on a mirrored system
# Call mkdev hd

if [ -n "$mirror" -a -z "$upgrade" ] 
then
   count=1
   while [ $count -le $mirror_count ]
   do
	case $count in
		1)      /mountram/usr/lib/mkdev/hd $mirror1_id $mirror_ha \
					      $lun $mirror1_driver -n -I -Mb
			stat=$?
			;;
		2)     	/mountram/usr/lib/mkdev/hd $mirror2_id $mirror_ha \
					      $lun $mirror2_driver -n -I -Mc
			stat=$?
			;;
		3) 	/mountram/usr/lib/mkdev/hd $mirror3_id $mirror_ha \
					      $lun $mirror3_driver -n -I -Md
			stat=$?
			;;
		*)
		   	echo ".profile: Invalid number of mirror disks"
		   cleanup $FAIL
	esac
	if [ "$stat" -eq "$FAIL" ] 
	then
	error "Cannot initialize the Mirror Hard Disk.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
		cleanup $FAIL
	fi
	count=`expr $count + 1`
   done
fi


# While upgrading if the installed OS is version 1.1aC0/1.0aC0, delete all the
# hard disk nodes, because of the new major device numbering scheme adopted in 
# the  2.0aC0 version.

if [ -n "$upgrade" ]
then
	# For a 2.0 ---> 2.* upgrade we want to preserve the mscsi file.
	# So we don't want to add any entries to it from the .profile.
	if [ -f /mnt/etc/conf/cf.d/version ]
	then
		echo "upgrade_mscsi=no" > /tmp/"$upgrade_info"
	else	
		rm -f /tmp/"$upgrade_info"
	fi
	rm -f /mnt/tmp/"$version_info"
	# This file doesn't exist in releases prior to 2.0aC0
	if [ ! -f /mnt/etc/conf/cf.d/version ]
	then
		# Upgrade from a mirrored root allowed only 
		# for 2.0 ---> 2.*
		dd if=/dev/hdaa of=/tmp/mirror.tst bs=512 skip=1 count=1 > /dev/null  2>&1
		grep "mirrordev" /tmp/mirror.tst > /dev/null 2>&1
		if [ $? -eq 0 ]
		then
			echo "
Error: Cannot upgrade from a mirrored root system, 
installed with an OS version prior to 2.0aC0 (Refer to 
your Release Notes). Please back-up all your data and 
then re-install the OS by choosing either option 1 or 
option 2 of the Installation menu."
			cleanup $FAIL
		fi
		rm -f /tmp/mirror.tst
		if [ -b /mnt/dev/vd* ]
		then
			echo "
WARNING ! Please make sure that you have backed up all
data from your virtual disks, before attempting to
upgrade. Once you have upgraded, you will not be able
to access your virtual disks. You will have to recreate
your virtual disks and then restore all the data which 
was on your virtual disks originally. If you have not 
already saved your data on the virtual disks, you can 
quit at this stage, back up all your data and then 
attempt to upgrade again, by answering 'n' to the
question below. If you have already backed up your 
data, answer 'y' to the question below to proceed with 
upgrade.\n"
			getyn "Would you like to proceed with the upgrade process ?"
			ans=$?
			case $ANS in
				Y) 
				   ;;
				N) echo "
Aborting Upgrade..."
				   cleanup $FAIL
					;;
			esac
		fi
		echo "\n\n
The upgrade procedure will now delete all the additional
hard disk nodes besides the root hard disk (The data on 
the disks will remain intact). Once the upgrade is 
complete and the system reboots, all the additional 
hard disks can be re-configured by invoking the command 
/usr/lib/mkdev/hd and choosing option 2 to 
non-destructively add a hard disk. Once all the hard
disks have been re-configured invoke /usr/lib/mkdev/fs 
to create a new /etc/default/filesys.\n"
		getyn "Would you like to proceed with the upgrade process ?"
		ans=$?
		case $ANS in
			Y)   echo "1.0aC0/1.1aC0" > /mnt/tmp/"$version_info"
			     rm -f /mnt/dev/hd[a-zA-I]*
			     rm -f /mnt/dev/rhd[a-zA-I]*
			     rm -f /mnt/dev/dsk/[a-zA-I]*
			     rm -f /mnt/dev/rdsk/[a-zA-I]*
			     rm -f /mnt/dev/vd* 
			     break
				;;
			N)   echo "
	Aborting the upgrade process.................\n"
			     cleanup $STOP
				;;
		esac
	fi
fi

if [ -z "$upgrade" ]
then
	# Clear the second 512 bytes where the mirror information is
	# stored in releases prior to 2.0aC0. We have a new Virtual
	# disk scheme in Release 2.0aC0.
	if [ ! -f /mnt/etc/conf/cf.d/version ]
	then
		echo "0" > /tmp/junk
		dd if=/tmp/junk of="$rwmdisk" bs=512 seek=1 2> /dev/null
	fi
fi


if [ -z "$upgrade" ]
then
	/etc/mount $mdisk /mnt > /dev/null 2>&1 || {
	error "Cannot mount the hard disk root filesystem.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
	cleanup $FAIL
	}
	/etc/mount $usrfs /usr > /dev/null 2>&1 || {
	error "Cannot mount the hard disk user filesystem.
The installation cannot proceed until this is possible.
Contact your Support Representative immediately.
Exiting..."
	cleanup $FAIL
	}
fi

# copies files from the floppy root and do a little setup
echo "
Setting up hard disk root filesystem. This will take a 
few minutes....\n"

# Create the /u mount directory on the root filesystem
# if /dev/u was created by divvy.
if [ -b /mnt/dev/u -a -z "$upgrade" ]
then
	mkdir /mnt/u
#	mkdir /mnt/u/lost+found
#	cd /mnt/u/lost+found
#	for i in 1 2 3 4 5
#	do
#		> temp$i
#	done
#	rm temp*
#	cd /
fi

fsavelist="/mountram/fsavelist"
fsaveulist="/mountram/fsaveulist"

if	[ -n "$upgrade" -a -f "$fsavelist" -a -f "$fsaveulist" ]
then
	# check for free space for an upgrade
	rootspace=`getfssize $mdisk`
	usrspace=`getfssize $usrfs`

	if [ $rootspace -eq 1 ]
	then
		echo "\nNot able to obtain free blocks infomation of the Root filesystem."
	elif [ $usrspace -eq 1 ]
	then
		echo "\nNot able to obtain free blocks infomation of the User filesystem."
	fi

	if [ $rootspace -eq 1 -o $usrspace -eq 1 ]
	then
		echo "Please reboot on the hard disk and make sure the filesystem is valid."
		echo "\nPress <Return> to reboot."
		read reboot
		cleanup $FAIL
	fi

	if [ $rootspace -lt 2000 ]
	then
		echo "\nNot enough space in the root filesystem for an upgrade."
	elif [ $usrspace -lt 1000 ]
	then
		echo "\nNot enough space in the usr filesystem for an upgrade."
	fi

	if [ $rootspace -lt 2000 -o $usrspace -lt 1000 ]
	then
		echo "
Please reboot on the hard disk and remove some files.
You will need at least 2000 512 byte free blocks for the root
filesystem and 1000 512 byte free blocks for the usr filesystem.
\nPress <Return> to reboot."
		read reboot
		cleanup $FAIL
	else
		echo "\nUpgrade is in progress..."
	fi

	cd /mountram
	echo "Old" | cpio -pdmu /mnt/tmp > /dev/null 2>&1
	cd /mnt
	cpio -pdmu /mnt/tmp/Old < "$fsavelist" > /dev/null 2>&1
	find ./etc/auth -print | cpio -pdmu /mnt/tmp/Old > /dev/null 2>&1
	find ./etc/default -print | cpio -pdmu /mnt/tmp/Old > /dev/null 2>&1
	find ./etc/rc?.d -print | cpio -pdmu /mnt/tmp/Old > /dev/null 2>&1
	find ./tcb/files/auth -print | cpio -pdmu /mnt/tmp/Old > /dev/null 2>&1
	cd /
	cpio -pdmu /mnt/tmp/Old < "$fsaveulist" > /dev/null 2>&1
fi

hlist="hdisklist"
rlist="rootlist"
usrlist="usrlist"
ramdisk="/dev/ram1m"
ramdiskmark="/mountram"
if [ -f "$ramdiskmark/$rlist" -a -f "$ramdiskmark/$usrlist" ]
then
	cd $ramdiskmark
	cpio -pudm /mnt < $ramdiskmark/$rlist > /dev/null 2>&1
	cd "$ramdiskmark/usr"
	cpio -pudm /usr < $ramdiskmark/$usrlist > /dev/null 2>&1
	sed  -e "s!AUTOBOOT=YES!AUTOBOOT=NO!" \
	     -e "s!VERBOSE=NO!VERBOSE=YES!" \
		/mountram/etc/default/boot \
  	     	> /mnt/etc/default/boot
else
	cpio -pudm /mnt < /rootlist > /dev/null 2>&1
fi


cd /
# set the keyboard nationality on hard disk root
mapkey -d > /usr/lib/keyboard/keys 2> /dev/null

# copy over any file in /hdisk or /mountram/hdisk if it is there
[ -d "/hdisk" ] && {
	cd /hdisk
	cpio -pdmu /mnt  < /hdisklist > /dev/null 2>&1
}

[ -d "$ramdiskmark/hdisk" ] && {
	cd $ramdiskmark/hdisk
	cpio -pdmu /mnt  < $ramdiskmark/$hlist > /dev/null 2>&1
}

#	copy files from the N1 boot floppy to the hard disk
cd /
umask 0


if [ -f "/bootlist" ] 
then	# cpio all the files in bootlist and bhdlist
{
	# While upgrading save the original .profile in case
	# the upgrade process is interrupted.
	if [ -n "$upgrade" ]
	then
		mv /mnt/.profile /mnt/.prof.real > /dev/null 2>&1
	fi
	cpio -pdmu /mnt  < /bootlist > /dev/null 2>&1
	cd /dev/hdisk
	cpio -pdmu /mnt/dev  < /bhdlist > /dev/null 2>&1
	cd /

	[ -n "$mirror" -a -z "$upgrade" ] && {
			# Copy hdb0, rhdb0......, vd0 & rvd0 onto the hard disk.
			cd /dev
			echo "hdaroot"  | cpio -pdmu /mnt/dev > /dev/null 2>&1
			echo "rhdaroot" | cpio -pdmu /mnt/dev > /dev/null 2>&1
			if [ -n "$mirror_usr" ]
			then
				echo "hdausr"  | cpio -pdmu /mnt/dev > /dev/null 2>&1
				echo "rhdausr" | cpio -pdmu /mnt/dev > /dev/null 2>&1
			fi
			# Update /usr/lib/mkdev/perms/HD and then run
			# fixperm from rtsinstall to create the nodes.
			update_HD
			echo "vd0"   | cpio -pdmu /mnt/dev > /dev/null 2>&1
			echo "rvd0"  | cpio -pdmu /mnt/dev > /dev/null 2>&1
			if [ -n "$mirror_usr" ]
			then
				echo "vd1"   | cpio -pdmu /mnt/dev >/dev/null 2>&1
				echo "rvd1"  | cpio -pdmu /mnt/dev >/dev/null 2>&1
			fi

			cd /
	}

} else { # mountable floppy, but not the right one
	echo "
Error: incorrect floppy in drive.\n"
	echo ""
	echo "
Installation Aborted. 
Contact your Support Representative if you require assistance.\n"
	cleanup $FAIL
}
fi

# Write the VD info to hard disk.
if [ -n "$mirror" ]
then
	if [ -n "$mirror_usr" ]
	then
		echo "$mirror_count	MIRROR_USR=yes" >  "$MIRROR_INFO"
	else
		echo "$mirror_count      MIRROR_USR=no" >  "$MIRROR_INFO"

	fi		
	if [ -z "$upgrade" ]
	then
		case $mirror_count in
			1) echo "0 M 16 /dev/hdaroot /dev/hdbroot" > /mnt/"$VDINFO"
			if [ -n "$mirror_usr" ]
			then
			 	echo "1 M 16 /dev/hdausr /dev/hdbusr" >> /mnt/"$VDINFO"
			fi
				
				;;
			2) echo "0 M 16 /dev/hdaroot /dev/hdbroot /dev/hdcroot" > /mnt/"$VDINFO"
			if [ -n "$mirror_usr" ]
			then
			 	echo "1 M 16 /dev/hdausr /dev/hdbusr /dev/hdcusr" >> /mnt/"$VDINFO"
			fi
				;;
			3) echo "0 M 16 /dev/hdaroot /dev/hdbroot /dev/hdcroot /dev/hddroot" > /mnt/"$VDINFO"
			if [ -n "$mirror_usr" ]
			then
			 	echo "1 M 16 /dev/hdausr /dev/hdbusr /dev/hdcusr /dev/hddusr" >> /mnt/"$VDINFO"
			fi
				;;
		esac
	fi
fi

cleanup $OK
