#!/bin/sh

case "`whoami`" in
  root) ;;
  bert) echo "Use the bits, bert, use the bits!"; exit 1;;
  *)    echo "You know, you may want to nuke your disks as root. =)"; exit 1;;
esac

BOOTBLOCK=/usr/lib/fs/ufs/bootblk
if [ ! -f $BOOTBLOCK ]; then
    BOOTBLOCK="/usr/platform/`uname -i`/lib/fs/ufs/bootblk"
    if [ ! -f $BOOTBLOCK ]; then
	echo "Cannot locate the boot block."
	exit 1
    fi
fi

cat <<'ENDofMESSAGE'

*** WARNING: if partitions changed, this may DESTROY /dev/dsk/c0t1d0s{0,5} ***

This script makes the following assumptions:
   (1) / and /usr are currently mounted as separate partitions.
   (2) The external disk is set up on SCSI bus 0, unit 1, lun 0 (ie, c0t1d0)
   (3) Partitions 0 and 5 aren't being used for storing useful data,
       and don't overlap with any partitions which are in use.
   (4) Partitions 0 and 5 have enough space to hold the currently mounted
       / and /usr partitions, respectively.
If you continue, you will DESTROY any contents of /dev/dsk/c0t1d0s{0,5},
as well as any partitions that overlap these.  Don't do anything stupuid. =)

If in doubt, check the output of "mount", or the contents of /etc/vfstab.
Continue? [n]
ENDofMESSAGE

read okay
case "$okay" in
  y*|Y*) ;;
  *) echo "Bye bye."; exit 0;;
esac

echo +++ Unmounting devices if mounted...
umount /dev/dsk/c0t1d0s0
umount /dev/dsk/c0t1d0s5

echo +++ Cleaning /new/root...
echo newfs -v -r 3600 /dev/dsk/c0t1d0s0
newfs -v -r 3600 /dev/dsk/c0t1d0s0

/usr/ucb/logger -t "sync_boot[$$]" -p user.warning \
    "external boot partitions temporarily non-functional (updating)"

echo +++ Cleaning /new/usr...
echo newfs -v -r 3600 -i 4096 /dev/dsk/c0t1d0s5
newfs -v -r 3600 -i 4096 /dev/dsk/c0t1d0s5

echo '*** /usr -> /new/usr ***'

echo +++ Remounting RW...
mount -o rw /dev/dsk/c0t1d0s5 /new/usr || exit 2

echo +++ Copying /usr ...
(cd /;/usr/local/bin/gtar clf - usr)|(cd /new;/usr/local/bin/gtar xvpf -)

echo +++ Unmounting RW mount...
umount /dev/dsk/c0t1d0s5 || exit 3
echo +++ Remounting on default mountpoint with default options...
mount /dev/dsk/c0t1d0s5 || exit 2

echo '*** / -> /new/root ***'

echo +++ Remounting RW...
mount -o rw /dev/dsk/c0t1d0s0 /new/root || exit 2

echo +++ Copying / ...
(cd /;/usr/local/bin/gtar clf - .)|(cd /new/root;/usr/local/bin/gtar xvpf -)

echo +++ Editing /etc/vfstab...
rm -f /new/root/etc/vfstab.internal
mv /new/root/etc/vfstab /new/root/etc/vfstab.internal
echo "### Generated by a script from ./vfstab.internal (from internal disk)" \
  > /new/root/etc/vfstab
sed -e 's@ /new/root .*@   %/ ufs 1 no -@'   \
    -e 's@ /new/usr .*@   %/usr ufs 3 no -@' \
    -e 's@ / .*@   /new/root ufs 6 no ro@'   \
    -e 's@ /usr .*@   /new/usr ufs 7 no ro@' \
    -e 's@ %@ @' /new/root/etc/vfstab.internal >> /new/root/etc/vfstab

echo +++ Unmounting RW mount...
umount /dev/dsk/c0t1d0s0 || exit 3
echo +++ Installing boot blocks...
/usr/sbin/installboot $BOOTBLOCK /dev/rdsk/c0t1d0s0
echo +++ Remounting on default mountpoint with default options...
mount /dev/dsk/c0t1d0s0 || exit 2

/usr/ucb/logger -t "sync_boot[$$]" -p user.warning \
    "external boot partitions are bootable again (update complete)"

echo +++ ...done!
