#!/bin/ksh
#
#pragma ident   "@(#)disktype.m4 1.11     00/06/01 SMI"
#
# Copyright (c) 1997-1998 by Sun Microsystems, Inc.
# All rights reserved.
#
# Cluster Application Bit Assignment. Refer to the cdb file for all
# assignments

pre="SUNWcluster.disktype"
CVM=3
VxVM=4
SDS=5
 
if [ $# -ne 2 ]; then
   log_info "${pre}.1000" "disktype usage: disktype [-t | -h] diskset"
   echo "usage: disktype [-t | -h] diskset" 1>&2
   exit 1
fi
if [ "$1" = "-t" ]; then
   gettype=1
else 
   if [ "$1" = "-h" ]; then
	getloghost=1
   else 
	log_info "${pre}.1000" "disktype usage: disktype [-t | -h] diskset"
   	echo "usage: disktype [-t | -h] diskset" 1>&2
	exit 1
   fi
fi

diskset=$2
CLUSTERBIN=/opt/SUNWcluster/bin
CLUSTERVAR=/var/opt/SUNWcluster
CLUSTERETC=/etc/opt/SUNWcluster/conf
ccdfile=${CLUSTERETC}/ccd.database

loghost_row=""
CLUSTERNAME=$(/bin/cat ${CLUSTERETC}/default_clustername)
cdbfile=${CLUSTERETC}/${CLUSTERNAME}.cdb
#
# Test if the ${ccdfile} exists, if not we could be using
# a veritas volume manager and we should have access to
# the shared ccd: ccd.database.ssa
# This of course is not going to help in an SDS configuration,
# but then the ccd.database file should exist
#
if [ ! -f "${ccdfile}" ]; then
	log_info "${pre}.4350" "No ccdfile, switching to alternative"
	ccdfile=${CLUSTERETC}/ccdssa/ccd.database.ssa
fi

if [ ! -f "${ccdfile}" ]; then
	log_info "${pre}.4350" "disktype: no ccd file!"
	exit 1
fi
loghost_row=$(${CLUSTERBIN}/scccd -f ${ccdfile} ${CLUSTERNAME} LOGHOST \
      query lname \* ) 
if [ "$loghost_row" = "" ]; then
	log_info "${pre}.4350" "scccd LOGHOST failed"	 
	exit 1
fi

for r in ${loghost_row}; do
   echo ${r} | awk -F':' '{print $4}' | /usr/bin/egrep -e ${diskset} 1>/dev/null 2>/dev/null 
   ret=$?
   if [[ ${ret} -eq 0 ]]; then
	break
   fi
done

if [[ ${ret} -ne 0 ]]; then
   log_info "${pre}.3030" "The diskset ${diskset} isn't defined in ${ccdfile}"
   exit 1
fi

loghost=$(echo ${r} | awk -F':' '{ print $2 }')
if [ "${getloghost}" = "1" ]; then
	echo ${loghost}
	exit 0
fi
localhostname=$(${CLUSTERBIN}/scccd -f ${ccdfile} ${CLUSTERNAME} LOGHOST_CM \
	query lname ${loghost} | awk -F':' '{ print $3 }' ) 
pdbapps=$(${CLUSTERBIN}/cdbmatch cluster.pdbapps ${cdbfile})
vm=$(${CLUSTERBIN}/appmatch ${pdbapps} ${CVM})
if [[ "${vm}" = "1" ]]; then
  vm=cvm
else
  vm=$(${CLUSTERBIN}/appmatch ${pdbapps} ${VxVM})
  if [[ "${vm}" = "1" ]]; then
    vm=vxvm
  else
      vm=`appmatch ${pdbapps} ${SDS}`
      if [ "${vm}" = "1" ]; then
        vm=sds
        METASET=/usr/opt/SUNWmd/sbin/metaset
        METASTAT=/usr/opt/SUNWmd/sbin/metastat
      else
        log_info "${pre}.4340" \
          "Volume manager is not properly configured."
          exit 1
      fi 
  fi
fi
echo $vm
