#!/usr/bin/pfksh
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)create_metasets	1.12	01/10/02 SMI"
#
# This script reads the contents of the node's /var/cluster/spm/metaset_db file
# and then proceeds to make the metaset.  The contents of the
# /var/cluster/spm/metaset_db file hold the disk name's in CTD format so we must
# read each line and then convert the CTD names into DID names.

SUNWscvw_dir=/opt/SUNWscvw/cgi-bin/installation/sds

# The three line formats of the input file are:
# 1.	metaset -s <metasetname> -a -h <host>
# 2. 	metaset -s <metasetname> -a <ctd_disk1> <ctd_disk2>
# 3.	metaset -s <metasetname> -a -m <host1> <host2>

function process {
	line=$1
	echo $line | /usr/bin/grep ' \-h ' > /dev/null
	hostline=$?
	echo $line | /usr/bin/grep ' \-m ' > /dev/null
	mediatorline=$?
	if [[ $hostline -eq 0 || $mediatorline -eq 0 ]]
	then
	# Line format #1 or #3:
		# Add hosts to the metaset
		# Just eval the line - no need to do anything special
		print $line
		eval $line
	else
	# Line format #2:
		# Add the disks to the metaset
		# Before we eval the line, we must convert the CTD to DID
		ctd_disk1=$(echo $line | awk '{print $5}')
		ctd_disk2=$(echo $line | awk '{print $6}')

		did_disk1=$($SUNWscvw_dir/ctd2did $ctd_disk1)
		did_disk2=$($SUNWscvw_dir/ctd2did $ctd_disk2)

		# Construct the new line with DID disks
		newline=$(echo $line | awk '{ $5 =""; $6 =""; print}')
		newline=$(echo $newline /dev/did/dsk/$did_disk1 /dev/did/dsk/${did_disk2})
		# Now invoke the line
		print $newline
		eval $newline

	fi
}

FILE=/var/cluster/spm/metaset_db
if [[ ! -f $FILE ]]
then
	echo "Node `uname -n` is not responsible for creating any metasets."
else
	# Log our execution of /var/cluster/spm/metaset_db

	exec 0<$FILE
	while read line
	do
		process "$line"
	done
fi
