#!/bin/sh

. $JSINCLUDE/Subroutines || exit 1 ;
SetRootOpts ; [ X$ROOT != X/ ] && exec Chroot_Script $0 $*

#
# Save prtvtoc so that we can have the same slices
#
DISK=/dev/rdsk/`echo \`basename $SI_ROOTDISK\` | cut -ds -f1`s2

VTOC="`prtvtoc -h $DISK | sort -n`"

#
# Swap has to be re-configured here, otherwise the patching fails
# for space in /tmp
#
OLDSWAP=`/etc/swap -l | grep '^/dev/' | awk '{print $1}'`

[ "$TARGET" != "2.6" ] &&\
  dumpadm -d `grep /dev/dsk/ /etc/vfstab | awk '{if($4 == "swap") print $1}'`

#
# As of VxVM 4.0 we need vxdctl to set up the rootdg/bootdg group
# before encapsulation; therefore vxconfigd is needed

mkdir -p /dev/vx/dsk/rootdg /dev/vx/rdsk/rootdg

vxconfigd -k -m disable
vxdctl init
vxdctl defaultdg rootdg
vxdctl bootdg    rootdg

/etc/vx/bin/vxencap -c -g rootdg -N rootdisk=`echo $SI_ROOTDISK | cut -ds -f1` || exit 0

</etc/init.d/vxvm-reconfig \
  sed -e 's/uadmin 2 1/ exit 0/' -e "s/vxconfigd/vxconfigd.`uname -s`_$BOSREL/g" \
>/tmp/vxvm-reconfig

LD_LIBRARY_PATH=/lib.${BOOTED}:$LD_LIBRARY_PATH
PATH=/bin.${BOOTED}:$PATH
export PATH LD_LIBRARY_PATH
echo "$LICENSE" | /sbin/vxlicinst
vxdctl enable

touch /etc/vx/reconfig.d/state.d/reconfig
echo "\n\n" | sh /tmp/vxvm-reconfig

for i in `vxprint -v | awk '$4=="ENABLED" && $7=="ACTIVE" {print $2}'` ; do vxvol stop $i ; done
vxdctl stop

#
# Add the swap devices and check if they are the same
#
for i in $OLDSWAP ; do /etc/swap -a $i 2>&-  ; done
NEWSWAP=`/etc/swap -l | grep '^/dev/' | awk '{print $1}'`
[ "$NEWSWAP" != "$OLDSWAP" ] && {
  echo "
	WARNING: Swap configuration changed by $0 :
	  OLD : $OLDSWAP
	  NEW : $NEWSWAP

  "
}

echo Looking if any slices got lost at encapsulation ...
#
# Recover the VTOC entries lost at encapsulation
#
VTOV="`prtvtoc -h $DISK | sort -n`"
[ "$VTOV"  =  "$VTOC" ] && {
  echo "
      WARNING: Partition table (VTOC) has not changed by vxencap
  "
  exit 0
}

# Take the current VTOC and add the slices from old VTOC that
# a. Have not "Veritas-known" tags
# b. Have inexistent slice numbers
#
BTG="2 3 5 7 14 15"
BNM="`echo \"$VTOV\" | awk '{print $1}'`"

echo Adding lost slices ...
(
  echo "$VTOV"
  echo "$VTOC" | while read Nm Tg Fl St Cn En Mp ; do
    if (for t in $BTG; do for n in $BNM; do [ $Tg -eq $t -o $Nm -eq $n ] && exit 1; done; done; exit 0) ; then
      echo $Nm $Tg $Fl $St $Cn $En
    fi
  done
) | sort -n | uniq | fmthard -s - $DISK

