#!/usr/bin/ksh -p
#
#    SAM-QFS_notice_begin
#
#      Solaris 2.x Sun Storage & Archiving Management File System
#
#      Copyright (c) 2006 Sun Microsystems, Inc.
#      All Rights Reserved.
#
#      Government Rights Notice
#      Use, duplication, or disclosure by the U.S. Government is
#      subject to restrictions set forth in the Sun Microsystems,
#      Inc. license agreements and as provided in DFARS 227.7202-1(a)
#      and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#      FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#      as applicable.  Sun Microsystems, Inc.
#
#    SAM-QFS_notice_end
#
#ident  "$Id: HAStoragePlus_samfs.sh,v 1.3.16.1 2006/02/08 20:04:09 jdunn Exp $"
#
# HAStoragePlus_qfs is a script to obtain the constituent components
# of a single QFS family set or mount point.  It assumes that
# HAStoragePlus has already read /etc/vfstab and determined that
# the family set ("mount special") or mount point is described as
# a 'samfs' file system.
#
#	usage: HAStoragePlus_samfs {family_set}
#
#	usage: HAStoragePlus_samfs {mount_point}
#		-- only works if {mount_point} is a mounted QFS filesystem
#

#
# Global variables
#
typeset rc

#
# Assume existence of system utilities
#
typeset -r AWK=/usr/bin/awk
typeset -r PRINT=/usr/bin/print
typeset -r LOGGER=/usr/bin/logger
typeset -r BASENAME=/usr/bin/basename
typeset -r XARGS=/usr/bin/xargs
typeset -r ECHO=/usr/bin/echo
typeset -r PGREP=/usr/bin/pgrep

# sam-qfs CLI utility
typeset -r SAMCMD=/opt/SUNWsamfs/sbin/samcmd
typeset -r SAMD=/opt/SUNWsamfs/sbin/samd

# the syslog facility to use
typeset -r SYSLOG_FACILITY=daemon

# This script
typeset -r ARGV0=$($BASENAME $0)

# Assure presence of mount_point argument
#
if [[ -z "$1" ]] ; then
	$LOGGER -p ${SYSLOG_FACILITY}.err \
		 -t "QFS.[$ARGV0]" "usage: $ARGV0 { mount_point }"
	exit 1
else
	typeset mount_point=$1
fi

$LOGGER -p ${SYSLOG_FACILITY}.info \
	-t "QFS.[$ARGV0]" "Obtain QFS volumes for mount point: $mount_point"

#
# Invoke SAMD to assure that the sam-fsd daemon is up and running
#
$PGREP "sam\-fsd" > /dev/null 2>&1
rc=$?
if [[ $rc -ne 0 ]]; then
	$SAMD config > /dev/null 2>&1
fi

#
# Perform the samcmd once to determine if the mount point exists
#
$SAMCMD N $mount_point > /dev/null 2>&1
rc=$?
if [[ $rc -ne 0 ]]; then
	$LOGGER -p ${SYSLOG_FACILITY}.err -t "QFS.[$ARGV0]" \
		"Failure to obtain QFS volumes for mount point: $mount_point"
	exit $rc
fi

#
# Perform the samcmd again to parse for QFS volumes
# NOTE: Only supports QFS volumes of the types 'mm', 'md', 'mr', and 'gNNN'
#
$SAMCMD N $mount_point 2>/dev/null | \
	$AWK '/^g[0-9]*+[ 	]|^m[dmr][	 ]/ { print $4 }' |\
	$XARGS |\
	$AWK ' { for (i = NF; i >=2; i--) { printf $i "," } print $i } '
exit 0
