#!/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}'`

#
# Surprisingly, we do not need vxconfigd to encapsulate the disk...
#
# vxconfigd -k -m disable
# vxdctl init
# vxdg init rootdg
# rm    /etc/vx/reconfig.d/state.d/*
# touch /etc/vx/reconfig.d/state.d/install-db

/etc/vx/bin/vxencap -N rootdisk=`echo $SI_ROOTDISK | cut -ds -f1` || exit 0
touch /etc/vx/reconfig.d/state.d/reconfig
touch /etc/vx/reconfig.d/state.d/install-db

#
# Quick patch for 2.6 hanging at vxconfigd start
#
# [ "$TARGET" = 2.6 ] && exit 0

sed 's/uadmin 2 1/ exit 0/'  </etc/init.d/vxvm-reconfig |\
  sed "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 "\n\n" | sh /tmp/vxvm-reconfig

#
# 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

mkdir /etc/KilledScripts
# mv ./etc/rc3.d/S*Patches* ./etc/rc3.d/S*Volume* /etc/init.d     /etc/KilledScripts

# echo Starting Shell ; /sbin/sh
#
#
#
#
