#====================
# setup file systems
#
cd /

#-------------------------------
# create fstab then mount by it
#
mount -w /dev/rd0a /

mfs_alloc_mb_max=$((`sysctl -n hw.usermem`/1024/1024))
mfs_alloc_mb=$(($mfs_alloc_mb_max/2))

greet_pc98=NO

if [ X"$greet_pc98" = XYES ]; then
    echo -n "[H[2JHow many megabytes(16-$mfs_alloc_mb_max)? "; read ans
else
    echo "========================================"
    echo $mfs_alloc_mb_max MB of memory available for you.
    echo -n "your mfs size in megabytes? [default: $mfs_alloc_mb] -> "; read ans
fi

if [ X"$ans" != X ]; then
    mfs_alloc_mb=$ans
fi
echo Allocating $mfs_alloc_mb MB for mfs.

echo "/dev/rd0a	/	ffs	rw	0 0
/dev/cd0c	/cdrom	cd9660	ro	0 0
swap		/mfs	mfs	rw,async,-b=4096,-f=512,-i=1024,-s=$(($mfs_alloc_mb*1024*2)) 0 0" >/boottmp/fstab

if /sbin/mount -a; then
    :
else
    echo "Mount failed. Invoking shell to debug..."
    /bin/sh
fi

#----------------------------
# re-link bin dirs to CD-ROM
#
/cdrom/bin/rm -rf /bin /sbin
/cdrom/bin/ln -sf cdrom/bin cdrom/sbin .

#-------------------------------------
# copy CD-ROM contents to rw-able mfs
#
echo -n "Copying files from CD-ROM to mfs ... "
cd /mfs
(cd ../cdrom && tar cf - etc home tmp root var) | tar xpf -
echo "done"

#-----------------------
# symlink from / to mfs
#
cd /
rm -rf /tmp
mv /etc /bootetc
ln -sf mfs/* .
cp /boottmp/fstab /etc

#-------------------------------------------
# symlink rest of contents from / to CD-ROM
#
ln -s cdrom/* .     2>/dev/null
#ln -s cdrom/.??* . 2>/dev/null ; # Maybe not needed

#-------------------------------------------
# lndir from /mfs/usr to /cdrom/usr
#
echo -n "Linking files from mfs to CD-ROM ... "
mkdir /mfs/usr && cd /mfs/usr && lndir -s /cdrom/usr
cd /mfs/usr/libexec/auth && rm * && cp -p /cdrom/usr/libexec/auth/* .
cd / && ln -sf mfs/usr .
echo "done"

#========================================
# setup in /etc (network etc...)
#
echo ""

echo "Set your keyboard type;"
while :;
do
    echo -n `kbd -l`
    echo -n ' -> '; read kbtype
    if kbd $kbtype; then
        echo $kbtype > /etc/kbdtype
        break
    fi
done

echo -n " Please wait ... "
until passwd root
do
    echo "passwd failed, Try again."
done

echo -n 'Setting up network configurations? -> '; read ans
case X"$ans" in
    X[Yy])
        echo "========================================"
        ifconfig -a
        echo "========================================"
        echo -n 'your network interface ? -> '; read mynic
        echo -n 'your IPv4 addr or "dhcp" ? -> '; read myv4addr
        if [ X"$myv4addr" = Xdhcp ]; then
            echo "dhcp" > /etc/hostname.$mynic
        else
            echo -n 'your netmask ? -> '; read mynetmask
            echo -n 'your FQDN ? -> '; read myfqdn
            echo -n 'your DNS server ? -> '; read mydns
            echo -n 'your default gateway ? -> '; read mygw

            echo "inet $myv4addr $mynetmask NONE" > /etc/hostname.$mynic
            echo "nameserver $mydns" > /etc/resolv.conf
            echo "$myfqdn" > /etc/myname
            echo "$mygw" > /etc/mygate
        fi
    ;;
esac

echo -n 'Generate cryptographic keys? -> '; read ans
case X"$ans" in
    X[Yy])
        rm -f /etc/ssh/*_key* /etc/isakmpd/private/local.key
    ;;
esac
echo "========================================"

#========================================
# run original /etc/rc
#
. /etc/rc
