#!/bin/sh
#
# Copyright (c) 2004 Andre Guibert de Bruet <aguibert@siliconlandmark.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# For updates, please check http://bling.properkernel.com/freebsd/.
#
# $Id: genconf.sh,v 1.1 2004/11/01 01:16:59 andy Exp $
# $FreeBSD$

CONFFILE="AUTOGEN"
MACHTYPE=`sysctl -n hw.machine`
NCPU=`sysctl -n hw.ncpu`
NEEDNET=1
NEEDSCSI=0

if [ -f $CONFFILE ]
then
	echo "$CONFFILE found. Renaming existing file to $CONFFILE.old."
	mv -f $CONFFILE $CONFFILE.old
fi

if [ ! -f LINT ]
then
	make LINT 2> /dev/null

	if [ ! -f LINT ]
	then
		echo "Could not build/find LINT. Bailing out..."
		exit 1
	fi
fi

echo "# $CONFFILE - This kernel config file was automatically generated using genconf.sh for FreeBSD." > $CONFFILE
echo "#           You may need to edit this file manually to add any devices that were not detected." >> $CONFFILE
echo "#           For more information on the kernel building process, please refer to the FreeBSD Handbook." >> $CONFFILE
echo "#           Please send ideas or feedback to <aguibert@siliconlandmark.com>." >> $CONFFILE
echo "" >> $CONFFILE

# Machine type.
echo "machine		$MACHTYPE" >> $CONFFILE
echo "ident		$CONFFILE" >> $CONFFILE
if [ $MACHTYPE = "i386" ]
then
	# XXX FIX ME...
	echo "cpu		I686_CPU" >> $CONFFILE
fi

# Stuff that really needs to be here.
echo "options		SCHED_4BSD" >> $CONFFILE
echo "options		FFS" >> $CONFFILE
echo "options		INET" >> $CONFFILE
echo "options		SOFTUPDATES" >> $CONFFILE

# SMP.
if [ ! "$NCPU" = "1" ]
then
	echo "options		SMP" >> $CONFFILE
	echo "device		apic" >> $CONFFILE
fi

# Devices.
for i in `cat LINT |grep device | sed 's/^device.//'`; do
	OUTPUT=`sysctl dev.$i 2>&1`
	if [ ! "$OUTPUT" = "sysctl: unknown oid 'dev.$i'" ]
	then
		echo "device		$i" >> $CONFFILE
		
		if [ "ata" = "$i" ]
		then
			echo "device		atadisk" >> $CONFFILE
		fi

		if [ "isa" = "$i" ]
		then
			echo "device		eisa" >> $CONFFILE
		fi
		
		if [ "umass" = "$i" ]
		then
			NEEDSCSI=1
		fi

#		echo "Detected device: $i"
	fi
done

if [ $NEEDNET -eq 1 ]
then
	echo "device		loop" >> $CONFFILE
	echo "device		ether" >> $CONFFILE
fi

if [ $NEEDSCSI -eq 1 ]
then
	echo "device		da" >> $CONFFILE
	echo "device		scbus" >> $CONFFILE
fi


