#!/bin/sh

[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1

mntpoint=/mnt
mdstart=10
newfs_flags='-U'

set -e
u1=$mdstart
u2=$((mdstart + 1))
mp1=${mntpoint}$u1
mp2=${mntpoint}$u2
mkdir -p $mp1 $mp2
diskimage=$mp1/fsck8.sh.diskimage

max=$((10 * 1024 * 1024))
[ "$newfs_flags" = "-j" ] &&
    max=$((20 * 1024 * 1024))

mount | grep "on $mp1 " | grep -q /dev/md && umount -f $mp1
[ -c /dev/md$u1 ] && mdconfig -d -u $u1
mdconfig -a -t swap -s 1g -u $u1
newfs $newfs_flags -n /dev/md$u1 > /dev/null
mount /dev/md$u1 $mp1

[ -c /dev/md$u2 ] && mdconfig -d -u $u2
dd if=/dev/zero of=$diskimage bs=$max count=1 status=none
mdconfig -a -t vnode -f $diskimage -u $u2
backups=`newfs -N $newfs_flags md$u2 | grep -A1 "super-block backups" | \
    tail -1 | sed 's/,//g'`
newfs $newfs_flags md$u2
dumpfs /dev/md$u2 | head -24
mount /dev/md$u2 $mp2 || exit 1
[ -d /usr/include/sys ] && cp -r /usr/include/sys $mp2
umount $mp2
mdconfig -d -u $u2
umount $mp1
mdconfig -d -u $u1
rm -f /tmp/flip
exit $s
