#!/bin/sh
# mktarget
# Install the target so it can be booted

error_exit() {
    echo "ERROR: $1"
    exit 1
}

echo Make bootable TARGET 
[ "$TARGET" ] || error_exit "TARGET is not defined"
[ "$TARGET_DEV" ] || error_exit "TARGET_DEV is not defined"

mk_fstab()
{
cat <<EOF > $TARGET/etc/fstab
$TARGET_DEV  /      auto   defaults   1  1
none         /proc  proc   defaults   0  0
EOF
}

mk_lilo()
{
cat <<EOF >> /etc/lilo.conf
## TARGET
## $TARGET_DEV must be mounted under $TARGET 
image = $TARGET/boot/vmlinuz
    root = $TARGET_DEV
    label = TARGET
    read-only
EOF
}

SETUP_DIR="/var/log/setup"
INIT_DIR="$SETUP_DIR/init"

mk_config()
{
mkdir -p ${TARGET}${INIT_DIR}
cat <<EOF > ${TARGET}${INIT_DIR}/config-stage2
#!/bin/sh

KEYMAP=$KEYMAP

/sbin/config-stage2 vector-virgin-heavy $TARGET_DEV $KEYMAP

EOF
chmod a+x $TARGET/tmp/setup/config-stage2
}

vcmount $TARGET $TARGET_DEV

if [ ! -f $TARGET/boot/vmlinuz ]; then
   echo "Target has no kernel yet !"
   exit 1
fi

echo Making fstab
mk_fstab

echo Making first time configuration
mk_config

if ! grep -e "^ *root *= *$TARGET_DEV" /etc/lilo.conf; then
    echo Adding LILO entry
    mk_lilo
else
    echo TARGET is already in the lilo.conf
fi

echo Please examine the /etc/lilo.conf
echo then call lilo -v

