#!/bin/sh
#*******************************************************************************
#  Copyright 1998 Symbios Inc.  All Rights reserved.                           *
#  This file is confidential and a trade secret of Symbios Inc.  The receipt   *
#  or possession of this file does not convey any rights to reproduce or       *
#  disclose its contents or to manufacture, use, or sell anything it may       *
#  describe, in whole, or in part, without the specific written consent of     *
#  Symbios Inc.                                                                *
#*******************************************************************************

#*******************************************************************************
#
# NAME:		genscsiconf
# VERSION:	rm6_2
# UPDATE DATE:	%date_modified: Thu May 04 11:02:49 2000 %
# INSPECTED ON:
#
# CONTACTS:
#   Originator:
#   Owner:
#   Backup:
#   Updater:	%created_by:    awilner %
#
# DESCRIPTION:
#
#
# NOTES:
#
#
# REFERENCE:
# 1.  Source Code Integrity Measures
#
#
# CODING STANDARD WAIVERS:
#
#
#*******************************************************************************

#*******************************************************************************
# PROCEDURE:	
#
# DESCRIPTION: Generate (NATIVE_DRIVER).conf entries for array LUNs at IDs where arrays
# currently exist and at IDs where arrays might exist (hot add) OR at all IDs.
#
# SYNTAX:
#
#
# NOTES:
#
#
# RETURNS:
#
#

# G E N S D C O N F   M A I N   P R O C E D U R E

RMPARAMS_FILE=/etc/raid/rmparams

MAXLUNS=`grep -v "^#" $RMPARAMS_FILE | grep System_MaxLunsPerController | cut -d= -f2`
MAXIDS=`grep -v "^#" $RMPARAMS_FILE | grep System_MaxSCSIid | cut -d= -f2`

RM_BOOT_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmBootHomeDirectory | cut -d= -f2`
RM_NAME_FILE=`grep -v "^#" $RMPARAMS_FILE | grep System_NamefileDirPath | cut -d= -f2`
NATIVE_DRIVERS=`grep -v "^#" $RMPARAMS_FILE | grep Rdac_NativeScsiDrivers`


LAD=$RM_BOOT_HOME/lad
USAGE="Usage:  $0 [ -v ]"
VERBOSE=FALSE
SENTINEL1="# BEGIN RAID Manager additional lun entries"
SENTINEL2="# END RAID Manager additional lun entries"
WARNING="# DO NOT EDIT from BEGIN above to END below..."

PATH="/usr/bin:/usr/sbin:${PATH}"
export PATH

set -- `getopt v $* 2>/dev/null`
if [ $? != 0 ]
then
	echo $USAGE
	exit 1
fi
for i in $*
do
	case $i in
	-v) VERBOSE=TRUE; shift;;
	--) shift; break;;
        esac
done

if [ $VERBOSE = TRUE ]
then
	set -x
fi


if grep -v "^#" $RMPARAMS_FILE | grep "Rdac_HotAddDisabled.*=.*FALSE" >/dev/null 2>&1
then
	HOTADD_ALL="TRUE"
fi

if grep -v "^#" $RMPARAMS_FILE | grep "Rdac_HotAddDisabled.*=.*PARTIAL" >/dev/null 2>&1
then
	RDAC_HOTADD_IDS=`grep -v "^#" $RMPARAMS_FILE | grep Rdac_HotAddIDs`
fi

$LAD >/tmp/genscsiconf.lad.$$

if [ $? -ne 0 ]
then
	echo "Error while creating RAID additions to SCSI .conf files: Unable to write /tmp/genscsiconf.lad.$$"
	exit 1
fi

EXISTING_IDS=""

cat /tmp/genscsiconf.lad.$$ |
while read CTRL_INFO
do
	ID=`echo $CTRL_INFO | cut -dt -f2 | cut -dd -f1`
	EXISTING_IDS="$EXISTING_IDS"":$ID:"
	echo $EXISTING_IDS >/tmp/genscsiconf.ids.$$

done

EXISTING_IDS=`cat /tmp/genscsiconf.ids.$$ 2>/dev/null`

for DRIVER in 2 3 4 5 6 7 8 9
do
	NATIVE=`echo $NATIVE_DRIVERS | cut -d: -f$DRIVER`

	if [ -z "${NATIVE}" ]
	then
		/etc/raid/bin/symconf >/kernel/drv/rdriver.conf
		sc_err=$?
		if [ $sc_err -ne 0 ]
		then
			if [ $sc_err -eq 10 ]
			then
				echo "Warning: /kernel/drv/rdnexus.conf has run out of pseudo-busses."
				echo "Some disk array controllers may not be accessible until more entries are"
				echo "added to rdnexus.conf."
			else
				echo "Error: Unable to build /kernel/drv/rdriver.conf."
			fi
		fi
		exit 0
	fi

	IS_SSD=`echo $NATIVE | grep ssd`
	if [ -n "${IS_SSD}" ]
	then
		continue
	fi

	CONF_FILE=/kernel/drv/$NATIVE.conf
	cp $CONF_FILE /tmp/$NATIVE.conf.$$  2>/dev/null

	if [ -f /tmp/$NATIVE.conf.$$ ]
	then
		ed /tmp/$NATIVE.conf.$$ >/dev/null 2>&1 <<EOF
			/$SENTINEL1/,/$SENTINEL2/d
        		w
        		q
EOF
	else
		continue
	fi


	

	echo $SENTINEL1 >/tmp/raid.$NATIVE.conf.$$
	if [ $? -ne 0 ]
	then
		echo "Error while creating RAID additions to $NATIVE.conf: Unable to write /tmp/raid.$NATIVE.conf.$$"
		exit 2
	fi
	echo $WARNING >>/tmp/raid.$NATIVE.conf.$$


	ID=0


	while [ $ID -le $MAXIDS ]
	do
		EXISTING_DOID=`echo "$EXISTING_IDS" | grep ":$ID:"`

		if [ -n "${RDAC_HOTADD_IDS}" ]
		then
			HOT_ADD_DOID=`echo "$RDAC_HOTADD_IDS" | grep ":$ID:"`
		fi

		if [ -n "${HOTADD_ALL}" ] || [ -n "${EXISTING_DOID}" ]  || [ -n "${HOT_ADD_DOID}" ]
		then
			LUN=0
			while [ $LUN -lt $MAXLUNS ]
			do
				ALREADY_THERE=`cat /tmp/$NATIVE.conf.$$ | grep "target=$ID" | grep "lun=$LUN"`
				if [ -z "${ALREADY_THERE}" ]
				then
					CF_LINE=`printf ' name="%s" class="scsi" \n' "$NATIVE"`
					echo ${CF_LINE} >>/tmp/raid.$NATIVE.conf.$$
					CF_LINE=`printf '        target=%s lun=%s;\n' "$ID" "$LUN"`
					echo ${CF_LINE} >>/tmp/raid.$NATIVE.conf.$$
					CF_LINE=`printf '\n' `
					echo ${CF_LINE} >>/tmp/raid.$NATIVE.conf.$$
					if [ $? -ne 0 ]
					then
						echo "Error while creating RAID additions to $NATIVE.conf: Unable to write /tmp/raid.$NATIVE.conf.$$"
						exit 3
					fi
				fi
				LUN=`expr $LUN + 1`
			done
		fi
		ID=`expr $ID + 1`
	done


	echo $SENTINEL2 >>/tmp/raid.$NATIVE.conf.$$
	if [ $? -ne 0 ]
	then
		echo "Error while creating RAID additions to $NATIVE.conf: Unable to write /tmp/raid.$NATIVE.conf.$$"
		exit 4
	fi


	cat /tmp/$NATIVE.conf.$$ /tmp/raid.$NATIVE.conf.$$ >$CONF_FILE 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo "Error while creating RAID additions to $NATIVE.conf: Unable to write $CONF_FILE"
		exit 4
	fi

	rm  /tmp/$NATIVE.conf.$$ /tmp/raid.$NATIVE.conf.$$ /tmp/genscsiconf.lad.$$ /tmp/genscsiconf.ids.$$ 2>/dev/null
done

/etc/raid/bin/symconf >/kernel/drv/rdriver.conf
sc_err=$?
if [ $sc_err -ne 0 ]
then
	if [ $sc_err -eq 10 ]
	then
		echo "Warning: /kernel/drv/rdnexus.conf has run out of pseudo-busses."
		echo "Some disk array controllers may not be accessible until more entries are"
		echo "added to rdnexus.conf."
	else
		echo "Error: Unable to build /kernel/drv/rdriver.conf."
	fi
fi

exit 0
