#!/bin/sh
#
# aryzhov@spasu.net
# Remount and possibly rename the metadevices
# according to #!Rename comment(s) in $SI_PROFILE
#
[ -z "$SID" ] && SID=/tmp/install_config
[ X$SubrIsRead = XYes ] || . $SID/Scripts/Misc/!Includes/Subroutines
[ X$VarsAreSet = XYes ] || SetJVars
SetRootOpts

cp $ROOT/etc/vfstab $ROOT/etc/vfstab.pre-`basename $0`
cp $ROOT/etc/vfstab /tmp/vfstab_a
cp /etc/vfstab      /tmp/vfstab
A_MOUNTS=`/sbin/mount | expand | awk '/^\/a on |^\/a\// && $3~/\/dev\// {print  $1" "$3}'`

for i in `echo "$A_MOUNTS" | awk '{print $1}' | sort -r`; do
  umount $i || StartShell "Failed to umount $i"
done

#
# If "/a" is configured in $SI_PROFILE to be mounted on metadevice,
# but still is on physical slice, we shall change this here, i.e.
# remount on a metadevice
#
cat $SI_PROFILE | expand | egrep '^filesys .*mirror:d' | egrep -s ' /$| / | /#' && {
  #
  # OK, root (/a) is configured on a metadevice,
  # let's see if it's still on physical slice
  # and patch the $A_MOUNTS then
  #
  ROOT_DEVICE=`echo "$A_MOUNTS" | awk '$1=="/a" {print $2}'`
  echo $ROOT_DEVICE | egrep -s 'dsk/c[0-9]' && {
    ROOT_SLICE=`echo $ROOT_DEVICE | cut -d/ -f4`
    ROOT_MD=""
    for MIRR in `/sbin/metastat | grep ': Mirror' | cut -d: -f1`; do
      metastat $MIRR | awk '$1=="'$ROOT_SLICE'"' | egrep -s $ROOT_SLICE && ROOT_MD=$MIRR
    done
    [ x$ROOT_MD != x ] && {
      A_MOUNTS=`echo "$A_MOUNTS" | sed "s:$ROOT_DEVICE:/dev/md/dsk/$ROOT_MD:"`
      #
      # Update the original vfstab on a diskless client as well
      # Remember, we have already sourced the original one into /tmp,
      # and are working with a copy so far
      #
      cp /tmp/vfstab /tmp/v || StartShell "Failed to backup vfstab"
      sed "s:/dev/dsk/$ROOT_SLICE:/dev/md/dsk/$ROOT_MD:;s:/dev/rdsk/$ROOT_SLICE:/dev/md/rdsk/$ROOT_MD:" </tmp/v >/tmp/vfstab
    }
  }
}


#
# See if we need to rename any metadevices
#
MYLABEL="#!Rename"
MYLINES=`awk '/'$MYLABEL'/ && $1=="filesys" && $2~/^mirror:d/' $SI_PROFILE`
for mirr in `echo "$MYLINES" | awk '{print $2}'`; do

  list=`echo "$MYLINES" | awk '$2=="'$mirr'"' | sed "s@^.*$MYLABEL@@" | awk '{print $1}'`

  [ x$SC_NODENAME = x   -o  `echo "$SC_NODE_IDs" | wc -w` -lt 1 ] && StartShell "#!Rename requested in SI_PROFILE but SC_NODENAME not defined"
  SC_NODENUM=`echo "$SC_NODE_IDs" | sed 's/ *//' | awk -F: '$1=="'$SC_NODENAME'" {print $2}'`

  OLD_MD_NAME=`echo $mirr | cut -d: -f2`
  NEW_MD_NAME=`echo $list | cut -d, -f$SC_NODENUM`
  echo $NEW_MD_NAME | egrep 'd[0-9]' || StartShell "Syntax error in Profile:#!Rename or in Cluster_Wide:SC_NODE_IDs"

  metarename $OLD_MD_NAME $NEW_MD_NAME || StartShell "Failed to metarename $OLD_MD_NAME $NEW_MD_NAME"
  A_MOUNTS=`echo "$A_MOUNTS" | sed "s:dsk/$OLD_MD_NAME$:dsk/$NEW_MD_NAME:"`

  for v in /tmp/vfstab /tmp/vfstab_a; do
    cp $v /tmp/v
    expand /tmp/v | sed "s:/$OLD_MD_NAME :/$NEW_MD_NAME :g" >$v
  done
done

MOPTS="rw,logging,largefiles,onerror=panic"
for i in `echo "$A_MOUNTS" | awk '{print $1}' | sort`; do
  MLINE=`echo "$A_MOUNTS" | awk '$1=="'$i'" {print $2 " " $1}'`
  mount -o $MOPTS $MLINE || StartShell "Failed to mount $i"
done

cat /tmp/vfstab   >/etc/vfstab
cat /tmp/vfstab_a >$ROOT/etc/vfstab

# StartShell "Debugging"
