# ksh functions() for Solstice DiskSuite.
# version $(metafunver)

function metafunver
  {
  echo 23
  return 0
  }
  
function formcopy
  {
  prtvtoc $1 | fmthard -s - $2
  }
 
function metaformcopy
  {
  formcopy /dev/rdsk/$1s2 /dev/rdsk/$2s2
  }
 
# Given a mount point, MUST BE MOUNTED
# returns device file.

function metadev
  {
  df -k $1 | grep $1 | head -1 | awk '{ print $1 }' 
  return
  }
 
function metardev
  {
  metadev $1 | sed 's/dsk/rdsk/'
  return
  }

# Given a dNN, return mount point.
function metamp
  {
#  df -k /dev/md/dsk/$1 | grep $1 | cut -d' ' -f6
  df -k /dev/md/dsk/$1 | grep $1 | awk '{print $6}'
  return
  }
    
#Given parent, return submirrors.
function metasubs
  {
  metastat $1 | grep 'Submirror of' | cut -d: -f1
  return
  }
  
function metalastsub
  {
  metasubs $1 | tail -1
  return
  }

#given dNN, return Type_of_metadevice.
function metatype
  {
  metastat $1 | grep $1 | head -1 | awk '{print $2}' 
  return
  }
  
#Given trans dNN, return its Master device (data)
function metamaster
  {
  metastat $1 | grep 'Master Device' | cut -d: -f2 | sed 's/ //g'
  return
  }
function metasize
  {
  devinfo -p /dev/md/rdsk/$1 | awk '{ print $5 }'
  return
  }
  
# returns unique list of slice devices.

function metadbdevs
  {
  metadb -s rsg | grep /dev | awk '{ print $5 }' | sort -u 
  return
  }
  
function metagrow
  {
  growfs -M $1 $(metardev $1)
  return
  }
    
# Returns state field : Okay,Initialising
# Good for Raid device which will fail if used while syncing.
# Mirror does not matter as you can I/O while it syncs.

function metastatus
  {
  status=$(metastat $1 | grep State | head -1 | awk '{ print $2 }' )
  if [ -z ${status} ] ; then
      status=Okay     
  fi
  if [ "Start" = "${status}" ] ; then
      status=Okay     
  fi
  echo ${status}
  return
  }
  
#  returns an integer percentage of how complete a sync operation.

function metaprogress
  {
  val=$( metastat $1 | grep progress | head -1 | awk '{ print $4 }' )
  if [ -z "${val}" ] ; then
      val=100                   # No "progress" string = complete.
  fi
  val=${val%\%}                 # strip %
  echo ${val}
  return
  }

# Wait for a Raid device to be ready for I/O.
# A raid device can be mounted after creation but will fail until
# syncing is finished.

function metawait 
  {
  shortest=5         # sleep timer interval in seconds
  longest=120        # sleep timer interval in seconds

  let timer=2        # 2,3,4,6,9,13,24... #
  let progress=0
  let lastprog=0
  let deltaprog=0    # 0 <-> ~15 #

  echo Waiting on $1 ...

  while [ true ]
  do
    stat=$(metastatus ${1})
    
    let lastprog=${progress}
    progress=$( metaprogress ${1} )
    let deltaprog=${progress}-${lastprog}
    [ "${progress}" = "100" ] &&    break
    
#   if progress is big, increase timer
#   if progres is small, increase timer

    if [ $progress -gt 90 ] ; then
      [ $deltaprog -lt 1  ]   &&  let timer=${timer}*3/2
      [ $deltaprog -gt 1  ]   &&  let timer=${timer}/2
    elif [ $progress -gt 80 ] ; then
      [ $deltaprog -lt 1  ]   &&  let timer=${timer}*3/2
      [ $deltaprog -gt 4  ]   &&  let timer=${timer}*2/3
    else
      [ $deltaprog -lt 5  ]   &&  let timer=${timer}*3/2
      [ $deltaprog -gt 10 ]   &&  let timer=${timer}*2/3 
    fi

    [ $timer -lt $shortest ] && timer=$shortest
    [ $timer -gt $longest  ] && timer=$longest  
    if [[ $deltaprog -gt 0 || $progress -eq 0 ]] ; then
        print "  ${1} ${stat},  ${progress}% complete.        (${timer})"
    fi
    sleep $timer 
  done
  echo ${1} is ready.
  return
  }
  

#function metarat
#  {
#  metareattach $1 $2
#  return $?
#  }
function metareattach
  {
  metadetach $1 $2
  if [ $? -ne 0 ] ; then
      return $?
  fi
  metattach  $1 $2 
  return $?
  }
  
# metafs dNN [type]
#  
function metafs
  {

  fs_typ=$3
  if [ -z "${fs_typ}" ] ; then
      fs_typ=$( fstyp /dev/md/dsk/$1 )
  fi
  if [ -z "${fs_typ}" ] ; then
      fs_typ="ufs"
  fi
  if [ "${fs_typ}" != vxfs ] ; then
      fs_typ="ufs"
  fi
  
# Ok, we know what they want, but can we do it.

  if [ "${fs_typ}" = "vxfs" ] ; then
      if [ -x /usr/lib/fs/vxfs/mkfs ] ; then
          continue
      else
          echo Warning : vxfs not installed, using ufs
          fs_typ=ufs
      fi
  fi
  
  case ${fs_typ} in
      ufs) 
          newfs -m 1 -i 8192 /dev/md/rdsk/$1 <<END
y
END
          code=$?
          # ufs_options=""   # -o largefiles
          # echo /usr/lib/fs/ufs/mkfs   -F ufs  ${ufs_options}   /dev/vx/rdsk/rootdg/$1
          # /usr/lib/fs/ufs/mkfs   -F ufs  ${ufs_options}   /dev/vx/rdsk/rootdg/$1
          break
          ;;
      vxfs) 
          /usr/lib/fs/vxfs/mkfs -F vxfs -o largefiles  /dev/md/rdsk/$1
          code=$?
          break          
          ;;
      *)
          echo Error : unknown fs type of "${fs_typ}" on $1 will not be made on $2
          return 1    # should not get here
  esac
  [[ $code -ne 0 ]] && return ${code} 
  
  # If they want, mount it on $2
  if [[ -n $2 ]] ; then
      metamount $1 $2 ; code=$?
  fi
  return ${code} 
  }    
#  
  
# Simple enhanced mount.
# mount dN /mnt

function metamount
  {
  if [[ ! -d $2 ]] ; then
    echo mkdir -p $2 ; mkdir $2
  fi
  
  fs_typ=$( fstyp /dev/md/dsk/$1 )
  case ${fs_typ} in
      ufs) 
          mount /dev/md/dsk/$1  $2 
          break
          ;;
      vxfs)
          mount -F vxfs -o largefiles  /dev/md/dsk/$1  $2 
          break          
          ;;
      *)
          echo Error : unknown fs type of "${fs_typ}" on $1 will not be mounted on $2
          return 1
  esac
  
  df -k $2
  }

function relog
  {
  dev=$(metadev $1)
  mount -o remount,logging $dev $1
  return
  }
    
function yes_or_no
    {
    yorn=$(ckyorn -Q)
    if [ ${yorn} = "Y" ] || [ ${yorn} = "y" ] ; then
            echo "y" ; return 0
    else
            echo "n" ; return 1
    fi
    }
 
  
#end
alias rat="metareattach $1 $2"
alias fs="metafs $1 $2"
alias en="metareplace -e $1 $2"
alias wt="metawait $1"
alias stat="metastat $1"

#
