#!/bin/bash

logFile=/var/tmp/vxvm_`date "+%Y%m%d"`.txt
tmpLogFile=/tmp/vxvmtmp.$$

usage()
{
cat << EOF
usage: $0 options

OPTIONS:
   -h      Show this message
   -n      Search for those disks already but not yet assigned
EOF
}

determineOS() {
if uname -a | grep Linux ; then
  Linux=1
  AWK=awk
  mntLocation=3
  rdsk=""
  s2=""
else
  AWK=nawk
  mntLocation=1
  rdsk="rdsk/"
  s2="s2"
fi
}

captureOldConfig() {
[[ ! -e /var/tmp/df-h_`date "+%Y%m%d"`.txt ]]        && df -h > /var/tmp/df-h_`date "+%Y%m%d"`.txt
[[ ! -e /var/tmp/inq_`date "+%Y%m%d"`.txt ]]         && inq > /var/tmp/inq_`date "+%Y%m%d"`.txt
[[ ! -e /var/tmp/vxdisk_list_`date "+%Y%m%d"`.txt ]] && vxdisk -o alldgs list > /var/tmp/vxdisk_list_`date "+%Y%m%d"`.txt
[[ ! -e /var/tmp/vxprint-ht_`date "+%Y%m%d"`.txt ]]  && vxprint -ht > /var/tmp/vxprint-ht_`date "+%Y%m%d"`.txt
}

scanDisks() {
echo "Scanning disk, please wait.. (to skip this part, you may use $0 -n)"
if [[ $Linux ]] ; then
  lun_scan.sh
else
  cfgadm -c configure `cfgadm -l 2> /dev/null | $AWK '$2 == "fc-fabric" {print $1}'`
  devfsadm -Cc disk
  devfsadm
fi

vxdctl enable
vxddladm set namingscheme=osn
}

displaySummary() {
[[ ! $scanned ]] && scanDisks

echo "Generating disk summary, please wait.."

disklist=`vxdisk -o alldgs list | $AWK '$3 == "-" && $4 == "-" {print length($1),$1 | "sort -n"}' | awk '{print $2}' | sed 's/s2$//'`
inqmsg=`inq 2>/dev/null `
inqmsg=`inq 2>/dev/null `
diskmsg=`for i in $disklist ; do echo -e "$inqmsg" | grep -v rdmp | $AWK '$1 == "/dev/'"$rdsk$i$s2"'" {print $1"\t"int($NF/1024/1024+0.5)"g"}'; done`

echo -e "$diskmsg"
}

diskAction() {
echo "###############################################"
cat $tmpLogFile
echo "###############################################"
echo "Confirm to do so (y/n) ?"
local confirm="" ; while [[ (("$confirm" != "y")) && (("$confirm" != "n")) ]] ; do read -n 1 confirm ; echo ; done
[[ "$confirm" == "y" ]] && bash -x $tmpLogFile 2>&1 | tee -a $logFile
cat /dev/null > $tmpLogFile
}

diskAllocation() {
cat /dev/null > $tmpLogFile
for i in $disklist; do
  diskInfo=`echo -e "$diskmsg" | awk '$1 == "/dev/'"$rdsk$i$s2"'"' | sed 's/ / (/'`")"
  echo "**************** $diskInfo ****************"
  echo "Allocation for $i (y/n) ?"
  ans="" ; while [[ (("$ans" != "y")) && (("$ans" != "n")) ]] ; do read -n 1 ans ; echo ; done
  [[ "$ans" == "n" ]] && continue

  echo "Format $i (y/n) ?"
  ans="" ; while [[ (("$ans" != "y")) && (("$ans" != "n")) ]] ; do read -n 1 ans ; echo ; done
  if [[ "$ans" == "y" ]] ; then
 [[ $Linux ]] && echo "echo w | fdisk /dev/`vxdisk list $i | awk '/type=primary/ {print $1}'`" >> $tmpLogFile
 [[ `uname -p` == "sparc" ]] && echo "printf \"label\ny\nquit\n\" | format -d `vxdisk list $i | awk '/type=primary/ {print $1}' | sed 's/s2$//'`" >> $tmpLogFile
 echo "/usr/lib/vxvm/bin/vxdisksetup -if $i" >> $tmpLogFile
  fi
  echo "Please enter Disk Group Name for $i" ; read vxvmDiskGrp
  diskName="${vxvmDiskGrp}01"
  if [[ `vxdisk -o alldgs list | grep $vxvmDiskGrp | wc -l` -gt 0 ]]; then
 let lastNum=`vxdisk -o alldgs list | $AWK '$4 == "'"$vxvmDiskGrp"'" {print substr($3, length($3)-1)}' | sort -n | tail -1`+1
 diskName=`printf "%s%02d" $vxvmDiskGrp $lastNum`
 echo "vxdg -g $vxvmDiskGrp adddisk $diskName=$i" >> $tmpLogFile
  else
 echo "vxdg init $vxvmDiskGrp $diskName=$i" >> $tmpLogFile
  fi
 
  echo "Allocate volume now (y/n) ?"
  ans="" ; while [[ (("$ans" != "y")) && (("$ans" != "n")) ]] ; do read -n 1 ans ; echo ; done
  [[ "$ans" == "n" ]] && diskAction

  while [[ "$ans" == "y" ]] ; do
 echo "Please enter Volume Name for Disk Group $vxvmDiskGrp" ; read vxvmVolName
 echo "Please enter size to be added in mb" ; read vxvmSize
 echo "Raw device (y/n) ?"
 rawDeivce="" ; while [[ (("$rawDeivce" != "y")) && (("$rawDeivce" != "n")) ]] ; do read -n 1 rawDeivce ; echo ; done
 if [[ "$rawDeivce" == "y" ]] ; then
  echo "Please enter username for the volume" ; read vxvmUser
  echo "Please enter group for the volume" ; read vxvmGrp
  echo "vxassist -g $vxvmDiskGrp make $vxvmVolName ${vxvmSize}m" >> $tmpLogFile
  echo "vxedit -g $vxvmDiskGrp set user=$vxvmUser group=$vxvmGrp $vxvmVolName" >> $tmpLogFile
 else
  echo "Please enter mount point" ; read vxvmMountPoint
  if [[ `mount | $AWK '$'"$mntLocation"' == "'"$vxvmMountPoint"'"' | wc -l` -ne 0 ]] ; then
   echo "Mount point $vxvmMountPoint already exists! Will resize the volume, current size shown below"
   df -h $vxvmMountPoint
   echo "/usr/lib/vxvm/bin/vxresize -F vxfs -g $vxvmDiskGrp $vxvmVolName +${vxvmSize}m" >> $tmpLogFile
  else
   echo "New Mount point $vxvmMountPoint will be created"
   echo "vxassist -g $vxvmDiskGrp make $vxvmVolName ${vxvmSize}m" >> $tmpLogFile
   if [[ $Linux ]] ; then
    echo "echo \"/dev/vx/dsk/$vxvmDiskGrp/$vxvmVolName  $vxvmMountPoint vxfs       _netdev,suid          0 0\" >> /etc/fstab" >> $tmpLogFile
    echo "mkfs.vxfs -o largefiles /dev/vx/rdsk/$vxvmDiskGrp/$vxvmVolName" >> $tmpLogFile
   else
    echo "echo \"/dev/vx/dsk/$vxvmDiskGrp/$vxvmVolName /dev/vx/rdsk/$vxvmDiskGrp/$vxvmVolName $vxvmMountPoint vxfs 1 yes largefiles,suid\" >> /etc/vfstab" >> $tmpLogFile
    echo "mkfs -F vxfs -o largefiles /dev/vx/rdsk/$vxvmDiskGrp/$vxvmVolName" >> $tmpLogFile
   fi
   echo "mkdir -p $vxvmMountPoint" >> $tmpLogFile
   echo "mount $vxvmMountPoint" >> $tmpLogFile
   echo "df -h $vxvmMountPoint" >> $tmpLogFile
  fi
 fi
 diskAction
 echo "Anymore volume in this disk group (y/n)?" ; read -n 1 ans ; echo
  done
 
done

rm -f $tmpLogFile
}

main () {
determineOS
captureOldConfig
displaySummary
diskAllocation
}

while getopts ":hn" option
do
  case $option in
    h     ) usage ; exit 0 ; shift;;
    n     ) scanned=1 ; shift;;
    *     ) echo "Invaild option chosen."; shift ;;
  esac
done

main