#!/bin/sh
# $Id: 4.kernel.sh,v 1.15 2004/10/24 16:04:42 drizzt Exp $

set -e -u

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

export KERNCONFDIR=$LOCALDIR/files

if [ "x$MINIMAL" = "xYES" ]; then
    export __MAKE_CONF=$LOCALDIR/files/make.conf.minimal # Redefine make.conf path
else
    export __MAKE_CONF=$LOCALDIR/files/make.conf # Redefine make.conf path
fi

if [ "$RELEASE" -ge "5" ];
then
    export KERNCONF="FREESBIE.5"
    cp /usr/src/sys/i386/conf/GENERIC.hints $FREESBIEBASEDIR/boot/device.hints || print_error
else
    export KERNCONF="FREESBIE"
fi

if [ "x$MINIMAL" = "xYES" ]; then
    echo -n "Do you want to edit the kernel file? [N/y]: "
    read ANSWER
    if [ "${ANSWER}" = "Y" -o "${ANSWER}" = "y" ]; then
	if [ "x${EDITOR}" = "x" ]; then
	    EDITOR=/usr/bin/ee
	fi
	[ -f $KERNCONFDIR/$KERNCONF.mini ] || cp $KERNCONFDIR/$KERNCONF $KERNCONFDIR/$KERNCONF.mini
	export KERNCONF=$KERNCONF.mini
        ${EDITOR} $KERNCONFDIR/$KERNCONF 
    fi
fi

echo "Building FreeSBIE kernel..."
sleep 1

cd /usr/src || print_error
$MAKE kernel DESTDIR=$FREESBIEBASEDIR $MAKEOPT || print_error

if [ "$RELEASE" -ge "5" ];
then
    echo hint.psm.0.flags=\"0x100\" >> $FREESBIEBASEDIR/boot/device.hints || print_error
    if [ "x$MINIMAL" = "xYES" ]; then
	echo "Compiling acpi"
	cd /usr/src/sys/modules/acpi
	$MAKE depend DESTDIR=$FREESBIEBASEDIR $MAKEOPT && \
	    $MAKE DESTDIR=$FREESBIEBASEDIR $MAKEOPT && \
	    $MAKE install DESTDIR=$FREESBIEBASEDIR $MAKEOPT
    fi
    cd $FREESBIEBASEDIR/boot/kernel || print_error
    strip kernel || print_error
    gzip -f9 kernel || print_error
fi
