#!/bin/ksh -p
#*******************************************************************************
#
# NAME:		rdac_name_change
# SUMMARY:	%description%
# COMPONENT:	solsysd
# VERSION:	rm6_1
# UPDATE DATE:	%date_modified: Mon Jun 25 15:11:13 2001 %
# PROGRAMMER:	%created_by:    jbeadles %
# 
#		Copyright 1998 by Symbios Logic Inc.
#
# DESCRIPTION:
# rdac_name_change carries out those operations needed for SYMplicity storage manager
# to be able to see a new array module.  
#
# NOTES:
#
# REFERENCE:
#
# CODING STANDARD WAIVERS:
#
#*******************************************************************************

#*******************************************************************************
# PROCEDURE:	MutexAcquire
# SUMMARY:	get exclusive ownership of mutex file
#
# DESCRIPTION:
# MutexAcquire will try repeatedly to acquire a "mutex file" - success means
# the thread has been admitted to a "critical region"
#
# SYNTAX:
# MutexAcquire <mutex file>
# <mutex file> - name of the mutex guarding the critical region
#
# NOTES:
# MutexAcquire and the program it calls, rm_mutex_acquire, rely on O_CREAT
# semantics
#
# RETURNS:
# Nothing

MutexAcquire()
{
	while true
	do
		$RM_HOME/bin/rm_mutex_acquire $1
		if [ $? = 0 ]
		then
			break;
		fi
		sleep 1
	done;
}

#*******************************************************************************
# PROCEDURE:	MutexRelease
# SUMMARY:	relenquish mutex file ownership
#
# DESCRIPTION:
# MutexRelease releases mutex file ownership, so another thread can enter the
# critical region
#
# SYNTAX:
# MutexRelease <mutex file>
# <mutex file> - name of the mutex guarding the critical region
#
# NOTES:
#
# RETURNS:
# Nothing

MutexRelease()
{
	/bin/rm $1;
}

#*******************************************************************************
# PROCEDURE:	rdac_name_change (main)
# SUMMARY:	Change RAID Module names in the rdac_address file
#
# DESCRIPTION:
# 
#
# SYNTAX:
# rdac_name_change OldName NewName
#
# NOTES:
#
# RETURNS:
# 0     - success
# non-0 - failure (currently not returned)


# R D A C _ N A M E _ C H A N G E   M A I N   P R O C E D U R E
#

ADDRESS_FILE="/etc/raid/rdac_address"
RMPARAMS_FILE=/etc/raid/rmparams
RM_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmHomeDirectory | cut -d= -f2`
RM_BOOT_HOME=`grep -v "^#" $RMPARAMS_FILE | grep System_RmBootHomeDirectory | cut -d= -f2`
MutexAcquire /tmp/mutex.rdac_name_change	# Begin critical region

OLD_NAME=$1
NEW_NAME=$2
OLD_ENTRY=`cat $ADDRESS_FILE | grep "^${OLD_NAME}:" | cut -d: -f2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33`

if [ -n "${OLD_ENTRY}" ]
then
	NEW_ENTRY=$NEW_NAME:$OLD_ENTRY
	cat $ADDRESS_FILE | grep -v "^${OLD_NAME}:" >/tmp/rdac_address.$$
	echo "$NEW_ENTRY" >>/tmp/rdac_address.$$
	cp  /tmp/rdac_address.$$ $ADDRESS_FILE
	rm  /tmp/rdac_address.$$
fi
MutexRelease /tmp/mutex.rdac_name_change	# End of critical region


ISALIST_EXIST=`which isalist | grep no`
if [ -z "${ISALIST_EXIST}" ]
then
        ISALIST="isalist"
else
        ISALIST="echo not"
fi

MutexAcquire /tmp/mutex.hot_add	# Begin critical region
$RM_BOOT_HOME/symconf >/kernel/drv/rdriver.conf 
if [ $? != 0 ]
then
	echo "Array Name Change error: Unable to build the RDAC Driver conf file"
	MutexRelease /tmp/mutex.hot_add	# End of critical region
	exit 1
fi

MutexRelease /tmp/mutex.hot_add	# End of critical region

exit 0
