#!/bin/sh
# $Id: lus_add_fp_devs.sh,v 6.1.2.5 2002/09/13 19:53:51 jquintei Exp $ Copyright (c) 2002, Legato Systems, Inc.
# All rights reserved.

echo
echo "		Updating /usr/kernel/drv/lus.conf"
echo

#check for needed binaries

for file in luxadm awk sed; do
	/bin/which ${file} | grep 'no ${file}' > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		echo ${file} is not in your path
		exit 1
	fi
done

hbaports=`luxadm -e port | /bin/grep 'SUNW,qlc' |
	awk ' "CONNECTED" == $2  { print $1 }'`

if [ "x" = "x${hbaports}" ]; then
	echo "	No CONNECTED Sun StorEdge HBA ports found with luxadm -e port"
	echo "	   This script is only used to configure lus to allow access"
	echo "	   to devices connected through StorEdge Fibrechannel HBAs"
	echo
	exit 1
else
	for hba in ${hbaports}; do
		echo "  Found StorEdge HBA device:  ${hba}"
	done
fi

if [ -f /usr/kernel/drv/lus.conf.old ]; then
	rm -f /usr/kernel/drv/lus.conf.old
fi

#save existing lus.conf
mv /usr/kernel/drv/lus.conf /usr/kernel/drv/lus.conf.old

#strip off old parent="fp" live entries
# comments and blank lines are preserved
sed -n -e'/^#/p' -e'/parent="fp.*"/d' -e'/^$/p' -e'/^[^#]/p' /usr/kernel/drv/lus.conf.old > /usr/kernel/drv/lus.conf

target=0
lus_changed=NO

for hba in ${hbaports}; do
	# find all tape devices
	echo
	echo Mapping tape devices starting at target ${target}
	portwwn=`luxadm -e dump_map $hba |
		awk ' "0x1"==$7 { print $5 }'|sort`
	portwwn="$portwwn `luxadm -e dump_map $hba |
		awk ' "0x1"==$6 { print $4 }'|sort`"

	if [ "x" != "x${portwwn}" ]; then
		echo "   Adding:"
		for port in ${portwwn}; do
			echo "     port WWN: ${port} as SCSI target ${target}"
			echo "name=\"lus\" parent=\"fp\" target=${target} lun=0 fc-port-wwn=\"${port}\";" >> /usr/kernel/drv/lus.conf
			target=`expr ${target} + 1`
			lus_changed=YES
		done
	fi
done

# start jukebox addresses near the top to make sure that st doesn't claim them
# and to minimize any address changes that may occur due to configuration
# changes (adding or removing jukeboxs and/or tape drives)
#
# starting at 100 seems like a good place - room for 25 libraries and 100 tape
# drives.  If you run into an overlap, you can move the libraries up higher
# by changing this value

target=100

for hba in ${hbaports}; do
	# find all medium changers
	echo
	echo "Adding medium changer devices starting at target ${target}"
	portwwn=`luxadm -e dump_map $hba |
		awk ' "0x8"==$7 { print $5 }'|sort`
	portwwn="$portwwn `luxadm -e dump_map $hba |
		awk ' "0x8"==$6 { print $4 }'|sort`"

	if [ "x" != "x${portwwn}" ]; then
		echo "   Adding:"
		for port in ${portwwn}; do
			echo "     port WWN: ${port} as SCSI target ${target}"
			echo "name=\"lus\" parent=\"fp\" target=${target} lun=0 fc-port-wwn=\"${port}\";" >> /usr/kernel/drv/lus.conf
			target=`expr ${target} + 1`
			lus_changed=YES
		done
		echo
	fi
done

if [ ${lus_changed} = "NO" ]; then
	echo "	No appropriate devices were found to add to lus.conf"
	echo "	Will restore saved lus.conf file"

	cp /usr/kernel/drv/lus.conf.old /usr/kernel/drv/lus.conf
	exit 0
fi


# offer to unload & reload lus if lus is present in memory
lus_num=`modinfo |grep lus | awk ' 0x0!=$1 { print $1 }`;

if [ ! -z "$lus_num" ]; then
	echo
	echo The changes made to lus.conf will not take effect until lus is reloaded.
	echo
	echo If you are sure that no process is currently using lus to control
	echo      a tape library, then it may be safely unloaded.
	echo
	echo "Would you like to unload lus? (y/n) \c"
	read userin
	echo
	if [ "${userin}" = "y" -o "${userin}" = "Y" ]; then
		modunload -i ${lus_num}
		if [ $? != 0 ]; then
			echo
			echo "   ERROR: modunload -i $lus_num failed"
			echo "   You must unload and reload lus manually or reboot this system before"
			echo "   changes will take effect"
			echo
			echo

			exit 1
		fi
	fi

fi

lus_num=`modinfo |grep lus | awk ' 0x0!=$1 { print $1 }`;

if [ -z "$lus_num" ]; then

	echo "lus is not currently loaded"
	echo
	echo "Would you like to run inquire?"
	echo "  (this will cause lus to be loaded using the newly modified lus.conf) (y/n) \c"
	read userin
	echo
	if [ "${userin}" = "y" -o "${userin}" = "Y" ]; then
		/etc/LGTOuscsi/inquire
	fi
else
	echo lus is currently loaded!  It must be unloaded and reloaded
	echo for any changes to lus.conf to take effect

fi

exit 0
