#!/usr/bin/ksh
#
#
# Author Bobby Chamness: Enterprise Backup Production Engineering
#
# useage: ./update_drive_serial_number.sh
#
# For Netbackup 4.5 FP6 Use only
# Description: This script takes input from the user, by requesting the drive name to be update. 
#              The inputed drive is check to see if its a valid name if so it then finds the 
#              device path to get the new serial number and to see if its seen at the os level 
#              for netbackup. If the drive is not seen at the os level, the script returns contact
#               prod. engineering. If its seen at the os level for netbackup then the WWPN was set
#              properly. After it has the new s/n it checks the vmglob for the old s/n. Now
#              that old and new s/n are known compare the two values to see if there different.
#              If they are different find all the media servers that share this drive. Once all
#              the servers are known determine if the server is windows or unix, then do a vmoprcmd
#              to update the drive and then sync the glob. After the drive is updated on all the 
#              hosts validate the it was updated properly by rechecking vmglob. If the vmglob is 
#              not correct they'll be a response to contact prod. engineering.
#
#
sudo=''
if [ `id | awk -F\( '{ print $1 }' | awk -F= '{ print $2 }'` -ne 0 ]; then
sudo="sudo "
fi
#
#
#
# debugger
#set -x
#
VOLMGR_DIR=/usr/openv/volmgr/bin
NBU_MASTER=`/usr/openv/netbackup/bin/bpclntcmd -self | grep gethostname | awk '{ print $(NF ) }'`
#
#
# get the drive name to be updated
echo "Enter the name of the drive to update ==>\c"
read DRIVE_NAME
# Check to see if the drive exist in the vmglob
VALID_DRIVE=`$sudo $VOLMGR_DIR/vmglob -listall -java | grep -w $DRIVE_NAME`
RC=$?
if [ $RC -eq 1 ]
then
    echo "The drive is not a valid name in the vmglob, please check the name again"
    exit 1
fi
echo "Is this the drive $DRIVE_NAME you want updated?, enter y/n ==>\c"
read ANSWER
if [ $ANSWER = y -o $ANSWER = Y ]
then
      # that gives the device path. variable is DEV_PATH
      DEV_PATH=`$sudo $VOLMGR_DIR/tpconfig -l | grep -w $DRIVE_NAME | awk '{ print $(NF - 4) }'`
      #
      # this gives the serial number NEW_SERIAL_NUM
      NEW_SERIAL_NUM=`$sudo $VOLMGR_DIR/tpautoconf -t | grep $DEV_PATH | awk '{ print $(NF - 7) }'`
      RC=$?
      # test statement to check to see if drive is seen at os level for NBU
      if [ $RC -eq 1 ]
      then
            echo "The drive is not seen at the OS level please contact Production Egineering"
            exit 1
      else
            echo "The drive is seen at the OS level and the new serial number is $NEW_SERIAL_NUM"
      fi
      #
      # this gives the glob s/n  OLD_SERIAL_NUM 
      OLD_SERIAL_NUM=`$sudo $VOLMGR_DIR/vmglob -listall -java | grep -w $DRIVE_NAME | grep $NBU_MASTER | \
      awk {'print $4'} | sort | uniq`
      echo "The vmglob serial number $OLD_SERIAL_NUM"
      #
      # Compares the new serial number to the old serial number
      if  [ $NEW_SERIAL_NUM != $OLD_SERIAL_NUM ] 
      then
           # update the vmglob
           # gets the servers 
           echo "new s/n $NEW_SERIAL_NUM is different from vmglob s/n $OLD_SERIAL_NUM"
           DRIVE_HOSTS=`$sudo $VOLMGR_DIR/vmglob -listall -java | grep -w $DRIVE_NAME | awk '{print $5}'`
           for HOSTS in $DRIVE_HOSTS
           do
              DEVICE_FILE=`$sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig -l | grep $DRIVE_NAME | \
              awk '{ print $(NF -4) }' | grep -i tape`
              RC=$?
              if [ $RC -eq 0 ]
              then
                  WIN_DEVICE_FILE=`$sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig -l | grep $DRIVE_NAME | \
                  awk '{ print $(NF -4) }' | cut -c5-10`
                  #
                  DRIVE_INDEX=`$sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig -l | grep $DRIVE_NAME | awk {'print $3'}`
                  #
                  echo "$HOSTS uses device file $WIN_DEVICE_FILE and its index $DRIVE_INDEX"
                  $sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig "-update -drive $DRIVE_INDEX -path $WIN_DEVICE_FILE"
                  $sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -autoconfig -sync
              else
                  UNIX_DEVICE_FILE=`$sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig -l | grep $DRIVE_NAME | \
                  awk '{ print $(NF -4) }'`
                  #
                  DRIVE_INDEX=`$sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig -l | grep $DRIVE_NAME | awk {'print $3'}`
                  #
                  echo "$HOSTS uses device file $UNIX_DEVICE_FILE and its index $DRIVE_INDEX"
                  $sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -devconfig "-update -drive $DRIVE_INDEX -path $UNIX_DEVICE_FILE"
                  $sudo $VOLMGR_DIR/vmoprcmd -h $HOSTS -autoconfig -sync
              fi
           done
           # recheck the glob
           DRIVE_SERIAL_NUM=`$sudo $VOLMGR_DIR/vmglob -listall -java | grep -w $DRIVE_NAME | \
           awk {'print $4'} | sort | uniq | wc -l` 
           if  [ $DRIVE_SERIAL_NUM -gt 1 ]
           then
               # 
               echo "Drive serial number was not updated properly more than 1 serial number for this drive"
               echo "Contact Production Egineering"
            else
               #
               echo "Drive serial number was updated correctly"
           fi
      else
            echo "Drive $DRIVE_NAME Serial Number doesnt need updating, new serial number and vmglob are the same"
            exit 0
      fi
else
      echo "No drives where updated!"
      exit 0   
fi

