#!/bin/ksh
#
# hdisk names are COMPLETELY cosmetic and don't really matter, 
# provided that the PVID and VGDA are intact.  
# hdisk names are assigned when they are configured and don't change unless 
# deleted CurrentName the ODM,the ODM is rebuilt, or if the disks are replaced.
#====================================================================
# mvhdisk : by Kevin Gee, 6 August 1996
#
# Syntax:  mvhdisk <CurrentName> <NewName>
#
# modified by: David Engelman, Creo, 27 February 2005
#
# NewName search for new name if not provided
#

CurrentName=$1
NewName=$2
HostName=`hostname`
list=/tmp/.mvhdisk.l
file1=/tmp/.mvhdisk.0
file2=/tmp/.mvhdisk.1

checking ()
{
 clear
 echo "\n Renaming $CurrentName to $NewName ...\n"
 
 hdisk=`echo $NewName | tr -d "0-9"` 
 if [ "$hdisk" != "hdisk" ]
 then
   echo "\n The name should be: hdiskX \n"
   End 
 fi
 
 # why to rename
 vg=`lspv | grep $CurrentName | awk '{print $3}'`
 hd_num=`echo $CurrentName | tr -d "a-z"`
 if [ "$vg" = "svg"$hd_num ]
 then
   echo "\n\n $CurrentName looks OK: \c"
   cat $list | grep $CurrentName
   echo "\n\n Do you really want to rename it ? (Yes/No) [No]: \c"
   read ANS
   ANS=`echo $ANS | grep -i "y"` 
   if [ "$ANS" = "" ]; then End; fi
 fi   

 # svg exist
 hd_num=`echo $NewName | tr -d "a-z"`
 vg=`lsvg | grep "svg"$hd_num` 
 svg=`lspv | grep $CurrentName | awk '{print $3}'` 
 if [ "$vg" != "" -a "$vg" != "$svg" ]
 then
   echo "\n\n $vg already exist: \c"
   if [ `cat $list | grep -c $vg` -gt 1 ]; then echo "\n"; fi 
   cat $list | grep $vg 
   echo "\n\n Do you really want to rename $CurrentName to $NewName ? (Yes/No) [No]: \c"   
   read ANS
   ANS=`echo $ANS | grep -i "y"`
   if [ "$ANS" = "" ]; then End; fi
 fi

 # dv exist
 hd_num=`echo $NewName | tr -d "a-z"`
 dv=`echo $HostName"."$hd_num`
 dvs=`cat $list | grep $dv | grep -v $CurrentName | grep -v "svg"$hd_num`
 striped=`lspv | grep $svg | grep -v None | grep -v $CurrentName | awk '{print $3}'`  
 if [ "$striped" != "" ]
 then  
   dvs=`echo $dvs | grep -v $striped`
 fi
 if [ "$dvs" != "" ]
 then
   echo "\n\n $dv already exist: \c"
   if [ `cat $list | grep -c $dv` -gt 1 ]; then echo "\n"; fi
   cat $list | grep $dv
   echo "\n\n Do you really want to rename $CurrentName to $NewName ? (Yes/No) [No]: \c"
   read ANS
   ANS=`echo $ANS | grep -i "y"`
   if [ "$ANS" = "" ]; then End; fi
 fi  
}

swapping ()
{
 odmget -q name=$CurrentName CuAt > $file1
 odmdelete -q name=$CurrentName -o CuAt > /dev/null 2>&1
 odmget -q value=$CurrentName CuAt >> $file1
 odmdelete -q value=$CurrentName -o CuAt > /dev/null 2>&1
 odmget -q name=$CurrentName CuDep >> $file1
 odmdelete -q name=$CurrentName -o CuDep > /dev/null 2>&1
 odmget -q name=$CurrentName CuDv >> $file1
 odmdelete -q name=$CurrentName -o CuDv > /dev/null 2>&1
 odmget -q value3=$CurrentName CuDvDr >> $file1
 odmdelete -q value3=$CurrentName -o CuDvDr > /dev/null 2>&1
 odmget -q name=$CurrentName CuVPD >> $file1
 odmdelete -q name=$CurrentName -o CuVPD > /dev/null 2>&1
 sed "s/$CurrentName/$NewName/g" $file1 > $file2
 odmadd $file2
 mv -f /dev/$CurrentName /dev/$NewName
 mv -f /dev/r$CurrentName /dev/r$NewName
 savebase
 echo "\n\n $CurrentName changed to: $NewName "
 rm /tmp/.mvhdisk*
 echo "\n To see the new disk status please run:  mvhdisk -l \n\n"
 echo " Be aware that these are temporary names. After removing and reconfig"
 echo " devices - these names may change according to the physical sequence. \n\n"  
}

End ()
{
echo "\n\n\n Renaming $CurrentName to $NewName ... canceled !"  
echo "\n To see the disk status please run:  mvhdisk -l \n" 	
exit
}

# START

#list the current disks
echo "\n Disk status" > $list
echo "=============" >> $list
PV=`lspv | awk '{print $1}' | sort`
for pv in $PV
do
  vg=`lspv | grep "$pv " | awk '{print $3}'` 
  if [ "$vg" = "rootvg" ]; then vg=`echo "rootvg  [system disk]"`; fi
  echo "$pv\t$vg\t\c" >> $list
  svg=`echo $vg | grep svg`
  if [ "$svg" != "" ]
  then
    dv=`lsvg -l $svg | grep $HostName | awk '{print $7}' |  awk '{print $1}' | awk -F "/" '{print $3}'` 
    dv=`echo $dv`
    echo "$dv \n" >> $list
  else
    if [ "$vg" = "None" ]
    then
      lqueryvg -p $pv -L | grep scitexlv > /dev/null 2>&1 
      if [ $? -eq 0 ]
      then     
        echo "[has AIX system] \n" >> $list
      else
        echo "\n" >> $list 
      fi   
    else 
      echo "\n" >> $list 
    fi
  fi  
done

if [ "$1" = "-l" ]
then
  cat $list | more
  exit
fi  

if [ "$CurrentName" = "" ] 
then 
  echo "\n Current hdisk name: \c"
  read CurrentName 
  if [ "$CurrentName" = "" ]
  then 
    CurrentName=hdisk-X
    NewName=Hdisk-Y
    End
  fi
fi

lspv | grep $CurrentName > /dev/null 2>&1
if [ $? -ne 0 ]
then
  echo "\n Cannot find $CurrentName in the device configuration database"
  if [ "$NewName" = "" ]; then NewName=hdisk-X; fi
  End 
fi

if [ "$NewName" = "" ] 
then 
  echo "\n New hdisk name: \c"
  read NewName 
  if [ "$NewName" = "" ]
  then 
    NewName=hdisk-X
    End
  fi
fi

lspv | grep $NewName > /dev/null 2>&1
if [ $? -eq 0 ]
then
  echo "\n $NewName already exists in the device configuration database" 
  End 
else
  checking
  swapping
fi 
