#!/bin/sh

set -e -u

. ./config.sh
. ./.common.sh

DEF_DEV=/dev/da0

echo -n "Device where your CF card is attached [${DEF_DEV}]: "
read DEVICE

if [ "x${DEVICE}" = "x" ]; then
    DEVICE=$DEF_DEV
fi

if [ \! -c "${DEVICE}" ]; then
    echo "Unable to find ${DEVICE}, please check the pathname"
    exit 1;
fi



dd if=/dev/zero of=${DEVICE} bs=512 count=32
fdisk -BI ${DEVICE}
dd if=/dev/zero of=${DEVICE}s1 bs=512 count=32
bsdlabel -w -B ${DEVICE}s1
bsdlabel ${DEVICE}s1 >> tmp.$$
ed tmp.$$ <<EOF
/a:
s/unused/4.2BSD
wq
EOF
bsdlabel -R ${DEVICE}s1 tmp.$$
rm tmp.$$
newfs ${DEVICE}s1a

TEMPDIR=$PWD/tmpdir.$$
mkdir $TEMPDIR
mount ${DEVICE}s1a $TEMPDIR
cd $FREESBIEBASEDIR
find . -depth -print | cpio -pvudm $TEMPDIR
umount $TEMPDIR
rmdir $TEMPDIR
