#!/bin/sh 

VERITAS_VXVM_PKG_NAME1=VRTSvxvm
VERITAS_VXVM_PKG_NAME2=SUNWvxvm
dont_vxdmp_file=./dontresolvedmp

#--------------------------------------------------------------------------
# s_prog()
#   Returns calling script name.  
#--------------------------------------------------------------------------
s_prog() {
  basename $0
}

#--------------------------------------------------------------------------
# create_tmpfile()
#   Creates a tmp file name.
#
#   Arg_1: unique identifier, optional, defaults to PID
#--------------------------------------------------------------------------
create_tmpfile() {
  temp_file="/tmp/`s_prog`_$$_${1:-$$}.tmp"
  rm -f $temp_file
  touch $temp_file
  echo $temp_file
}

#--------------------------------------------------------------------------
# rm_tmpfile()
#   Delete tmp file(s).  Deletes all files with this PID if no argument is
#   given
#
#   Arg_1: unique identifier
#--------------------------------------------------------------------------
rm_tmpfile() {
  rm -f /tmp/`s_prog`_$$_${1:-""}*.tmp
}

#--------------------------------------------------------------------------
# strips the slice number of non-enclosure named disk names.
# 
# Arg_1: name of the device, e.g., c0t0d0s2
#--------------------------------------------------------------------------
strip_slice()
{
#  tst1=`echo $1 | sed -n '/c[0-9].*t*d[0-9].*s[0-9]/p'`
#  tst2=`echo $1`
#  if [ "$tst1" = "" ]
#  then
#    disk_name=$tst2
#  else
#    tst1=`echo "$tst1" | sed 's/s[0-9]$//'`
  disk_name=$1
  if echo $1 | grep -s "s[0-9]$" > /dev/null; then
    tst1=`echo "$1" | sed 's/s[0-9]$//'`
    disk_name=$tst1
  fi  
}

#--------------------------------------------------------------------------
# make_dmp_map_entry()
# Routine for creating a dmp multipath entry for a given device 
# Arg_1: dmpnodename 
# Arg_2: dmp paths for the given dmpnodename
#--------------------------------------------------------------------------
make_dmp_map_entry()
{
  if [ "x$1" = "x" ] 
  then
      return 1
  fi

  strip_slice $1
  device_name=$disk_name
  # First write name of the dmpnodename
  printf "%s " $device_name >> $vxvm_dmp_map

  # Now - write each and every path for a given device
  for apath in `echo $dmp_paths` 
  do
      strip_slice $apath
      path_name=$disk_name

      printf "%s " $path_name >> $vxvm_dmp_map    
  done
  printf "\n" >> $vxvm_dmp_map

  return 0
} 

###########################################################################
# 
# Start of script processing

# Handle arguments
if [ "$1" = "-vxlist" ]; then
    dump_vxlist=true
fi

sid=$SRMAGT_SCAN_ID
if [ "$sid" = "" ]
then
  sid="no_sid"
fi

# Set names for some temporary files
vx_encl_file="/tmp/GetVxvmEnclosures.vxvmdisks.$sid"
non_vx_encl_file="/tmp/GetVxvmEnclosures.non.$sid"
#vxvm_dmp_map="/tmp/GetLocalDiskList.dmp_map.$sid"

vxlist=""
if  [ -f $dont_vxdmp_file ]
then
    exit 0
fi

# check for use of veritas... make sure to handle enclosure names properly...
# especially when removing slices from some disk names.
pkginfo -q $VERITAS_VXVM_PKG_NAME1
if [ $? -ne 0 ]
then
  # try Sun-branded package name
    pkginfo -q $VERITAS_VXVM_PKG_NAME2
  if [ $? -ne 0 ]
  then
    # vxvm not found, exit now
        exit 0      
  fi
fi
  
# create file to store LID and GID of disk names for enclosures to be used
# in getdiskinfo... will be deleted in EndScan.sh
if [ -f $vx_encl_file ]
then
    rm -f $vx_encl_file
fi
touch $vx_encl_file

if [ -f $non_vx_encl_file ]
then
    rm -f $non_vx_encl_file
fi
touch $non_vx_encl_file

# Create a file to store the Veritas-DMP mapping between dmpnodename and 
# corresponding paths
#if [ -f $vxvm_dmp_map ]
#then
#  rm -f $vxvm_dmp_map
#fi
#touch $vxvm_dmp_map

# Generate list of disks
tmp_vxdisks_file=`create_tmpfile vxdisks`
vxdisk list | grep -v "DEVICE" | awk '{printf("%s %s\n",$1,$5);}' > $tmp_vxdisks_file
vxlist=`cat $tmp_vxdisks_file | awk '{print $1}'`

# check for tags 14 or 15 on disks showing as error. Proceed on those, otherwise
# they are not under vertias control and should be using the device name not the enclosure name
err_disks=`cat $tmp_vxdisks_file | grep "error" | awk '{print $1}'`
for z in $err_disks
do
    dev_name=`vxdmpadm getsubpaths dmpnodename=$z 2>/dev/null | grep "ENABLED" | awk '{print $1}' | sed -n '1,1p'`
    tag_flag=`prtvtoc "/dev/rdsk/"$dev_name | grep -v "*" | 
              awk '$2 ~ /^(14|15)$/ {print "t"}'`
                       
    if [ "$tag_flag" ]
    then
        continue
    else
        # not controlled by vxvm
        strip_slice $dev_name
        echo $disk_name >> $non_vx_encl_file
    fi
done
 
vxlist_back=$vxlist

# Build list of enclosure based names and their raw device names
vxvm_encs=`create_tmpfile encs`
for x in $vxlist  
do
#  vxdmpadm_output=`vxdmpadm getsubpaths dmpnodename=$x 2> /dev/null | grep -v "NAME" | grep -v "=" | tr '\n' '#' `
  # again, be careful of enclosure names when dealing with slice 
  enclosure=`vxdmpadm getsubpaths dmpnodename=$x 2> /dev/null | grep "ENABLED" | awk '{print $1}' | sed -n '1,1p'`
#  enclosure=`echo $vxdmpadm_output | awk 'BEGIN { RS = "#" } { print $0}' | grep "ENABLED" | awk '{ print $1}' | sed -n '1,1p' `

  # Make an entry in the Veritas DMP - multipath map file
#  dmp_paths=`echo $vxdmpadm_output | awk 'BEGIN { RS = "#" } { printf("%s ", $1);}'`
#  make_dmp_map_entry $x $dmp_paths

  strip_slice $enclosure
  enclosure=$disk_name
    
  if [ "-" = "$x" ] 
  then
      # when we have a disk thats in a not-good state, this raw device name
      # can come back as a "-" in which case lets not process this item
      continue 
  fi

  is_vx=`grep "$disk_name$" $non_vx_encl_file`
  if [ "$is_vx" != "" ]
  then
    # if it's a simple or nopriv device, report it anyway
    type=`vxdisk list $x | awk '/^type:/ {print $2}'`
    if [ "$type" = "simple" ]; then :
    elif [ "$type" = "nopriv" ]; then :
    else
    	x=$disk_name
    fi
  fi

  if [ "$enclosure" = "" ]
  then
    enclosure=$x
  fi 

  if [ "$enclosure" != "$x" ] 
  then
    for y in $enclosure
    do
      strip_slice $x
      x=$disk_name
      enc=`echo "$x $y"`
      echo "$enc" >> $vxvm_encs
       echo "$enc" >> $vx_encl_file
    done
  else
    echo "$enclosure" >> $vxvm_encs
  fi
done

if [ "$dump_vxlist" ]; then
    cat $vxvm_encs | uniq
fi

rm_tmpfile
