#!/bin/ksh
#*******************************************************************************
#
# NAME:		raidutilsh
# SUMMARY:	%description%
# COMPONENT:	solsysd
# VERSION:	rm6_4
# UPDATE DATE:	%date_modified: Tue May 25 10:48:11 1999 %
# PROGRAMMER:	%created_by:    awilner %
# 
#		Copyright 1996, 1997 by Symbios Logic Inc.
#
# DESCRIPTION:
# raidutil is a wrapper for raidutil.exec - introduced in RM 6.00.04 to handle
# certain post-processing requirements.  Currently all this consists of is
# labelling the LUN.
#
# NOTES:
# (question, really) Can raidutil create a LUN even if no -g option is
# specified?
#
# 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()
{
	TRIES=0
	while true
	do
		$RM_HOME/bin/rm_mutex_acquire $1
		if [ $? = 0 ]
		then
			break;
		fi
		sleep 1
		TRIES=`expr $TRIES + 1`
		if [ $TRIES -ge 3600 ]
		then
			$LOGGER -p user.err "Unable to acquire mutex:  $1"
			break
		fi
	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:	raidutil (main)
# SUMMARY:	Encapsulate raidutil.exec and handle OS-specific post-processing
#
# DESCRIPTION:
# raidutil (the shell script) is simply a wrapper for raidutil.exec (the
# C-derived binary).  It performs Solaris-specific dependent processing and
# is probably unnecessary on other OS's.
#
# SYNTAX:
# (refer to the raidutil "man" page)
#
# NOTES:
#
# RETURNS:
# 0  - successful completion
# 98 - raidutil.exec program does not exist
# 99 - command line argument inconsistency detected
# XX - the exit status from raidutil.exec

PATH=$PATH:/etc/raid/bin
export PATH

DEV=""
proceed=0
PARMS=/etc/raid/rmparams
DEV_KEY=System_AltDevDirRoot
HOME_DIR_KEY=System_RmHomeDirectory
RM_HOME=`grep -v "^#" $PARMS | grep $HOME_DIR_KEY | cut -d= -f2`

RMDEVROOT=`grep "^[ \t]*$DEV_KEY" $PARMS | sed -e 's/^.*=[ \t]*//' -e 's/\/ *$//' -e 's/ *$//'`

if [ ! -x /etc/raid/bin/raidutil.exec ]
then
	exit 98
fi

# display usage
if [ "$1" = "" ]; then
   raidutil.exec $*
   exit $?
fi

# set up all parameters
i=1
for opt
do 
   ARGV[$i]=$opt
   i=`expr $i + 1`
done

# find the device and lun number
i=1
LCOUNT=0
while [ $i -le $# ]
do
  case ${ARGV[$i]} in
     -c*)
	if [ "${ARGV[$i]}" = "-c" ]; then
	   i=`expr $i + 1`	   
	   DEV=${ARGV[$i]}
	else
	   DEV=`echo ${ARGV[$i]} | cut -dc -f3`
	   DEV="c"$dev
	fi
	;;
     -n*)
	if [ "${ARGV[$i]}" = "-n" ]; then
	   i=`expr $i + 1`	   
	   LUNS=${ARGV[$i]}
	else
	   LUNS=`echo ${ARGV[$i]} | cut -dn -f2`
	fi
	
	# Load LUN number array with args to -n
	l=0
	for LUN in `echo $LUNS | sed -e 's/,/ /g'`
	do
	   l=`expr $l + 1`	  
	   LUNLIST[$l]=$LUN
	done
	LCOUNT=$l
        ;;
     -g*)
	proceed=1
	;;
  esac
  i=`expr $i + 1`
done

SHORTDEV=`echo $DEV | cut -dd -f1`

# pre-cautions
if [ "$DEV" = "" ]; then
   proceed=0
fi

if [ $proceed -eq 1 -a $LCOUNT -eq 0 ]; then
   echo "raidutil: -n parameter is required for LUN creation" 1>&2
   exit 99
fi

# If not doing LUN creation, proceed with regular call to raidutil, and
# don't bother with RDAC node registration.
if [ $proceed -eq 0 ]; then
   raidutil.exec $*
   exit $?
fi

sleep 5
raidutil.exec $*

raidutilstat=$?

MutexAcquire /tmp/mutex.add_disk        # Begin critical region

formatstat=0
i=1
while [ $i -le $LCOUNT ]
do
	NODE=${SHORTDEV}d${LUNLIST[$i]}s0
	/usr/sbin/format -f ${RM_HOME}/format.cmds -s ${RMDEVROOT}/${NODE}
	if [ $formatstat -eq 0 ]
	then
		formatstat=$?
	fi
	i=`expr $i + 1`
done

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

if [ $raidutilstat -ne 0 ]
then
	exit $raidutilstat
fi
if [ $formatstat -ne 0 ]
then
	exit $formatstat
fi
exit 0
