#! /bin/sh
#
# boottape - build a boot TK50 for a MicroVAX
#
# Usage: boottape <root-file> <kernel-name> [<boot-program> [<tape-name>]]
#
# <root-file> is a name of a file in which the new root partition will
#  be stored.  The partition this is on must have enough space or you
#  will get device full errors.  Unfortunately, we can't catch this, so
#  you'll have to kill it by hand if this happens.
#
# <kernel-name> is a name of the kernel to install in the root
#  partition to go on the tape (this appears as /vmunix on the
#  constructed partition).
#
# <boot-program> is optional; the default is
#  /u1/robot/rccl.new/sys/tk50-boot.  This is the name of the boot
#  program that's put on the tape first.
#
# <tape-name> is optional; the default is /dev/rmt12.  This is of
#  course the device name the tape should be written on.  This is
#  expected to be a no-rewind tape device.
#
PATH=/local/bin:/local/etc:/usr/ucb:/bin:/usr/bin:/etc
export PATH
BOOT=/u1/robot/rccl.new/sys/tk50-boot
TAPE=/dev/rmt12
if [ $# -lt 2 ]; then
	echo Usage: $0 '<root-file> <kernel-name> [<boot-program> [<tape-name>]]' 1>&2
	exit 1
fi
DISK=$1
KERNEL=$2
if [ $# -gt 2 ]; then BOOT=$3; fi
if [ $# -gt 3 ]; then TAPE=$4; fi
ROOT=`</etc/fstab awk -F: '$2=="/" { print $1; exit; }'`
echo BOOT: $BOOT
echo TAPE: $TAPE
echo DISK: $DISK
echo KERNEL: $KERNEL
echo ROOT: $ROOT
ROOTSIZE=`probepartsize $ROOT \
| tail -1 \
| sed -e 's/^\([0-9]*\).*$/\1/'`
echo ROOTSIZE: $ROOTSIZE
# awk bug workaround in awk script below: fs[0] won't stay set, so we
#  add 1 all over the place.  grrr.  (This might not be an awk bug.
#  I think l is "" when we start, not zero.  might be related.)
DDSIZE=`factor $ROOTSIZE \
| awk '\
{									\
 fc[$1] ++;								\
 if ($1 != last)							\
  { fs[1+l] = last = $1;						\
    l ++;								\
  }									\
}									\
END									\
{									\
 n = 1;									\
 l --;									\
 while (n < 256)							\
  { f = fs[1+l];							\
    while (fc[f] <= 0)							\
     { l --;								\
       if (l < 0)							\
	{ print n;							\
	  exit;								\
	}								\
       f = fs[1+l];							\
     }									\
    n *= f;								\
    fc[f] --;								\
  }									\
 print n;								\
}'`
echo DDSIZE: $DDSIZE
echo '# copy the existing root partition (expect count='`expr $ROOTSIZE / $DDSIZE`')'
echo dd if=$ROOT of=$DISK bs="$DDSIZE"b
dd if=$ROOT of=$DISK bs="$DDSIZE"b
trap 'fddisc 0 >/dev/null 2>&1' 0 1 2 15
echo '# connect it to a pseudo-disk'
echo fdconn 0 $DISK
fdconn 0 $DISK
echo '# make sure it passes fsck'
echo fsck /dev/rfiledisk0
fsck /dev/rfiledisk0
sync
trap '( umount /dev/filedisk0 ; fddisc 0 ) >/dev/null 2>&1' 0 1 2 15
echo '# mount it'
echo mount /dev/filedisk0 /mnt
mount /dev/filedisk0 /mnt
echo '# install kernel'
echo cp $KERNEL /mnt/vmunix
cp $KERNEL /mnt/vmunix
echo '# unmount it'
echo umount /dev/filedisk0
umount /dev/filedisk0
echo '# disconnect the pseudo-disk'
echo fddisc 0
fddisc 0
trap '' 0 1 2 15
echo '# rewind the tape'
echo mt -f $TAPE rew
mt -f $TAPE rew
echo '# put the boot program on the tape'
echo dd if=$BOOT of=$TAPE bs=512
dd if=$BOOT of=$TAPE bs=512
echo '# put the root partition on the tape'
echo dd if=$DISK of=$TAPE bs=10240
dd if=$DISK of=$TAPE bs=10240
echo '# have fun.  You may want to delete '$DISK':'
ls -l $DISK
