#!/bin/sh
# $Id: lus_add_fp_devs.sh,v 6.6.54.4 2002/12/19 20:02:27 chejlava 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

#find location of inquire
inquire_bin=none
for pathname in /usr/sbin/nsr /usr/sbin /etc/LGTOuscsi; do
	if [ -f ${pathname}/inquire ]; then
		inquire_bin=${pathname}/inquire
		break;
	fi
done

#remove explicit driver names from the test.  Any port that luxadm shows us is a valid
# HBA port, so just pick out any lines that represent CONNECTED port listings.
hbaports=`luxadm -e port | grep CONNECTED |
	awk ' "CONNECTED" == $2  { print $1 }'`

if [ "x" = "x${hbaports}" ]; then
	echo "	No CONNECTED Sun StorEdge(TM) HBA ports found with luxadm -e port"
	echo "	   This script is only used to configure lus to allow access"
	echo "	   to devices connected through Sun StorEdge(TM) Fibrechannel HBAs"
	echo
	exit 1
else
	for hba in ${hbaports}; do
		echo "  Found Sun StorEdge(TM) 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
	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
		echo Mapping tape devices starting at target ${target} connected via
		echo "   HBA  "$hba
		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
	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
		echo "Adding medium changer devices starting at target ${target} connected via"
		echo "   HBA  "$hba
		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 load and configure lus using the newly modified lus.conf? (y/n) \c"
	read userin
	echo
	if [ "${userin}" = "y" -o "${userin}" = "Y" ]; then
		find /devices -name "lus*" -print |xargs rm
		drvconfig -i lus
		if [ $? != 0 ]; then
			echo
			echo "   ERROR: drvconfig -i lus failed"
			echo "   You must reload lus manually or reboot this system before"
			echo "   changes will take effect"
			echo "   You should also run this command to delete any existing"
			echo "   lus device files to make sure that what lus sees matches"
			echo "   what is really present on the system:"
			echo
			echo "        find /devices -name \"lus*\" -print |xargs rm"
			echo
			echo "   Run
			echo "        drvconfig -i lus
			echo
			echo "   afterward to rebuild the necessary lus device files"
			echo

			exit 1
		fi

		echo "Would you like to run inquire? (y/n) \c"
		read userin
		echo
		if [ "${userin}" = "y" -o "${userin}" = "Y" ]; then
			if [ "${inquire_bin}" = "none" ]; then
				echo "          We are unable to find the 'inquire' program. You should run it manually"
			else
				${inquire_bin}
			fi
		fi
	fi
else
	echo "   lus is currently loaded!  It must be unloaded and reloaded"
	echo "       for any changes to lus.conf to take effect"
	echo
	echo "   You should also run this command to delete any existing"
	echo "   lus device files to make sure that what lus sees matches"
	echo "   what is really present on the system:"
	echo
	echo "        find /devices -name \"lus*\" -print |xargs rm"
	echo
	echo "   Run
	echo "        drvconfig -i lus
	echo
	echo "   afterward to rebuild the necessary lus device files"

fi




exit 0
