#!/bin/bash
if [ -d /home/wstearns ] && [ -d /usr/src/buildkernel-work ]; then
	DEVEL=yes
	#BKINSTALLROOT uses this for the moment.
fi
#FIXME - make sure boot loader stuff is working
#FIXME - Check that BKINSTALLHERE is being checked for all the places that might be installing locally.
#FIXME - future version: always tar up, do the local install from the tar.
#FIXME - INSTALLTAR won't pack up external-module-created files.
#ASKFIRSTHELP="" $PRECOMMAND 
#FIXME - MEGA-FIXME.  What to do about askfirst and sourced files?
export PRECOMMAND="eval"	#While in development
#If you uncomment the set -x following, bash will show every command before it 
#is executed.
#set -x
#
#Make sure Makefile has BKSCRIPTVER updated when this changes
BKSCRIPTVER='1.06'
# ------------------------ Buildkernel ------------------------
# 	This script will automatically open up the linux kernel source 
# .tar.gz, patch it, configure it, build it, and install it for you.
# It automatically cleans out code from other architectures.
#	To get going quickly, read the QUICKSTART file that is part of 
# this package.  For detailed information on how to customize the build 
# process, read buildkernel.doc .  Both of these files should be part of
# the buildkernel package.  They were probably installed to 
# /usr/share/doc/buildkernel-{version}/ .  If they are missing or you 
# would like to make sure you have the latest version, please go to 
# http://www.stearns.org/buildkernel/  .
#
# To change the operation of buildkernel, do not change the variables below.
# Instead, read buildernel.doc to see how.  (For the impatient, create 
# ~/.bkrc and put any variables you wish to change in it.)  Keep in mind 
# that this script has intelligent defaults for all of the following and 
# displays them for you before starting.
#
#Copyleft:
#	Buildkernel is used to create a new linux kernel from the raw source
#	and patches.
#	Copyright (C) 1997-2002 William Stearns <wstearns@pobox.com>
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation; either version 2 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#	The author can also be reached at:
#        William Stearns
#email:  wstearns@pobox.com              (preferred)
#web:    http://www.stearns.org
#snail:  6 Manchester Dr.
#        Lebanon NH, 03766
#
# Do not change any variables below this line; internal variables
#
#We are going to send all output to /tmp/BKlog.$$ and rename it at the end if 
#necessary.

#-----------------------------------------------------------------------
#Internal Functions
#-----------------------------------------------------------------------
if [ -f /usr/lib/samlib/samlib ]; then
	. /usr/lib/samlib/samlib
else
	echo "/usr/lib/samlib/samlib is missing - please get it from" >/dev/stderr
	echo "http://www.stearns.org/samlib/" >/dev/stderr
	echo "Exiting." >/dev/stderr
	exit 1
fi
for ONEFUNC in wrap ; do
	if ! type $ONEFUNC >/dev/null 2>/dev/null ; then
		echo "Missing $ONEFUNC , please update samlib from" >/dev/stderr
		echo "http://www.stearns.org/samlib/" >/dev/stderr
		echo "Exiting." >/dev/stderr
		exit 1
	fi
done


bkgetfile () {
#Downloads a file and stores it in the current directory
#Params: protocol, server, user, password, directory, file
#Example:
#cd $BKSOURCEDIR
#bkgetfile ftp "$BKFTPSITE" anonymous "$USER@$HOSTNAME" "pub/linux/kernel/v${VERSION}.$PATCHLEVEL" "linux-$BKKERNELTOBUILD.tar.$BKGETEXT"
	RETVAL=""
	if type -path wget >/dev/null 2>/dev/null ; then
		#FIXME - get username@password in there
		ASKFIRSTHELP="Download $6" $PRECOMMAND wget --timestamping -q $1://$2/$5/$6
		RETVAL=$?	#Returns (>=?)1 on failure, 0 on success
	elif [ "$1" = "ftp" && type -path ftp >/dev/null 2>/dev/null ]; then
		ASKFIRSTHELP="Download $6" $PRECOMMAND ftp -n "<<-EOTEXT" "2>&1" | tee -a /tmp/BKlog.$$
open $2
user $3 $4
passive
cd $5
binary
hash
prompt
get $6
close
quit
EOTEXT
#ZZZZ
#	elif [ "$1" = "http" && type -path lynx >/dev/null 2>/dev/null ]; then
#		#FIXME -auth=ID:PASSWD for non-anonymous
#		ASKFIRSTHELP="" $PRECOMMAND lynx -source .................... 
	else
		#FIXME - if lynx exists, use it for http download.
		bkwarn http URL requested, but wget not available.
		bkbeep
		sleep 10
	fi
}

newestftpfile () {
#Examples:
#PCMCIASTABLE=`newestftpfile ftp pcmcia.sourceforge.org anonymous wstearns@pobox.com pcmcia 'pcmcia.*\.tar\.gz'`
#PCMCIABETA=`newestftpfile ftp pcmcia.sourceforge.org anonymous wstearns@pobox.com pcmcia/NEW 'pcmcia.*\.tar\.gz'`
#PCMCIAMISSING=`newestftpfile ftp pcmcia.sourceforge.org anonymous wstearns@pobox.com pcmcia/NEW 'goober.*\.tar\.gz'`
	#Params: protocol, server, user, password, directory, filespec
	if [ "$1" = "ftp" ] && type -path ftp >/dev/null 2>/dev/null ; then
		ASKFIRSTHELP="Checking for the newest version of $6" $PRECOMMAND ftp -n <<-EOTEXT  2>&1 | grep "$6" | tail -1
open $2
user $3 $4
passive
cd $5
ls -A1rt
close
quit
EOTEXT
	fi
}

bkheader () {
	#This way allows status messages before BKVERBOSE is set.
	if [ "$BKVERBOSE" != "NO" ]; then
		echo  2>&1 | tee -a /tmp/BKlog.$$
		echo  2>&1 | tee -a /tmp/BKlog.$$
		echo  2>&1 | tee -a /tmp/BKlog.$$
		echo -----------------------------------------------------------------------  2>&1 | tee -a /tmp/BKlog.$$
	fi
	echo -- $* --  2>&1 | tee -a /tmp/BKlog.$$
	if [ "$BKVERBOSE" != "NO" ]; then
		echo -----------------------------------------------------------------------  2>&1 | tee -a /tmp/BKlog.$$
	fi
}

bkstatus () {
	#This way allows status messages before BKVERBOSE is set.
	if [ "$BKVERBOSE" != "NO" ]; then
		if [ -n "$*" ]; then
			WRAPHEADER='---- ' wrap $*
		else
			echo ----
		fi
	fi
	if [ -n "$*" ]; then
		WRAPHEADER='---- ' wrap $* >>/tmp/BKlog.$$ 2>&1
	else
		echo ---- >>/tmp/BKlog.$$ 2>&1
	fi
}

bkwarn () {
	if [ -n "$*" ]; then
		WRAPHEADER='XXXXXXXXXX ' wrap $* 2>&1 | tee -a /tmp/BKlog.$$
	else
		echo XXXXXXXXXX 2>&1 | tee -a /tmp/BKlog.$$
	fi
}

bkbeep () {
	if [ "$BKVERBOSE" = "YES" ]; then
		echo -n -e "\a" >/dev/stderr
		sleep 1
		echo -n -e "\a" >/dev/stderr
		sleep 1
		echo -n -e "\a" >/dev/stderr
	fi
}

bksplitversion () {
#'bksplitversion 2.0.32' returns VERSION=2, PATCHLEVEL=0, SUBLEVEL=32 in
#the shell environment
	unset VERSION PATCHLEVEL SUBLEVEL
	BKLEGALVER=YES

	VERSION=`echo $1 | gawk --field-separator '.' '{ print $1 }'`
	PATCHLEVEL=`echo $1 | gawk --field-separator '.' '{ print $2 }'`
	SUBLEVEL=`echo $1 | gawk --field-separator '.' '{ print $3 }'`

#I am not going to deal with 00, 001, 000, etc.
#And yes, this will break when linux 2.3.1000 comes out ;-)
	case $VERSION in
	[0-9]|[0-9][0-9]|[0-9][0-9][0-9])   : ;;
	*)                                  BKLEGALVER="NO" ;;
	esac
	case $PATCHLEVEL in
	[0-9]|[0-9][0-9]|[0-9][0-9][0-9])   : ;;
	*)                                  BKLEGALVER="NO" ;;
	esac
	case $SUBLEVEL in
	[0-9]|[0-9][0-9]|[0-9][0-9][0-9])   : ;;
	*)                                  BKLEGALVER="NO" ;;
	esac
}

callkids () {
#Sole parameter: the action to be taken, from the following list, in order (POSTINIT and POSTMINVERCHECK no longer available).
#POSTLOADVARS, POSTGETSOURCE, POSTOPENSOURCE, POSTCONFIGURE, POSTKERNELBUILD,
#POSTMODULEBUILD, POSTINSTALL, POSTCLEAN
#script should also handle a target of ALL
#FIXME - redirect output to logfile.  ( ... ) subshell, redirected?
	for ONEKID in ${BKLIBDIR}/bkrun* ; do
		#If the pipe is removed, this needs to be subshelled with parentheses to protect pwd and shell vars.
		#Unexported variables seen (but won't be if subshelled)
		. $ONEKID $1 2>&1 | tee -a /tmp/BKlog.$$
	done
}

#FIXME - add function and trap command to clean up temporary files on exit.


#-----------------------------------------------------------------------
#Initialize internal variables
#-----------------------------------------------------------------------
BKPATCHESAPPLIED=NO
BKFREESPACE=0
BKSHOWHELP=NO
BKSHOWVER=NO
BKORIGPARAMS="$0 $*"

#-----------------------------------------------------------------------
#Create log file and header
#-----------------------------------------------------------------------
#NOTE: If the last build failed, I think we want to keep the partial log.
#This allows the user to see _all_ the steps that led up to a successful build.
#On the other hand, if /tmp/BKlog.$$ is a symlink, someone may be trying to 
#get this script to dump garbage in the symlink target.
if [ -L /tmp/BKlog.$$ ]; then
	#This rm uses a complete path, so we don't need to check pwd.
	ASKFIRSTHELP="Remove /tmp/BKlog.$$ symlink." $PRECOMMAND rm -f /tmp/BKlog.$$
	ASKFIRSTHELP="Create log file." $PRECOMMAND touch /tmp/BKlog.$$
fi

echo ----------------------------------------------------------------------- 2>&1 | tee -a /tmp/BKlog.$$
echo Buildkernel Version $BKSCRIPTVER  2>&1 | tee -a /tmp/BKlog.$$
echo Run on `date` on `hostname`  2>&1 | tee -a /tmp/BKlog.$$
echo Command line is:  2>&1 | tee -a /tmp/BKlog.$$
echo $BKORIGPARAMS  2>&1 | tee -a /tmp/BKlog.$$
echo ----------------------------------------------------------------------- 2>&1 | tee -a /tmp/BKlog.$$


#ZZZZ can this be replaced with assigning the output of ps to an env var?  see bin/ps-var
# Avoid fork() race conditions by outputting from ps to temp file
# and then analysing it.
# FIXME: This temp file is hardly a security risk, but it's a bit messy.
ASKFIRSTHELP="Inspect ps for another buildkernel." $PRECOMMAND ps ax ">/tmp/buildkernel.ps.$$"
BKNUMPROCESSES=`grep buildkernel /tmp/buildkernel.ps.$$ | wc -l | tr -d ' '`
if [ $BKNUMPROCESSES -eq 1 ]; then
	bkstatus No other buildkernel running on the system, good.
	ASKFIRSTHELP="Remove temp ps output file." $PRECOMMAND rm -f /tmp/buildkernel.ps.$$
else
	bkwarn There is another task running on the system with buildkernel somewhere in its name. ps ax reports $BKNUMPROCESSES tasks with buildkernel in their name. This script was not designed to allow more than one copy running at a time. If you still wish to run this, please wait until the other is finished or kill the other program. The following may help you determine what else is running...
	grep buildkernel /tmp/buildkernel.ps.$$ | grep -v $$
	ASKFIRSTHELP="Remove temp ps output file." $PRECOMMAND rm -f /tmp/buildkernel.ps.$$
	bkbeep
	exit
fi

unset BKNUMPROCESSES


#-----------------------------------------------------------------------
#Check for minimum version requirements
#-----------------------------------------------------------------------


bkstatus The following version information can be compared to the minimum version requirements in /usr/src/linux/Documentation/Changes, once the kernel source has been opened, to make sure your system has up to date versions:
bkstatus

if type -path uname >/dev/null; then
	bkstatus uname found at `type -path uname`
	bkstatus Current kernel and platform information:
	bkstatus `uname -a`
else
	bkstatus uname executable not found
fi
bkstatus

if type -path rpm >/dev/null; then
	bkstatus rpm found at `type -path rpm`
	bkstatus `rpm --version`
	if [ "`rpm -q redhat-release 2>/dev/null | wc -l | sed -e 's/ //g'`" != "0" ]; then
		bkstatus RedHat Linux
		bkstatus `rpm -q redhat-release`
	else
		bkstatus rpm database does not have a redhat-release entry
	fi
else
	bkstatus rpm executable not found
fi
bkstatus

if type -path lilo >/dev/null && [ -f /etc/lilo.conf ]; then
	LOADER="lilo"
	LoaderConfigFile="/etc/lilo.conf"
	bkstatus lilo found at `type -path lilo`
	bkstatus `lilo -V`
elif type -path silo >/dev/null && [ -f /etc/silo.conf ]; then
	LOADER="silo"
	LoaderConfigFile="/etc/silo.conf"
	bkstatus silo found at `type -path silo`
	bkstatus `silo -V`
elif type -path grub >/dev/null && [ -f /boot/grub/menu.lst ]; then
	LOADER="grub"
	LoaderConfigFile="/boot/grub/menu.lst"
	bkstatus grub found at `type -path grub`
	bkstatus `grub --version`
else
	bkstatus I can\'t seem to find a matched set of lilo+lilo.conf, silo+silo.conf, or grub+menu.lst.  What boot loader are you using?
fi
bkstatus

if type -path gcc >/dev/null; then
	bkstatus GCC found at `type -path gcc`
	bkstatus GCC version `gcc --version`
else
	bkstatus gcc executable not found
fi
bkstatus

if type -path make >/dev/null; then
	bkstatus make found at `type -path make`
	bkstatus `make --version | grep 'ersion'`
else
	bkstatus make executable not found
fi
bkstatus

if type -path insmod >/dev/null; then
	bkstatus insmod found at `type -path insmod`
	bkstatus part of the modutils package
	bkstatus `insmod -V 2>&1 | grep insmod | grep ersion`
else
	bkstatus insmod executable not found, modutils probably not installed
fi
bkstatus

if type -path ld >/dev/null; then
	bkstatus ld found at `type -path ld`
	bkstatus part of the binutils package
	bkstatus `ld -v`
else
	bkstatus ld executable not found, binutils probably not installed
fi
bkstatus

if [ "`ls -l /lib/libc.so.* | wc -l | sed -e 's/ //g'`" != "0" ]; then
	bkstatus libc found at `ls /lib/libc.so.*`
	bkstatus `ls -l /lib/libc.so.* | sed -e 's/.* //g'`
else
	bkstatus libc not found. Just how _does_ your system boot?
fi
bkstatus

#FIXME old ldd's only accept '-v'. New ones only accept --version. Ugh.
if type -path ldd >/dev/null; then
	bkstatus ldd found at `type -path ldd`
	bkstatus Part of the ld.so dynamic library
	bkstatus `ldd --version | grep '^ldd'`
else
	bkstatus ldd executable not found, ld.so probably not installed.
fi
bkstatus

if [ "`ls -l /usr/lib/libg++.so.* | wc -l | sed -e 's/ //g'`" != "0" ]; then
	bkstatus libc++ found at `ls /usr/lib/libg++.so.*`
	bkstatus `ls -l /usr/lib/libg++.so.* | sed -e 's/.* //g'`
else
	bkstatus libc++ not found.
fi
bkstatus

if type -path ps >/dev/null; then
	bkstatus ps found at `type -path ps`
	bkstatus Part of the procps package
	bkstatus `ps --version`
else
	bkstatus ps executable not found, procps probably not installed
fi
bkstatus

if type -path procinfo >/dev/null; then
	bkstatus procinfo found at `type -path procinfo`
	bkstatus `procinfo -v`
else
	bkstatus procinfo executable not found
fi
bkstatus

if type -path mount >/dev/null; then
	bkstatus mount found at `type -path mount`
	bkstatus `mount --version`
else
	bkstatus mount executable not found
fi
bkstatus

if type -path hostname >/dev/null; then
	bkstatus hostname found at `type -path hostname`
	bkstatus Part of the net-tools package
	bkstatus `hostname -V 2>&1`
else
	bkstatus hostname executable not found, net-tools probably not installed.
fi
bkstatus

if type -path expr >/dev/null; then
	bkstatus expr found at `type -path expr`
	bkstatus Part of the sh-utils package
	bkstatus `expr --v`
else
	bkstatus expr executable not found, sh-utils probably not installed
fi
bkstatus

if type -path automount >/dev/null; then
	bkstatus automount found at `type -path automount`
	bkstatus Part of the autofs package
	bkstatus `automount --version`
else
	bkstatus automount executable not found, autofs probably not installed
fi
bkstatus

if type -path showmount >/dev/null; then
	bkstatus showmount found at `type -path showmount`
	bkstatus Part of the NFS package
	bkstatus `showmount --version`
else
	bkstatus showmount executable not found, NFS probably not installled
fi
bkstatus

if type -path bash >/dev/null; then
	bkstatus bash found at `type -path bash`
	bkstatus `bash -version </dev/null`
else
	bkstatus bash executable not found, ahem, how is this script being run?
fi
bkstatus


echo ----------------------------------------------------------------------- 2>&1 | tee -a /tmp/BKlog.$$
echo 2>&1 | tee -a /tmp/BKlog.$$


#-----------------------------------------------------------------------
#Load user configuration and initialize variables
#-----------------------------------------------------------------------
#ZZZZ - askfirst modifications

#-----------------------------
bkstatus Load.1: Loading system preferences.
#-----------------------------
#FIXME check for illegal values
if [ -f /etc/bkrc ]; then
	. /etc/bkrc
	bkstatus /etc/bkrc preferences loaded
else
	bkwarn no /etc/bkrc, problem?
fi

#-----------------------------
bkstatus Load.2: Loading user preferences.
#-----------------------------
#FIXME check for illegal values
if [ -f ~/.bkrc ]; then
	. ~/.bkrc
	bkstatus ~/.bkrc preferences loaded
else
	bkstatus no ~/.bkrc, problem? Ignore this warning if this is the first time running this script. Creating ~/.bkrc
	if [ -f /etc/bkrc ] ; then
		bkstatus creating ~/.bkrc from /etc/bkrc
		#The following sed adds a '#' at the beginning of any uncommented line.
		cat /etc/bkrc | sed -e 's/^\([^#]\)/#\1/' > ~/.bkrc
	else
		bkwarn There is no /etc/bkrc to copy to ~/.bkrc. Proceeding anyway. You can get a complete copy of this script package from http://www.pobox.com/~wstearns/
	fi
	chmod 644 ~/.bkrc
fi


#-----------------------------
bkstatus Load.3: Load command line parameters
#-----------------------------

until [ -z "$*" ]; do
	# Following statement is "if there is an equals sign in $1 then"
	if [ "`echo $1 | sed -e s/[^=]//g | cut -b 1`" = "=" ]; then
		BKVALUE=`echo $1 | sed -e s/.*=//`
		#I do not want to fix case for all parameters; filenames are case sensitive.
		#If one is silly enough to want to log to the file "yes", well...
		case `echo $BKVALUE | tr a-z A-Z` in
		YES)			BKVALUE=YES				;;
		NO)				BKVALUE=NO				;;
		NEWESTBETA)		BKVALUE=NEWESTBETA		;;
		NEWESTSTABLE)	BKVALUE=NEWESTSTABLE	;;
		NEWESTPCMCIA)	BKVALUE=NEWESTPCMCIA	;;
		CONFIG)			BKVALUE=config			;;
		MENUCONFIG)		BKVALUE=menuconfig		;;
		XCONFIG)		BKVALUE=xconfig			;;
		esac
	else
		BKVALUE=''
	fi

	case `echo $1 | tr a-z A-Z | sed -e s/=.*//` in
	--HELP|--BKHELP)
		BKSHOWHELP=YES ;								shift	;;
	--VERSION|--BKVERSION)
		BKSHOWVER=YES ;									shift	;;
	--AUTOGET|--BKAUTOGET)	
		BKAUTOGET=${BKVALUE:-YES} ;							shift	;;
	--BUILDTYPE|--BKBUILDTYPE)
		if [ -n "$BKVALUE" ]; then		BKBUILDTYPE=$BKVALUE ; fi ;		shift	;;
	--COPYTOFLOPPY|--BKCOPYTOFLOPPY)
		BKCOPYTOFLOPPY=${BKVALUE:-YES} ;						shift	;;
	--DEBUG|--BKDEBUG)
		BKDEBUG=${BKVALUE:-YES} ;							shift	;;
	--EDITOR|--BKEDITOR)
		if [ -n "$BKVALUE" ]; then		EDITOR=$BKVALUE ; fi ;			shift	;;
	--FTPSITE|--BKFTPSITE)
		if [ -n "$BKVALUE" ]; then		BKFTPSITE=$BKVALUE ; fi ; 		shift	;;
	--GETEXT|--BKGETEXT)
		if [ -n "$BKVALUE" ]; then		BKGETEXT=$BKVALUE ; fi ;		shift	;;
	--HOSTNAME|--BKHOSTNAME)
		if [ -n "$BKVALUE" ]; then		HOSTNAME=$BKVALUE ; fi ;		shift	;;
	--HOSTTYPE|--BKHOSTTYPE)
		if [ -n "$BKVALUE" ]; then		HOSTTYPE=$BKVALUE ; fi ;		shift	;;
	--INSTALLROOT|--BKINSTALLROOT)
		bkwarn INSTALLROOT is in development
		if [ -n "$BKVALUE" ]; then		BKINSTALLROOT=$BKVALUE ; fi ;		shift	;;
	--INSTALLHERE|--BKINSTALLHERE)
		bkwarn INSTALLHERE is in development
		BKINSTALLHERE=${BKVALUE:-YES}							shift	;;
#	--INSTALLRPM|--BKINSTALLRPM)
#		bkwarn INSTALLRPM is in development - it currently is forced to off
#		BKINSTALLRPM=${BKVALUE:-YES}							shift	;;
	--INSTALLTAR|--BKINSTALLTAR)
		bkwarn INSTALLTAR is in development
		BKINSTALLTAR=${BKVALUE:-YES}							shift	;;
	--INTERACTIVE|--BKINTERACTIVE)
		BKINTERACTIVE=${BKVALUE:-YES} ;							shift	;;
	--KERNELTOBUILD|--BKKERNELTOBUILD)
		if [ -n "$BKVALUE" ]; then		BKKERNELTOBUILD=$BKVALUE ; fi ;		shift	;;
	--LOAD|--BKLOAD)
#FIXME handle straight +/- numbers, "nice", "mean"
		if [ -n "$BKVALUE" ]; then		BKLOAD=$BKVALUE ; fi ;			shift	;;
	--MAKECLEANBEFORE|--BKMAKECLEANBEFORE)
		BKMAKECLEANBEFORE=${BKVALUE:-YES} ;						shift	;;
	--MAKECLEANAFTER|--BKMAKECLEANAFTER)
		BKMAKECLEANAFTER=${BKVALUE:-YES} ;						shift	;;
	--MAKETYPE|--BKMAKETYPE)
		if [ -n "$BKVALUE" ]; then		BKMAKETYPE=$BKVALUE ; fi ;		shift	;;
	NEWESTBETA|--NEWESTBETA)
		BKKERNELTOBUILD=NEWESTBETA ;							shift	;;
	NEWESTSTABLE|--NEWESTSTABLE|NEWESTVERSION|--NEWESTVERSION)
		BKKERNELTOBUILD=NEWESTSTABLE ;							shift	;;
	NEWESTPCMCIA|--NEWESTPCMCIA)
		BKPCMCIATOBUILD=NEWESTPCMCIA ;							shift	;;
	--NUMBUILDS|--BKNUMBUILDS)
		if [ -n "$BKVALUE" ]; then		BKNUMBUILDS=$BKVALUE ; fi ;		shift	;;
	--OPENFRESH|--BKOPENFRESH)
		#I could probably set MAKECLEANBEFORE=YES if 
		#OPENFRESH=YES, but it makes no difference.
		#Build files will get deleted anyways.
		BKOPENFRESH=${BKVALUE:-YES} ;							shift	;;
	--PCMCIATOBUILD|--BKPCMCIATOBUILD)
		if [ -n "$BKVALUE" ]; then		BKPCMCIATOBUILD=$BKVALUE ; fi ;		shift	;;
	--ROOTDEVICE|--BKROOTDEVICE)
		if [ -n "$BKVALUE" ]; then		BKROOTDEVICE=$BKVALUE ; fi ;		shift	;;
	--SOURCEDIR|--BKSOURCEDIR)
		if [ -n "$BKVALUE" ]; then		BKSOURCEDIR=$BKVALUE ; fi ;		shift	;;
	--USER|--BKUSER)
		if [ -n "$BKVALUE" ]; then		USER=$BKVALUE ; fi ;			shift	;;
	--VERBOSE|--BKVERBOSE)
		BKVERBOSE=${BKVALUE:-YES} ;							shift	;;
	--VIDMODE|--BKVIDMODE)
		if [ -n "$BKVALUE" ]; then		BKVIDMODE=$BKVALUE ; fi ;		shift	;;
	[0-9].[0-9].[0-9]|[0-9].[0-9].[1-9][0-9]|[0-9].[0-9].[1-9][0-9][0-9]|[0-9].[1-9][0-9].[0-9]|[0-9].[1-9][0-9].[1-9][0-9]|[0-9].[1-9][0-9].[1-9][0-9][0-9])
		if [ -z "$BKKERNELTOBUILD" ]; then
			BKKERNELTOBUILD=`echo $1 | sed -e s/=.*//`
		else
			BKPCMCIATOBUILD=`echo $1 | sed -e s/=.*//`
		fi ;															shift	;;
	*)
		bkwarn $1 is not recognized as a valid command line parameter ;	shift	;;
	esac
done
unset BKVALUE


##-----------------------------
#bkstatus Load.4: Load version specific parameters
#bkstatus Not currently implemented...
##-----------------------------
#FIXME


##-----------------------------
#bkstatus Load.5: Load variables from last build of this version
#bkstatus Not currently implemented...
##-----------------------------
#FIXME
#/usr/src/configs/.bkrc.version.onetime


BKLIBDIR="${BKLIBDIR:-/var/lib/buildkernel}/"		#This must be set before any callkids

callkids POSTLOADVARS


#-----------------------------
bkstatus Load.6: Sanity checks on variables.
#-----------------------------

BKMAKETYPE=`echo $BKMAKETYPE | tr A-Z a-z`
case $BKMAKETYPE in
config|menuconfig|xconfig)
	bkstatus $BKMAKETYPE is a legal choice for maketype
	;;
*)
	#Remember we only get here if BKMAKETYPE is not correctly set. Any 
	#previous legal user choice would override this.
	if ! set | grep -q '^DISPLAY='; then
		bkstatus Not in X-windows.
		if [ "`ls -al /usr/lib/libncurses* /lib/libncurses* 2>/dev/null | wc -l | sed -e s/\ //g`" = "0" ]; then
			bkstatus Ncurses libraries not installed, config chosen for kernel configuration
			BKMAKETYPE=config
		else
			bkstatus Ncurses libraries installed, menuconfig chosen for kernel configuration
			BKMAKETYPE=menuconfig
		fi
	else
		bkstatus In X-windows, xconfig chosen for kernel configuration
		BKMAKETYPE=xconfig
	fi
	;;
esac

# If not set, gnu make allows an infinite number of builds.
# By the way, if you have a 16 CPU machine, we need to talk... :-)
case $BKNUMBUILDS in
[1-9]|1[0-7])
	bkstatus BKNUMBUILDS is in the standard range of 1 to 17
	;;
[1-9][0-9]|[1-9][0-9][0-9])
	bkwarn Well, if you want up to 999 simultaneous builds, OK. Are you really sure you want more than 17?
	;;
*)
	if [ -f /proc/cpuinfo ]; then
		BKNUMBUILDS=$[`cat /proc/cpuinfo | grep ^processor | wc -l` + 1]
		bkwarn BKNUMBUILDS out of the range of 1 to 17, reset to $BKNUMBUILDS which is the number of identified CPUs in your system plus 1.
		#Note that if this is a UP kernel on an SMP machine, this value may
		#incorrectly be set to 2, but that just means we won't be using the
		#system to its fullest during the build.
	else
		BKNUMBUILDS=2
		bkwarn BKNUMBUILDS out of the range of 1 to 17, reset to $BKNUMBUILDS . This value was chosen as a safe default as this system is not able to report the number of active processors via /proc/cpuinfo.
	fi
	;;
esac



#Following are defaults assigned to variables ONLY IF none of the previous
#steps have assigned values to these.
#Note BKLIBDIR had to be set above.
BKAUTOGET=`echo ${BKAUTOGET:-YES} | tr a-z A-Z`
BKBUILDTYPE=${BKBUILDTYPE:-bzImage}
BKCOPYTOFLOPPY=`echo ${BKCOPYTOFLOPPY:-NO} | tr a-z A-Z`
BKDEBUG=`echo ${BKDEBUG:-NO} | tr a-z A-Z`
if type -path bzip2 >/dev/null; then
	BKFTPSITE=${BKFTPSITE:-'ftp.bz2.us.kernel.org'}
else
	BKFTPSITE=${BKFTPSITE:-'ftp.gz.us.kernel.org'}
fi
BKINTERACTIVE=`echo ${BKINTERACTIVE:-YES} | tr a-z A-Z`
if [ -n "$DEVEL" ]; then
	BKINSTALLROOT="${BKINSTALLROOT:-$BKLIBDIR/installroot}/"
else
	BKINSTALLROOT="${BKINSTALLROOT:-/}"
fi
#In development
BKINSTALLHERE=`echo ${BKINSTALLHERE:-YES} | tr a-z A-Z`
#BKINSTALLRPM=`echo ${BKINSTALLRPM:-NO} | tr a-z A-Z`
BKINSTALLTAR=`echo ${BKINSTALLTAR:-NO} | tr a-z A-Z`
BKKERNELTOBUILD=`echo ${BKKERNELTOBUILD:-NEWESTSTABLE} | tr a-z A-Z`
BKLOAD=${BKLOAD:-'nice -n 10'}
BKLOGFILE=${BKLOGFILE:-'/usr/src/linux/bklog'}
BKMAKECLEANBEFORE=`echo ${BKMAKECLEANBEFORE:-NO} | tr a-z A-Z`
BKMAKECLEANAFTER=`echo ${BKMAKECLEANAFTER:-NO} | tr a-z A-Z`
BKOPENFRESH=`echo ${BKOPENFRESH:-YES} | tr a-z A-Z`
BKPCMCIATOBUILD=`echo ${BKPCMCIATOBUILD:-NO} | tr a-z A-Z`
BKROOTDEVICE=${BKROOTDEVICE:-`cat /etc/mtab | grep ' / ' | cut -f 1 -d ' '`}
BKSOURCEDIR=${BKSOURCEDIR:-/usr/src/source/kernel/}
BKVERBOSE=`echo ${BKVERBOSE:-YES} | tr a-z A-Z`
BKVIDMODE=${BKVIDMODE:-'-2'}
HOSTNAME=${HOSTNAME:-localhost}
HOSTTYPE=${HOSTTYPE:-`uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/`}
USER=${USER:-root}
BKBUILDCOUNT=${BKBUILDCOUNT:-0}


if [ "$BKINTERACTIVE" = "YES" ]; then
	for BKTRYEDITOR in `type -path pico` \
					   `type -path mcedit` \
					   `type -path vi` \
					   `type -path jove` \
					   `type -path nedit` \
					   `type -path emacs`; do
		if [ -z "$EDITOR" ]; then
			EDITOR=$BKTRYEDITOR
			bkstatus Editor default of $EDITOR taken.
		fi
	done
	unset BKTRYEDITOR
	if [ -z "$EDITOR" ]; then
		bkwarn No editor was specified by the EDITOR= variable and this script was unable to find pico, mcedit, vi, jove, nedit or emacs on your system. Please set EDITOR=the_name_of_an_editor in ~/.bkrc or your environment and re-run $BKORIGPARAMS .
		bkbeep
		exit
	fi #no suitable editor found
fi #INTERACTIVE


#-----------------------------
bkstatus Load.7: Display current settings
#-----------------------------
if [ "$BKVERBOSE" = "YES" ]; then
	bkstatus -----------------------------
	set | grep -E '^BK|^EDITOR=|^HOSTNAME=|^HOSTTYPE=|^USER='  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus  -----------------------------
	bkstatus Pausing a few seconds so you can double check run-time settings. If you need more than ten seconds, press Ctrl-S now and then press Ctrl-Q when you have looked them over.
	bkbeep
	sleep 10		#Give user a few seconds to see what they are in for.
	bkstatus OK, off we go!
fi
if [ "$BKINTERACTIVE" = "NO" ]; then
#FIXME remove when it works
	bkwarn You have chosen BKINTERACTIVE=NO. This script can work around a requirement of the linux kernel that one of the config choices be run. This workaround requires ncurses. We will give that a try in a moment.
fi

#-----------------------------------------------------------------------
#Handle Help or Version requests
#-----------------------------------------------------------------------
if [ "$BKSHOWHELP" = "YES" ]; then
	cat <<EOTEXT  2>&1 | tee -a /tmp/BKlog.$$
Syntax: $0 kernel-version [pcmcia-cs-version]

NOTE: if you do not use a system with pcmcia sockets, generally
just laptops, you can ignore every reference to pcmcia.

Example: $0 2.1.58 2.9.11
	Would build kernel 2.1.58 and pcmcia-cs 2.9.11

Example: $0 2.0.31
	Would build just kernel 2.0.31.

Example: $0 NEWESTSTABLE NEWESTPCMCIA
	Instead of using specific kernel or pcmcia versions, you may 
	simply ask for the latest versions.  The script will figure 
	out what the latest versions are, download them if they are 
	not already here, and continue as if you had specified the 
	versions.  NEWESTSTABLE or NEWESTBETA can be used instead of
	an actual kernel version, and NEWESTPCMCIA can be used 
	instead of a pcmcia-cs version.  You can mix NEWESTsomething 
	and a real version on the command line.
	For this to work, you need an Internet connection from the 
	start of the script until one or both files are downloaded.
	You obviously cannot have both NEWESTBETA and NEWESTSTABLE
	at the same time.

NOTE: NEWESTPCMCIA and pcmcia-cs autodownload are not yet 
implemented... Sorry.  For the moment you will need to download
them yourself and specify the appropriate version.

Keep in mind that this script has intelligent defaults for all of the 
following and displays them for you before starting.

Command line parameters (defaults listed first, '|' separates examples):
--BKAUTOGET=YES|NO	#Can script go out and get missing source files?
--BKBUILDTYPE=zImage|zdisk|bzImage|bzdisk
--BKCOPYTOFLOPPY=NO|YES
--BKDEBUG=NO|YES
--BKFTPSITE=ftp.gz.us.kernel.org|ftp.bz2.uk.kernel.org
--BKINSTALLHERE=YES|NO	#Not implemented yet
--BKINSTALLROOT=/var/lib/buildkernel/installroot/|/some/other/dir/	#In testing
--BKINSTALLRPM=NO|YES	#Not implemented yet
--BKINSTALLTAR=NO|YES	#In testing
--BKINTERACTIVE=YES|NO	#'NO' means script should not ask user for anything
--BKKERNELTOBUILD=NEWESTSTABLE|NEWESTBETA|2.0.32|2.1.66|2.1.42...
--BKLOAD='nice -n 10'|''|'nice -n -4'
--BKLOGFILE='/usr/src/linux/bklog'|'~/bklog'
--BKMAKECLEANBEFORE=NO|YES
--BKMAKECLEANAFTER=NO|YES
--BKMAKETYPE=menuconfig|xconfig|config
--BKNUMBUILDS=2|1|3|4|5|...|17
--BKOPENFRESH=YES|NO
--BKPCMCIATOBUILD=NO|2.9.12|2.8.23|NEWESTPCMCIA
--BKROOTDEVICE=''|/dev/hda1|/dev/hda3|/dev/sda2|/dev/sdb1...
--BKSOURCEDIR=/usr/src/source/kernel/|/tmp
--BKVERBOSE=YES|NO
--BKVIDMODE='-2'|'-3'|'-1'|'0'|'1'|'2'...
--HOSTNAME=mymachine.mydomain.org|...
--HOSTTYPE=i386|alpha|m68k|mips|ppc|sparc|sparc64
--USER=root|wstearns|nobody...
--EDITOR='pico -w '|pico|mcedit|vi|jove|nedit|emacs...
--HELP			#Show this help
--VERSION		#Show version and copyleft information

If this scrolled off the screen too quickly, try
buildkernel --HELP | more<Enter>

EOTEXT
	exit
fi #BKSHOWHELP=YES
unset BKSHOWHELP

if [ "$BKSHOWVER" = "YES" ]; then
	cat <<EOTEXT  2>&1 | tee -a /tmp/BKlog.$$
Buildkernel, version $BKSCRIPTVER.
	Buildkernel is used to create a new linux kernel from the raw source
and patches. 
	Copyright (C) 1997-2002 William Stearns <wstearns@pobox.com>
	This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version. 
	This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details. 
	You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
	The author can also be reached at:
        William Stearns
email:  wstearns@pobox.com              (preferred)
web:    http://www.stearns.org
snail:  6 Manchester Dr., Lebanon NH, 03766
EOTEXT
	exit
fi #BKSHOWVER=YES
unset BKSHOWVER


#-----------------------------------------------------------------------
#Handle difficult running environment
#-----------------------------------------------------------------------
if [ $UID != 0 ]; then
#FIXME check for BKBUILDUSER
	bkwarn Hmmm... you do not seem to be user root. It may be the case that you _can_ compile a kernel on this system, but I make some assumptions about commands that are available to you that may not be true if you are not root. If you meant to run this as root, press Ctrl-C now and run it as root. If you meant to be running this while not root, the script will restart in 30 seconds.
	bkbeep
	sleep 30
fi


cd /usr/src
if [ "$BKKERNELTOBUILD" = "NEWESTSTABLE" ] || \
   [ "$BKKERNELTOBUILD" = "NEWESTBETA" ]; then
	#-----------------------------------------------------------------------
	bkheader Translate Kernel NEWESTSTABLE or NEWESTBETA to a real version
	#-----------------------------------------------------------------------
	if [ "$BKKERNELTOBUILD" = "NEWESTBETA" ]; then
		BKKERNELTYPE=beta
	else
		BKKERNELTYPE=stable
	fi
	bkstatus Figuring out what the newest $BKKERNELTYPE kernel is...
	#This rm uses a complete path, so we don't need to check pwd.
	rm -f /tmp/BKfingerkernel  2>&1 | tee -a /tmp/BKlog.$$
	finger @finger.kernel.org >/tmp/BKfingerkernel  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus Here is what Linus reports as the latest $BKKERNELTYPE kernel
	cat /tmp/BKfingerkernel | grep -i $BKKERNELTYPE  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus

	bksplitversion `cat /tmp/BKfingerkernel    | \
	 grep -i $BKKERNELTYPE                     | \
	 gawk --field-separator ':' '{ print $2 }' | \
	 sed -e s/\ //g`

	if [ -z "$VERSION$PATCHLEVEL$SUBLEVEL" ]; then
		bkwarn The attempt to finger finger.kernel.org did not seem to return a version number we can use.  Please check on this.  You have the option of finding out what the newest kernel is on your own and replacing NEWESTBETA or NEWESTSTABLE with that version number.
		bkbeep
		exit
	fi
	bkstatus Version is $VERSION , patchlevel is $PATCHLEVEL , sublevel is $SUBLEVEL
	bkstatus The current series of the $BKKERNELTYPE kernel is $VERSION.$PATCHLEVEL.$SUBLEVEL
	BKKERNELTOBUILD=$VERSION.$PATCHLEVEL.$SUBLEVEL
	BKAUTOGET=YES
fi




#-----------------------------------------------------------------------
bkheader Create necessary directories and links.
#-----------------------------------------------------------------------
cd /usr
for BKMAKEDIR in \
 /usr/src/configs $BKSOURCEDIR \
 /usr/include \
 $BKINSTALLROOT \
 $BKINSTALLROOT/boot \
 $BKINSTALLROOT/boot/grub \
 $BKINSTALLROOT/etc \
 $BKINSTALLROOT/etc/init.d \
 $BKINSTALLROOT/etc/pcmcia \
 $BKINSTALLROOT/etc/sysconfig \
 $BKINSTALLROOT/lib/modules \
 $BKINSTALLROOT/sbin \
 $BKINSTALLROOT/usr/X11R6/bin \
 $BKINSTALLROOT/usr/bin \
 $BKINSTALLROOT/usr/share/man/man4 \
 $BKINSTALLROOT/usr/share/man/man5 \
 $BKINSTALLROOT/usr/share/man/man8 \
 ; do
	if [ ! -d $BKMAKEDIR ]; then
		mkdir --parents $BKMAKEDIR 2>&1 | tee -a /tmp/BKlog.$$
	fi
	if [ ! -d $BKMAKEDIR ]; then 
#FIXME more robust checking
		bkwarn Hmmm... I do not seem to be able to create directory $BKMAKEDIR but need to to be able to to continue. Could you check on this and rerun $BKORIGPARAMS ? Thanks.
		bkbeep
		exit
	fi
done
unset BKMAKEDIR

#Copy over the last resort backup of .config - before we screw with /usr/src/linux
if [ -f /usr/src/linux/.config ]; then
	cp -p -b -f /usr/src/linux/.config /usr/src/configs/.config  2>&1 | tee -a /tmp/BKlog.$$
fi


#Skip over kernel build
#if [ "$BKDEBUG" != "YES" ]; then



#	Each of the following files should be symbolic links.  The first time 
#this is run, it is possible that we have something else, but we only need
#to deal with that once.  I am not going to deal with 
#making multi level backups of structures that should be symlinks anyways.

cd /usr
#Make sure each entry in the following is a complete path
for BKCHECKLINK in /usr/include/asm /usr/include/linux /usr/include/scsi \
                   /usr/src/linux ; do
	if [ -L $BKCHECKLINK ]; then
		bkstatus Removing symbolic link $BKCHECKLINK
		#This rm uses complete paths, so we don't need to check pwd.
		rm -f $BKCHECKLINK  2>&1 | tee -a /tmp/BKlog.$$
	elif [ -e $BKCHECKLINK ]; then
		if [ -f $BKCHECKLINK ] || [ -d $BKCHECKLINK ]; then
			bkwarn renaming file or directory $BKCHECKLINK to $BKCHECKLINK.old
			#This rm uses a complete path, so we don't need to check pwd.
			rm -rf $BKCHECKLINK.old  2>&1 | tee -a /tmp/BKlog.$$
			mv -f $BKCHECKLINK $BKCHECKLINK.old  2>&1 | tee -a /tmp/BKlog.$$
		else
			bkwarn $BKCHECKLINK exists, but is neither a symlink, a directory nor a file. This script was expecting to find a symbolic link. Please rename it, move it or delete it. It will be created by this script. Please fix it and run $BKORIGPARAMS again.
			bkbeep
			exit
		fi
	else
		bkstatus $BKCHECKLINK does not exist, no problem, we create it.
	fi
done


cd /usr/include
bkstatus Re-creating /usr/include links
bkstatus /usr/include/asm...
ln -s /usr/src/linux/include/asm asm  2>&1 | tee -a /tmp/BKlog.$$
bkstatus /usr/include/linux...
ln -s /usr/src/linux/include/linux linux  2>&1 | tee -a /tmp/BKlog.$$
bkstatus /usr/include/scsi...
ln -s /usr/src/linux/include/scsi scsi  2>&1 | tee -a /tmp/BKlog.$$
cd /usr/src
#Removed because tar doesn't like symlinks anymore
#bkstatus /usr/src/linux...
#ln -s /usr/src/linux-$BKKERNELTOBUILD linux  2>&1 | tee -a /tmp/BKlog.$$
bkstatus Links created.

#/usr/src/linux gets created later, once inside "if bkopenfresh=yes" and once
#right after.
#FIXME re-create links in abort routine


#-----------------------------------------------------------------------
bkheader Check drive space
#-----------------------------------------------------------------------
cd /usr/src
BKFREESPACE=`df -P /usr/src | grep -v '^Filesystem' | \
 gawk --field-separator ' ' '{ print $4 }'`

#We subtract the space taken up by /usr/src/linux-$BKKERNELTOBUILD 
#as we'll be toasting this in a moment if OPENFRESH=YES and we don't 
#care if OPENFRESH=NO.
#FIXME This could screw up if /usr/src/ and /usr/src/linux(-ver) are on 
#different partitions.
if [ -e /usr/src/linux-$BKKERNELTOBUILD ] ; then
#FIXME - screwed up by rh62 or devfs?  prints 96%
	BKFREESPACE=$[ $BKFREESPACE + `du -s /usr/src/linux-$BKKERNELTOBUILD \
	 | gawk --field-separator ' ' '{ print $1 }'`  ]
	bkstatus The filesystem containing /usr/src, excluding the space taken by /usr/src/linux-$BKKERNELTOBUILD, has $BKFREESPACE kilobytes free.
else
	bkstatus The filesystem containing /usr/src has $BKFREESPACE kilobytes free.
fi

#FIXME implement configurable high and low water marks
#FIXME rewrite now that we exclude /usr/src/linux-$BKKERNELTOBUILD
if [ $BKFREESPACE -lt 45000 ]; then
	if [ "$BKINTERACTIVE" = "YES" ]; then
		bkwarn I show less than 45M free in the partition used to build the kernel. This may or may not be a problem. If we will be erasing or overwriting an old kernel, as little as 15M may be enough. If not, we may need between 25M and 60M for the build. Please decide if this build should continue. If there is enough space, simply press enter. If there is not enough space, press Ctrl-C now, free up some space, and re-run $BKORIGPARAMS.
		bkbeep
		read BKJUNK
	else
		if [ $BKFREESPACE -lt 5000 ]; then
			bkwarn We have less than 5M in the /usr/src partition. I do not consider this to be enough to continue. Please free up some space and re-run $BKORIGPARAMS.
			bkbeep
			exit
		else
			bkwarn Warning!  We have between 5M and 45M in the /usr/src partition, but the current configuration asks for no interaction with the user. We will push on and hope that we do not run out of space.
		fi
	fi
else
	bkstatus More than 45M free in /usr/src, probably OK.
fi



bkstatus This will install, patch, and compile linux $BKKERNELTOBUILD




#-----------------------------------------------------------------------
bkheader Check if we have source files, get them if we do not.
#-----------------------------------------------------------------------
cd /usr/src
if [ "$BKOPENFRESH" = "YES" ]; then
	if [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz ]; then
		bkstatus ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz found.
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar ]; then
		bkstatus ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar found.
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.bz2 ]; then
		bkstatus ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.bz2 found.
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tgz ]; then
		bkstatus ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tgz found.
	elif [ "$BKAUTOGET" = "YES" ]; then
#FIXME How to handle partial downloads?
#FIXME Check for existance of file w/ right size before autodownload

		if [ -z "$BKGETEXT" ]; then
			BKGETEXT="gz"
			if type -path bzip2 >/dev/null; then
				if [ -n "`echo $BKFTPSITE | grep 'bz2'`" ]; then
					BKGETEXT="bz2"
				fi
			fi
		fi
		bksplitversion $BKKERNELTOBUILD
		bkstatus Kernel does not exist here. We will go get it. Opening anonymous connection to $BKFTPSITE . Attempting to download linux-$BKKERNELTOBUILD.tar.$BKGETEXT from pub/linux/kernel/v${VERSION}.$PATCHLEVEL
		#FIXME Someday.... echo get patch-$BKKERNELTOBUILD.gz
		#FIXME Get file size for later check of successful download

		cd $BKSOURCEDIR
		BKTIMEBASE=$SECONDS
		bkgetfile ftp "$BKFTPSITE" anonymous "$USER@$HOSTNAME" "pub/linux/kernel/v${VERSION}.$PATCHLEVEL" "linux-$BKKERNELTOBUILD.tar.$BKGETEXT"
		#FIXME - check return status of bkgetfile instead of checking for existance

		if [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.$BKGETEXT ]; then
			bkstatus Download took $[$SECONDS-$BKTIMEBASE] seconds. ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.$BKGETEXT successfully downloaded.
		elif [ "$BKGETEXT" = "bz2" ]; then
			bkwarn Download attempt took $[$SECONDS-$BKTIMEBASE] seconds. ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.$BKGETEXT was not successfully downloaded.
			bkstatus We will try to download the .gz version.

			cd $BKSOURCEDIR
			BKTIMEBASE=$SECONDS
			bkgetfile ftp "$BKFTPSITE" anonymous "$USER@$HOSTNAME" "pub/linux/kernel/v${VERSION}.$PATCHLEVEL" "linux-$BKKERNELTOBUILD.tar.gz"
			if [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz ]; then
				bkstatus Download took $[$SECONDS-$BKTIMEBASE] seconds. ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz successfully downloaded.
			else
				bkwarn Second download attempt took $[$SECONDS-$BKTIMEBASE] seconds. ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz was not successfully downloaded either. Is your Internet connection down? Out of disk space? $BKFTPSITE overloaded?
				bkbeep
				exit
			fi
		else
			bkwarn Download attempt took $[$SECONDS-$BKTIMEBASE] seconds. ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.$BKGETEXT was not successfully downloaded. Is your Internet connection down? Out of disk space? $BKFTPSITE overloaded?
			bkbeep
			exit
		fi
	else
		bkwarn I cannot find ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz . Please place the file there, or edit the BKSOURCEDIR variable in ~/.bkrc, and restart. .tar, .tgz, and .tar.bz2 files are acceptable. If you would like me to automatically get the newest kernel, try
		bkwarn $BKORIGPARAMS NEWESTSTABLE 
		bkwarn or 
		bkwarn $BKORIGPARAMS NEWESTBETA
		bkwarn or set BKAUTOGET=YES in ~/.bkrc and rerun $BKORIGPARAMS
		bkbeep
		exit
	fi
else
	bkstatus Current configuration chooses not to open a fresh kernel.
fi


callkids POSTGETSOURCE


#FIXME move to sanity checks
cd /usr/src
if [ "$BKINTERACTIVE" != "YES" ]; then
	#-----------------------------------------------------------------------
	bkheader Make sure we have configuration files
	#-----------------------------------------------------------------------
	#I only require the files to exist if user interaction is turned off.
	if [ ! -f /usr/src/linux-$BKKERNELTOBUILD/.config ] && \
	   [ ! -f /usr/src/configs/.config.$BKKERNELTOBUILD ]; then
		bkwarn Interactive mode is off, but I cannot find a kernel .config file in either /usr/src/linux-$BKKERNELTOBUILD or in /usr/src/configs/.config.$BKKERNELTOBUILD to reuse. You must re-enable interactive mode in ~/.bkrc and re-run $BKORIGPARAMS
		bkbeep
		exit
	else
		bkstatus Kernel .config or backup exists.
	fi #if linux .config (and backup) are missing
	# Missing .version is not a problem, we do not check for it.
	if [ "$BKPCMCIATOBUILD" != "NO" ]; then
		if [ ! -f /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/config.mk ] && \
		   [ ! -f /usr/src/configs/config.mk.$BKPCMCIATOBUILD ]; then
			bkwarn Interactive mode is off, but I cannot find a pcmcia-cs config.mk in either /usr/src/pcmcia-cs-$BKPCMCIATOBUILD or /usr/src/configs/config.mk.$BKPCMCIATOBUILD for reuse. You should re-enable interactive mode in ~/.bkrc and re-run $BKORIGPARAMS
			bkbeep
			exit
		else
			bkstatus Pcmcia config.mk or backup exists.
		fi #if pcmcia config.mk (and backup) are missing
		if [ ! -f /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/config.out ] && \
		   [ ! -f /usr/src/configs/config.out.$BKPCMCIATOBUILD ]; then
			bkwarn Interactive mode is off, but I cannot find a pcmcia-cs config.out in either /usr/src/pcmcia-cs-$BKPCMCIATOBUILD or /usr/src/configs/config.out.$BKPCMCIATOBUILD for reuse. You should re-enable interactive mode in ~/.bkrc and re-run $BKORIGPARAMS
			bkbeep
			exit
		else
			bkstatus Pcmcia config.out or backup exists.
		fi #if pcmcia config.out (and backup) are missing
	fi #if building pcmcia
fi #if Interactive!=YES

cd /usr/src
if [ "$BKOPENFRESH" = "YES" ]; then
#-----------------------------------------------------------------------
bkheader Remove old source, open new source, apply patches, rename tree
#-----------------------------------------------------------------------
#if .config exists nowhere else, try /usr/src/linux/.config as a last 
#resort.  The following commands back up whatever is in /usr/src/linux for 
#last resort retrieval later.
	cd /usr/src

#FIXME - double check 'remove existing' logic now that we're opening into /usr/src/linux instead of a real version.
	if [ -d linux-$BKKERNELTOBUILD ]; then
		if [ -f linux-$BKKERNELTOBUILD/.config ]; then
			bkstatus Backing up .config
			mv -b -f linux-$BKKERNELTOBUILD/.config configs/.config.$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
		else
			bkstatus No .config to back up, no problem
		fi
		if [ -f linux-$BKKERNELTOBUILD/.version ]; then
			bkstatus Backing up .version
			mv -f linux-$BKKERNELTOBUILD/.version configs/.version.$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
		else
			bkstatus No .version to back up, no problem
		fi
		if [ -f /bin/rpm ]; then
			bkstatus I am running on a RedHat 3 or greater system. I will first try to remove any RedHat installed kernel source files, but not an actual kernel
			#This rm uses a complete path, so we don't need to check pwd.
#FIXME just use rpm -q kernel-source (2>&1?) = "package kernel-source is not installed"
			rm -f /tmp/BKrpmqa  2>&1 | tee -a /tmp/BKlog.$$
			rpm -qa >/tmp/BKrpmqa  2>&1 | tee -a /tmp/BKlog.$$
			if grep kernel-source /tmp/BKrpmqa; then 
				bkstatus kernel-source.rpm is installed, removing
				rpm --erase kernel-source  2>&1 | tee -a /tmp/BKlog.$$
			else
				bkstatus kernel-source.rpm is not installed, no problem.
			fi
			##FIXME likewise
			#if grep kernel-headers /tmp/BKrpmqa; then 
			#	bkstatus kernel-headers.rpm is installed, removing
			#	rpm --erase kernel-headers  2>&1 | tee -a /tmp/BKlog.$$
			#else
			#	bkstatus kernel-headers.rpm is not installed, no problem.
			#fi
			bkstatus I do not touch kernel, kernel-modules or pcmcia-cs rpms as they might be needed to boot into an old kernel.
		fi
		#Even if we toasted the RedHat sources, we still clean up in case
		#files are left over.
		bkstatus Directory linux-$BKKERNELTOBUILD found, removing. This may take a minute...
		#These rm's use complete paths, so we don't need to check pwd.
		rm -rf /usr/src/linux-$BKKERNELTOBUILD/* 2>&1 | tee -a /tmp/BKlog.$$
		rm -rf /usr/src/linux-$BKKERNELTOBUILD/.[a-zA-Z0-9]*  2>&1 | tee -a /tmp/BKlog.$$
		rm -f /usr/src/linux  2>&1 | tee -a /tmp/BKlog.$$
		rmdir /usr/src/linux-$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
	else
		bkstatus No existing linux-$BKKERNELTOBUILD, no files to delete, no problem.
	fi

	cd /usr/src
	bkstatus Installing new version of kernel source tree.  linux directory is created by tar.
	#bkstatus Creating linux directory.
	#if [ ! -d linux ]; then
	#	mkdir --parents linux 2>&1 | tee -a /tmp/BKlog.$$
	#fi

	bkstatus Opening up source tar, may take a minute...
	bkstatus "Wouldn't a cup of coffee be nice right about now?"

	cd /usr/src
	BKTIMEBASE=$SECONDS
	if [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar ]; then
		bkstatus Opening ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar
		$BKLOAD tar -xf ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar  2>&1 | tee -a /tmp/BKlog.$$
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz ]; then
		bkstatus Opening ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz
		$BKLOAD tar -xzf ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.gz  2>&1 | tee -a /tmp/BKlog.$$
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tgz ]; then
		bkstatus Opening ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tgz
		$BKLOAD tar -xzf ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tgz  2>&1 | tee -a /tmp/BKlog.$$
	elif [ -f ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.bz2 ]; then
		bkstatus Opening ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.bz2
		$BKLOAD cat ${BKSOURCEDIR}linux-$BKKERNELTOBUILD.tar.bz2 | \
		$BKLOAD bunzip2                                          | \
		$BKLOAD tar -xf - 2>&1 | tee -a /tmp/BKlog.$$
	fi
	bkstatus Opening the source tar took $[$SECONDS-$BKTIMEBASE] seconds.

	cd /usr/src
	bkstatus Renaming linux to linux-$BKKERNELTOBUILD
	mv linux linux-$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
	bkstatus Creating /usr/src/linux symbolic link
	ln -sf linux-$BKKERNELTOBUILD linux 2>&1 | tee -a /tmp/BKlog.$$
	if [ -L linux.ac ]; then
		rm -f linux.ac
		bkstatus Linking linux.ac to linux for those using Alan Cox patches.
		ln -sf linux linux.ac
	elif [ -d linux.ac ]; then
		bkwarn linux.ac is a directory. Remove it if you want to use Alan Cox patches. Not linking.
	elif [ -f linux.ac ]; then
		bkwarn linux.ac is a file. Remove it if you want to use Alan Cox patches. Not linking.
	elif [ ! -e linux.ac ]; then
		bkstatus Linking linux.ac to linux for those using Alan Cox patches.
		ln -sf linux linux.ac
	else
		bkwarn linux.ac is not a symlink, directory, or file. What is it? Remove it if you want to use Alan Cox patches. Not linking.
	fi

	#DONT subtract out the space taken by /usr/src/linux-$BKKERNELTOBUILD
	BKFREESPACE=`df -P /usr/src | grep -v '^Filesystem' | \
	 gawk --field-separator ' ' '{ print $4 }'`
	bkstatus After opening the new kernel, the filesystem containing /usr/src has $BKFREESPACE kilobytes free.

	if [ $BKFREESPACE -lt 2000 ]; then
		if [ "$BKINTERACTIVE" = "YES" ]; then
			bkwarn I show less than 2M free in the area used to build the kernel. This may or may not be a problem. If there is a severely low amount of space, such as less than 100K, there may not have been enough space to open the kernel source. Please decide if this build should continue. If there is enough space, simply press enter. If there is not enough space, press Ctrl-C now, free up some space, and re-run $BKORIGPARAMS
			bkbeep
			read BKJUNK
		else
			if [ $BKFREESPACE -lt 100 ]; then
				bkwarn We have less than 100K in the /usr/src partition. I do not consider this to be enough to continue. Please free up some space and re-run $BKORIGPARAMS
				bkbeep
				exit
			else
				bkwarn Warning! We have between 100K and 2M in the /usr/src partition, but the current configuration asks for no interaction with the user. We will push on and hope that we do not run out of space.
			fi
		fi
	else
		bkstatus More than 2M free in /usr/src, probably OK.
	fi


	if [ -d kpatches-$BKKERNELTOBUILD ]; then
		if [ ! "$BKVERBOSE" = "NO" ]; then
			bkstatus Patching kernel. Here are the patches we will attempt to use.
			echo ----------------------------------------------------------------------- 2>&1 | tee -a /tmp/BKlog.$$
			cd /usr/src/kpatches-$BKKERNELTOBUILD
			for BKCHECKPATCH in 00 0 01 1 02 2 03 3 04 4 05 5 06 6 07 7 08 8 09 9 \
			                    10 11 12 13 14 15 16 17 18 19 20 ; do
				if [ -e /usr/src/kpatches-$BKKERNELTOBUILD/$BKCHECKPATCH ]; then
					ls -l /usr/src/kpatches-$BKKERNELTOBUILD/$BKCHECKPATCH  2>&1 | tee -a /tmp/BKlog.$$
				fi
			done
			echo ----------------------------------------------------------------------- 2>&1 | tee -a /tmp/BKlog.$$
		fi #Verbose != NO
		# _someone_ is going to use 0-9,10-20 instead of 00-20.  We deal with it.
		# We do it this way to ensure that patches are applied in numerical order.

		cd /usr/src
		BKTIMEBASE=$SECONDS
		for BKPATCH in 00 0 01 1 02 2 03 3 04 4 05 5 06 6 07 7 08 8 09 9  \
		               10 11 12 13 14 15 16 17 18 19 20                   ; do
			BKPATCHFILE=kpatches-$BKKERNELTOBUILD/$BKPATCH
			if [ -f $BKPATCHFILE ]; then
				bkheader Applying patch $BKPATCH
				ls -al $BKPATCHFILE 2>&1 | tee -a /tmp/BKlog.$$

				if [ -L $BKPATCHFILE ]; then
					BKLINKPATH=`file $BKPATCHFILE | sed -e 's/.*link to //'`
				else
					BKLINKPATH=/usr/src/$BKPATCHFILE
				fi

				BKTYPE=`file -L $BKPATCHFILE | sed -e 's/[^: ]*: //'`

				if echo $BKTYPE | grep -q "'diff' output text"; then
					bkstatus This is a straight diff file, no compression.
					BKPATCHCAT="cat $BKLINKPATH"
				elif echo $BKTYPE | grep -q "gzip compressed data"; then
					bkstatus "This is a gzipped .gz file; will gunzip on the fly ..."
					BKPATCHCAT="gzip -dc $BKLINKPATH"
				elif echo $BKTYPE | grep -q "bzip2 compressed data"; then
					bkstatus "This is a bzip2ed .bz2 file; will bunzip2 on the fly ..."
					BKPATCHCAT="bzip2 -dc $BKLINKPATH"
				elif echo $BKTYPE | grep -q "compress\'d data"; then
					bkstatus "This is a compressed .Z file; will uncompress on the fly ..."
					BKPATCHCAT="uncompress -c $BKLINKPATH"
				elif echo $BKTYPE | grep -q "English text"; then
					bkstatus This is probably a straight diff file, no compression.
					BKPATCHCAT="cat $BKLINKPATH"
				elif echo $BKTYPE | grep -q "C program text"; then		#Slightly mangled patch, probably
					bkstatus This is probably a straight diff file, no compression.
					BKPATCHCAT="cat $BKLINKPATH"
				else
					bkwarn "I don't understand patch of type $BKTYPE"
				fi

				cd /usr/src/linux
				( cd /usr/src/kpatches-$BKKERNELTOBUILD ; $BKLOAD $BKPATCHCAT ) | \
				 $BKLOAD patch -s --ignore-whitespace --remove-empty-files -p1 2>&1 | \
				 tee -a /tmp/BKlog.$$
				cd /usr/src

				if [ "`find /usr/src/linux-$BKKERNELTOBUILD/ -iname '*.rej' \
				 | wc -l | sed -e s/\ //g`" = "0" ]; then
					bkstatus No rejects from this patch
				else
					bkwarn Patching with
					ls -al $BKPATCHFILE 2>&1 | tee -a /tmp/BKlog.$$
					bkwarn created the following .rej reject files
					bkwarn 
					find /usr/src/linux-$BKKERNELTOBUILD/ -iname '*.rej' 2>&1 | tee -a /tmp/BKlog.$$
					bkwarn
					bkwarn Please fix and start me again. If you are not sure of how to fix, you might try removing /usr/src/$BKPATCHFILE and running $BKORIGPARAMS again.
#FIXME onetime: set BKOPENFRESH=YES, tell user.
					bkbeep
					exit
				fi
				unset BKPATCHCAT BKPATCHFILE BKTYPE BKLINKPATH
				BKPATCHESAPPLIED=YES
			fi
		done
		unset BKPATCH

		if [ "$BKPATCHESAPPLIED" = "YES" ]; then
			bkheader All patches applied
			bkstatus All patches applied successfully with no rejects. Patching the source took $[$SECONDS-$BKTIMEBASE] seconds.

			#Thanks to the KERNEL-HOWTO for this step...
			bkstatus Removing patch remnants, the .orig files.
			#The find command returns complete paths so we do not need to check pwd.
			find /usr/src/linux-$BKKERNELTOBUILD -name '*.orig' \
			 -exec rm -f {} ';'  2>&1 | tee -a /tmp/BKlog.$$

			#DONT subtract out the space taken by /usr/src/linux-$BKKERNELTOBUILD
			BKFREESPACE=`df -P /usr/src | grep -v '^Filesystem' | \
			 gawk --field-separator ' ' '{ print $4 }'`
			bkstatus After applying patches, the filesystem containing /usr/src has $BKFREESPACE kilobytes free.

			if [ $BKFREESPACE -lt 2000 ]; then
				if [ "$BKINTERACTIVE" = "YES" ]; then
					bkwarn I show less than 2M free in the area used to build the kernel. This may or may not be a problem. If there is a severely low amount of space, such as less than 100K, there may not have been enough space to apply the patches. Please decide if this build should continue. If there is enough space, simply press enter. If there is not enough space, press Ctrl-C now, free up some space, and re-run $BKORIGPARAMS
					bkbeep
					read BKJUNK
				else
					if [ $BKFREESPACE -lt 100 ]; then
						bkwarn We have less than 100K in the /usr/src partition. I do not consider this to be enough to continue. Please free up some space and re-run $BKORIGPARAMS
						bkbeep
						exit
					else
						bkwarn Warning!  We have between 100K and 2M in the /usr/src partition, but the current configuration asks for no interaction with the user. We will push on and hope that we do not run out of space.
					fi #BKFREESPACE lt 100K
				fi #BKINTERACTIVE=YES
			else
				bkstatus More than 2M free in /usr/src, probably OK.
			fi #BKFREESPACE lt 2M
		else
			bkheader No patches applied
			bkwarn kpatches-$BKKERNELTOBUILD exists, but no files with names 00 to 20. No patches applied. Problem? If you have no patches to apply, please remove directory /usr/src/kpatches-$BKKERNELTOBUILD. No need to exit, non-fatal error.
		fi #if patches applied
	else
		bkstatus No kpatches-$BKKERNELTOBUILD dir. No patches to apply, no problem.
	fi

	if [ ! -f /usr/src/linux-$BKKERNELTOBUILD/Makefile ]; then
		bkwarn Hmmm... there does not seem to be a Makefile in /usr/src/linux-$BKKERNELTOBUILD/. That would seem to indicate that the source files are incomplete or missing, or one of the patches removed the Makefile. Please check on this and run $BKORIGPARAMS again.
#FIXME onetime: set BKOPENFRESH=YES, tell user.
		bkbeep
		exit
	fi #if linux Makefile exists

	cd /usr/src
	if [ "$BKPATCHESAPPLIED" = "YES" ]; then
		bkstatus Checking if we patched to a higher version kernel

		eval `cat /usr/src/linux-$BKKERNELTOBUILD/Makefile | \
		 grep -E '^VERSION|^PATCHLEVEL|^SUBLEVEL|^EXTRAVERSION' | \
		 tr a-z A-Z | \
		 sed -e 's/\ //g'`

		BKNEWVER=$VERSION.$PATCHLEVEL.$SUBLEVEL
		if [ "$BKNEWVER" = ".." ]; then 
			#This probably means a messed up patch somewhere.  Pretty unlikely
			#to happen in normal operation.
			bkwarn BKNEWVER=.. - script error. This script was unable to get the correct VERSION, PATCHLEVEL, and SUBLEVEL fields from /usr/src/linux-$BKKERNELTOBUILD/Makefile . If you feel so inclined, would you help the author by letting him know why? Please email Bill Stearns, wstearns@pobox.com. Thanks.
			bkbeep
			exit
		else
			bkstatus The new version appears to be $BKNEWVER
		fi #If BKNEWVER=..
		if [ "$BKNEWVER" = "$BKKERNELTOBUILD" ]; then
			bkstatus No change in base kernel version from patching, no problem.
		else
			bkstatus Kernel version changed from $BKKERNELTOBUILD to $BKNEWVER.
			if [ -d /usr/src/linux-$BKNEWVER ]; then
				bkstatus Directory linux-$BKNEWVER found, removing. This may take a minute...
				#These rm's use complete paths, so we don't need to check pwd.
				rm -rf /usr/src/linux-$BKNEWVER/*  2>&1 | tee -a /tmp/BKlog.$$
				rm -rf /usr/src/linux-$BKNEWVER/.[a-zA-Z0-9]*  2>&1 | tee -a /tmp/BKlog.$$
				rmdir /usr/src/linux-$BKNEWVER  2>&1 | tee -a /tmp/BKlog.$$
			fi
			bkstatus Renaming source tree to match its correct version:
			bkstatus $BKKERNELTOBUILD to $BKNEWVER
			mv /usr/src/linux-$BKKERNELTOBUILD /usr/src/linux-$BKNEWVER  2>&1 | tee -a /tmp/BKlog.$$
			#This rm uses a complete path, so we don't need to check pwd.
			rm -f /usr/src/linux  2>&1 | tee -a /tmp/BKlog.$$
			ln -s linux-$BKNEWVER linux  2>&1 | tee -a /tmp/BKlog.$$
			BKKERNELTOBUILD=$BKNEWVER
		fi #End of if versions not the same
	else
		bkstatus No patches applied, we cannot have patched to a higher version.
	fi #End of if BKPATCHESAPPLIED=YES

	if [ -f configs/.config.$BKKERNELTOBUILD ]; then
		bkstatus Restoring .config
		cp --preserve configs/.config.$BKKERNELTOBUILD linux-$BKKERNELTOBUILD/.config  2>&1 | tee -a /tmp/BKlog.$$
	else
		bkstatus This kernel version does not have a pre-existing .config . We will try to get the previous .config from this series.

		bksplitversion $BKKERNELTOBUILD
		case $VERSION.$PATCHLEVEL in
		1.0)	BKLASTBUILT=$BKLASTBUILT10	;;
		1.1)	BKLASTBUILT=$BKLASTBUILT11	;;
		1.2)	BKLASTBUILT=$BKLASTBUILT12	;;
		1.3)	BKLASTBUILT=$BKLASTBUILT13	;;
		2.0)	BKLASTBUILT=$BKLASTBUILT20	;;
		2.1)	BKLASTBUILT=$BKLASTBUILT21	;;
		2.2)	BKLASTBUILT=$BKLASTBUILT22	;;
		2.3)	BKLASTBUILT=$BKLASTBUILT23	;;
		2.4)	BKLASTBUILT=$BKLASTBUILT24	;;
		2.5)	BKLASTBUILT=$BKLASTBUILT25	;;
		2.6)	BKLASTBUILT=$BKLASTBUILT26	;;
		2.7)	BKLASTBUILT=$BKLASTBUILT27	;;
		3.0)	BKLASTBUILT=$BKLASTBUILT30	;;
		3.1)	BKLASTBUILT=$BKLASTBUILT31	;;
		3.2)	BKLASTBUILT=$BKLASTBUILT32	;;
		3.3)	BKLASTBUILT=$BKLASTBUILT33	;;
		4.0)	BKLASTBUILT=$BKLASTBUILT40	;;
		4.1)	BKLASTBUILT=$BKLASTBUILT41	;;
		*)
			bkwarn I am not aware of the kernel series $VERSION.$PATCHLEVEL . Please get a new version of the buildkernel script at http://www.pobox.com/~wstearns/buildkernel/
			BKLASTBUILT=""
			;;
		esac

		if [ -f configs/.config.$BKLASTBUILT ]; then
			bkstatus Restoring .config from version $BKLASTBUILT
			cp --preserve configs/.config.$BKLASTBUILT linux-$BKKERNELTOBUILD/.config  2>&1 | tee -a /tmp/BKlog.$$
		else
			bkstatus Sorry, cannot find a previous .config from this series either.
		fi
	fi


	if [ -f configs/.version.$BKKERNELTOBUILD ]; then
		bkstatus Restoring .version
		cp --preserve configs/.version.$BKKERNELTOBUILD linux-$BKKERNELTOBUILD/.version  2>&1 | tee -a /tmp/BKlog.$$
	else
		bkstatus No .version to restore, no problem
	fi
fi #End of if BKOPENFRESH=YES
unset BKPATCHESAPPLIED

cd /usr/src
if [ ! -f linux-$BKKERNELTOBUILD/.config ]; then
	bkstatus We _still_ do not have a .config . As a last resort we will try to get /usr/src/configs/.config .
	if [ -f /usr/src/configs/.config ]; then
		cp -b -f configs/.config linux-$BKKERNELTOBUILD/.config 2>&1 | tee -a /tmp/BKlog.$$
		bkstatus Finally!  We found a .config to use. Check the configuration carefully to make sure it is correct.
	else
		bkwarn We were not able to find a .config to use. You will need to check the kernel configuration carefully.
	fi
fi

callkids POSTOPENSOURCE


#Warn the user if they are trying to build a development version.
bksplitversion $BKKERNELTOBUILD
case $PATCHLEVEL in
1|3|5|7|9|11|13|15|17|19)
	bkbeep
	bkwarn You are about to build a development kernel. This kernel may not work correctly on your system, up to and including causing filesystem corruption and/or loss of data. If you choose to continue, you are encouraged to subscribe to the linux-kernel mailing list to share experiences and fixes. To subscribe to this list: send the line \"subscribe linux-kernel\" in the body of a message to majordomo@vger.rutgers.edu and please read the FAQ at http://www.tux.org/lkml/ . If you would like to abort this build, press Ctrl-C now. Otherwise, the build will recommence in 15 seconds.
	sleep 15
	;;
esac


cd /usr/src
#Make sure the /usr/src/linux link is created even if we did not open fresh
if [ -L /usr/src/linux ]; then
	bkstatus Removing symlink /usr/src/linux
	#We toast it so the following ln will work correctly.
	#This rm uses a complete path, so we do not need to check pwd.
	rm -f /usr/src/linux
fi
#/usr/src/linux should _not_ exist at this point, so we should have no 
#problem with creating links inside the linux directory.
bkstatus Creating symlink /usr/src/linux
ln -sf linux-$BKKERNELTOBUILD linux  2>&1 | tee -a /tmp/BKlog.$$

if [ ! -f /usr/src/linux-$BKKERNELTOBUILD/Makefile ]; then
	bkwarn Hmmm... there does not seem to be a Makefile in /usr/src/linux-$BKKERNELTOBUILD/ . That would seem to indicate that the source files are incomplete or missing. Please check on this and run $BKORIGPARAMS again.
#FIXME onetime: set BKOPENFRESH=YES, tell user.
	bkbeep
	exit
fi



#-----------------------------------------------------------------------
bkheader Removing code for other architectures
#-----------------------------------------------------------------------
cd /usr/src/linux-$BKKERNELTOBUILD/arch
if [ ! "`pwd`" = "/usr/src/linux-$BKKERNELTOBUILD/arch" ]; then
	bkwarn We were not able to sucessfully go to the /usr/src/linux-$BKKERNELTOBUILD/arch directory. This could be dangerous as we are about to delete a number of directories. Exiting.
	bkbeep
	exit
fi
#FIXME tcsh sets HOSTTYPE to i386-linux.  Ugh.
#We test below to see if HOSTTYPE exists as an arch directory, so this 
#is not as urgent.
#if [ -z "$HOSTTYPE" ]; then
#	if [ -n "$MACHTYPE" ]; then
#		HOSTTYPE=$MACHTYPE		#A tcsh-ism, I think
#	else
#		bkwarn Unable to automatically determine your hardware platform. Since I cannot figure out what to remove, I must leave all platform specific directories in, wasting about 10M. To avoid this in the future, simply set the environment variable HOSTTYPE to your hardware architecture before starting buildkernel. For example: export HOSTTYPE=i386 would do it in bash.
#		sleep 20
#	fi
#fi

if [ -n "$HOSTTYPE" ]; then
	if [ -d "$HOSTTYPE" ]; then
#FIXME - handle deleting other-arch rules.cml more gracefully.  cml2 bags out if missing:
#python2 -O cml2/cmlcompile.py rules.cml 
#Compiling rules, please wait...|
#"rules.cml", line 82:  I/O error while opening 'arch/alpha/rules.cml'
#make: *** [rules.out] Error 1
		chattr +i /usr/src/linux-$BKKERNELTOBUILD/arch/*/rules.cml
		for BKARCHI in *; do
			if [ "$HOSTTYPE" = "$BKARCHI" ]; then
				bkstatus Keeping $BKARCHI
			elif [ "$BKARCHI" = "um" ]; then
				bkstatus Keeping the User Mode Linux architecture.
			elif [ `echo $HOSTTYPE | cut -b 1-5` = `echo $BKARCHI | cut -b 1-5` ]; then 
				#Sparc needs sparc64 code.
				bkstatus Keeping $BKARCHI because the code may be needed for $HOSTTYPE
			else
				if [ -d $BKARCHI ]; then 
					bkstatus Removing $BKARCHI
					#These rm's use complete paths, so we don't need to check pwd.
					#We have already checked that we are, in fact, in the 
					#/usr/src/linux-version/arch directory anyways.
					$BKLOAD rm -rf /usr/src/linux-$BKKERNELTOBUILD/arch/$BKARCHI/*  2>&1 | tee -a /tmp/BKlog.$$
					$BKLOAD rm -rf /usr/src/linux-$BKKERNELTOBUILD/arch/$BKARCHI/.[a-zA-Z0-9]*  2>&1 | tee -a /tmp/BKlog.$$
					rmdir /usr/src/linux-$BKKERNELTOBUILD/arch/$BKARCHI  2>&1 | tee -a /tmp/BKlog.$$
				else 
					bkstatus no $BKARCHI directory.
				fi

				if [ -d ../include/asm-$BKARCHI ]; then 
					bkstatus Removing asm-$BKARCHI
					#These rm's use complete paths, so we don't need to check pwd.
					$BKLOAD rm -rf /usr/src/linux-$BKKERNELTOBUILD/include/asm-$BKARCHI/* 2>&1 | tee -a /tmp/BKlog.$$
					$BKLOAD rm -rf /usr/src/linux-$BKKERNELTOBUILD/include/asm-$BKARCHI/.[a-zA-Z0-9]* 2>&1 | tee -a /tmp/BKlog.$$
					rmdir /usr/src/linux-$BKKERNELTOBUILD/include/asm-$BKARCHI 2>&1 | tee -a /tmp/BKlog.$$
				else 
					bkstatus no asm-$BKARCHI directory.
				fi
			fi
		done
		chattr -i /usr/src/linux-$BKKERNELTOBUILD/arch/*/rules.cml
		unset BKARCHI
	else
		bkwarn The /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE directory does not exist. Because of this, buildkernel is unable to automatically determine your hardware platform. Since I cannot figure out what to remove, I must leave all platform specific directories in, wasting about 10M. To avoid this in the future, simply set the environment variable HOSTTYPE to your hardware architecture before starting buildkernel. For example: export HOSTTYPE=i386 would do it in bash. This variable setting must match one of the directories in /usr/src/linux-$BKKERNELTOBUILD/arch/ .
		bkbeep
		sleep 20
	fi	#if -d HOSTTYPE
fi	#-n HOSTTYPE


cd /usr/src/linux-$BKKERNELTOBUILD
#-----------------------------------------------------------------------
bkheader Setting kernel configuration
#-----------------------------------------------------------------------
#Note: In the following we'll be modifying Makefile and .config.  I make a 
#backup of the original file first, then make any changes. If the original
#version is _identical_ to the changed version, I restore the orginal
#version.  Why?  This keeps the date and time of the original, so we don't 
#prompt gnu make to rebuild things on the logic that the file has changed 
#when in fact it hasn't.  If the user changes even one byte, the new 
#version is used.

#The "cp --preserve" approach keeps the original date and time w/ x.bkorig
if [ -f /usr/src/linux-$BKKERNELTOBUILD/Makefile.bkorig ]; then
	rm -f /usr/src/linux-$BKKERNELTOBUILD/Makefile.bkorig
fi
if [ -f /usr/src/linux-$BKKERNELTOBUILD/Makefile ]; then
	cp --preserve Makefile Makefile.bkorig
fi
if [ -f /usr/src/linux-$BKKERNELTOBUILD/.config.bkorig ]; then
	rm -f /usr/src/linux-$BKKERNELTOBUILD/.config.bkorig
fi
if [ -f /usr/src/linux-$BKKERNELTOBUILD/.config ]; then
	cp --preserve .config .config.bkorig
fi

if [ `cat Makefile | sed -e s/\ //g | grep 'SMP=' | \
 wc -l | sed -e s/\ //g` -ge 1 ]; then
	bkstatus Makefile has a reference to "SMP="
	if [ -f /proc/cpuinfo ]; then
		if [ "`cat /proc/cpuinfo | grep ^processor | wc -l | \
		 sed -e s/\ //g`" = "1" ]; then
			bkstatus IT SEEMS this machine has a single processor, but I might be wrong. We will set the Makefile to build a uniprocessor kernel.
			cat Makefile | sed -e 's/^[# \t]*SMP[ \t]*=[ \t]*[01][ \t]*$/# SMP = 1/' >Makefile.tmp
		else
			bkstatus IT SEEMS this machine has more than one processor. We will set the Makefile to build an SMP kernel.
			cat Makefile | sed -e 's/^[# \t]*SMP[ \t]*=[ \t]*[01][ \t]*$/SMP = 1/' >Makefile.tmp
		fi #if there is a single processor
		if [ "$BKINTERACTIVE" = "YES" ]; then
			bkstatus You can change this in a moment if you like. Please do change this if you know this to be incorrect.
		else
			bkstatus Because user has requested no interaction, there will not be an opportunity to change this.
		fi
		mv -f Makefile.tmp Makefile
	else
		bkstatus Because /proc/cpuinfo is missing, I cannot tell whether to build this kernel for Uniprocessor or SMP.
		if [ "$BKINTERACTIVE" = "YES" ]; then
			bkstatus Please set this by hand in a moment by commenting or uncommenting the "SMP=1" line. If you know your machine has a single processor, put a hash mark at the beginning of the line. If you know your machine has more than one processor, remove the hash mark.
		else
			bkstatus This will be left at the default.
		fi #Interactive
	fi #If /proc/cpuinfo exists
else
	bkstatus Makefile has no reference to "SMP=". Either this kernel only supports uniprocessor mode, or SMP Mode is not set in the Makefile for this kernel. In the latter case, it is probably set in the standard configuration, coming up.
fi #If there is a reference to SMP= in the Makefile.



#FIXME reinstate following when we can work around not running config.
if [ "$BKINTERACTIVE" = "YES" ]; then
	bkbeep
	if [ "$BKVERBOSE" != "NO" ]; then
		bkstatus About to let you manually edit the Makefile and set .config options. When done, save changes and exit.
		bkstatus NOTE - If you do not have any changes to make, simply save and exit. Please press enter when ready to edit Makefile.
		read BKJUNK
	fi
	$EDITOR Makefile
	bkstatus About to set kernel configuration - this takes a moment to set up.
	bkbeep
	if make $BKMAKETYPE ; then
		bkstatus make $BKMAKETYPE succeeded.
	else
		bkwarn make $BKMAKETYPE failed. Please fix the problem and restart.
		bkbeep
		exit
	fi
	if [ ! -f /usr/src/linux-$BKKERNELTOBUILD/include/linux/autoconf.h ]; then
		bkbeep
		bkwarn /usr/src/linux-$BKKERNELTOBUILD/include/linux/autoconf.h is missing. Is there any chance you did not save your changes to the kernel configuration? If you later get problems in the kernel build, restart it and make sure you save your kernel configuration. Build will automatically continue in 30 seconds.
		sleep 30
	fi #if autoconf.h does not exist
else
	bkstatus No configuration will be manually set as Interactivity is off.
#We run menuconfig, but feed it 'x' to exit and 'y' to save changes.
#FIXME handle this in a way that does not require x or ncurses.
#	make menuconfig <<EOTEXT
#xy
#EOTEXT
#	#FIXME - what _really_ has to be done?
#	#Make symlinks gets run as part of the make {config|xconfig|menuconfig} 
#	#process, but in non-interactive mode these don't get run.  Run it 
#	#manually now.
#	make symlinks
#	#make noconfig

#possible fix?
	make oldconfig
fi

cd /usr/src/linux-$BKKERNELTOBUILD
if [ -f Makefile.bkorig ]; then
	if [ "`diff Makefile.bkorig Makefile | wc -l | sed -e s/\ //g`" = "0" ]; then
		bkstatus No changes were made to the Makefile
		rm -f /usr/src/linux-$BKKERNELTOBUILD/Makefile
		mv -f Makefile.bkorig Makefile
	else
		bkstatus Changes were made to the Makefile
		#bkstatus Saving the kernel Makefile to the log...
		#cat /usr/src/linux-$BKKERNELTOBUILD/Makefile 2>&1 >>/tmp/BKlog.$$
		bkstatus The Makefile changes are being saved to the log...
		diff Makefile.bkorig Makefile 2>&1 >>/tmp/BKlog.$$
		rm -f /usr/src/linux-$BKKERNELTOBUILD/Makefile.bkorig
	fi
fi

if [ -f .config.bkorig ]; then
	if [ "`diff .config.bkorig .config | wc -l | sed -e s/\ //g`" = "0" ]; then
		bkstatus No changes were made to the kernel configuration
		rm -f /usr/src/linux-$BKKERNELTOBUILD/.config
		mv -f .config.bkorig .config
	else
		bkstatus Changes were made to the kernel configuration
		rm -f /usr/src/linux-$BKKERNELTOBUILD/.config.bkorig
	fi
fi

#.config should exist now after manual or automatic configuration
cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/.config /usr/src/configs/.config.$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$

bkstatus Saving the kernel configuration to the log...
cat /usr/src/linux-$BKKERNELTOBUILD/.config 2>&1 >>/tmp/BKlog.$$
bkstatus


callkids POSTCONFIGURE


#-----------------------------------------------------------------------
bkheader Main kernel build.
#-----------------------------------------------------------------------
cd /usr/src/linux-$BKKERNELTOBUILD

#bkwarn !!!!
#bkwarn As a test, this script will not be making dependencies in an attempt to see if the main build process makes them on demand. Please let the author, Bill Stearns, wstearns@pobox.com, know if you encounter any problems with this approach. Thanks
#bkwarn !!!!
#FIXME see if no "make dep" is OK.
#sleep 15

bksplitversion $BKKERNELTOBUILD
case $VERSION.$PATCHLEVEL in
1.0|1.1|1.2|1.3|2.0)
	bkstatus This 1.x or 2.0 kernel requires making dependencies.
	BKTIMEBASE=$SECONDS
	$BKLOAD make dep  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus Creating the dependencies took $[$SECONDS-$BKTIMEBASE] seconds.
	;;
*)
	bkwarn This kernel is newer than 2.0 - I will not make dependencies now. If this build fails, I will make them later.
	;;
esac

if [ "$BKMAKECLEANBEFORE" = "YES" ]; then
	bkstatus Cleaning out old compiled code for a fresh compile
	$BKLOAD make clean  2>&1 | tee -a /tmp/BKlog.$$
else
	bkstatus Current configuration chooses not to make clean, no problem.
fi

#FIXME - this autodetect forces a uml build even when building a regular kernel that simply
#has the uml tree in it.
#if [ -d /usr/src/linux-$BKKERNELTOBUILD/arch/um ]; then
#	bkwarn UserModeLinux requires a build target of \"linux\".  Resetting the build target to linux now.
#	BKBUILDTYPE="linux"
#fi

if [ -f /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE ]; then
	rm -f /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE
fi

# Compile kernel
# Ugly exit code hack seems to be required to preserve exit code from make
# (if there's a better way, please e-mail adam@spiers.net about it to
# satisfy his curiosity)
bkstatus Compiling actual kernel
BKTIMEBASE=$SECONDS
( $BKLOAD make $BKBUILDTYPE MAKE="make -j $BKNUMBUILDS"; \
 echo $? > /tmp/BKerr.$$ ) 2>&1 | tee -a /tmp/BKlog.$$
if [ "`cat /tmp/BKerr.$$`" -eq 0 ]; then
	BKBUILDSUCCEEDED="YES"
else
	BKBUILDSUCCEEDED="NO"
fi
rm -f /tmp/BKerr.$$

#FIXME Integrate gcc parameter filter

bkstatus Compiling the base kernel took $[$SECONDS-$BKTIMEBASE] seconds.

bkstatus Please look at the previous lines. If you see the message:
bkstatus \"System is too big. Try using bzImage or modules.\"
bkstatus this build did not succeed. Please press Ctrl-C now and restart the build with:
bkstatus $BKORIGPARAMS --BKBUILDTYPE=bzImage
bkstatus If you would like to avoid having to type this every time, add:
bkstatus BKBUILDTYPE=bzImage
bkstatus to /etc/bkrc and restart the build. Else, press Enter to go on.
bkbeep
read BKJUNK


if [ "$BKBUILDSUCCEEDED" = "NO" ]; then
	#If anything but a bzImage or bzdisk failed, we try that now.
	bkwarn Building $BKBUILDTYPE failed to create a kernel.

	#DONT subtract out the space taken by /usr/src/linux-$BKKERNELTOBUILD
	BKFREESPACE=`df -P /usr/src | grep -v '^Filesystem' | \
	 gawk --field-separator ' ' '{ print $4 }'`
	if [ $BKFREESPACE -lt 2000 ]; then
		bkwarn The filesystem where the build took place has $BKFREESPACE kilobytes free. This may have been a factor in the failed build.
	fi #if lt 2M free.
	if [ "`echo $BKBUILDTYPE | cut -b 1`" = "b" ]; then
		bkwarn Since we already tried to build a $BKBUILDTYPE kernel, we do not have another build type to try automatically. Please look at any above error messages to see why the build failed, then re-run $BKORIGPARAMS
#FIXME onetime: set BKOPENFRESH=YES, tell user.
		bkbeep
		exit
	else
		bkstatus On the chance that the previous build failed because the kernel was too big, we will retry the build, this time using b$BKBUILDTYPE as the target rather than the $BKBUILDTYPE we just tried. We will also run make dep and make clean as the 2.0 kernels seem to need these.
		BKBUILDTYPE=b$BKBUILDTYPE
		bkstatus Retrying to build actual kernel, this time as $BKBUILDTYPE

		if [ -f /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE ]; then
			bkwarn This is the file left after the build attempt:
			ls -al /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE 2>&1 | tee -a /tmp/BKlog.$$
			bkwarn Removing incomplete kernel.
			rm -f /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE 2>&1 | tee -a /tmp/BKlog.$$
		fi
		bkstatus Running make dep
		$BKLOAD make dep MAKE="make -j $BKNUMBUILDS"  2>&1 | tee -a /tmp/BKlog.$$
		bkstatus Running make clean
		$BKLOAD make clean MAKE="make -j $BKNUMBUILDS"  2>&1 | tee -a /tmp/BKlog.$$
		BKTIMEBASE=$SECONDS
		( $BKLOAD make $BKBUILDTYPE MAKE="make -j $BKNUMBUILDS"; \
		 echo $? > /tmp/BKerr.$$ ) 2>&1 | tee -a /tmp/BKlog.$$
		if [ "`cat /tmp/BKerr.$$`" -eq 0 ]; then
			BKBUILDSUCCEEDED="YES"
		else
			BKBUILDSUCCEEDED="NO"
		fi
		rm -f /tmp/BKerr.$$
		bkstatus Second attempt at base kernel build took $[$SECONDS-$BKTIMEBASE] seconds.

		if [ "$BKBUILDSUCCEEDED" = "NO" ]; then
			bkwarn Second attempt to build $BKBUILDTYPE kernel also failed. Please look at any above error messages to see why the build failed. Please check and rerun $BKORIGPARAMS
			cd /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot
#FIXME onetime: set BKOPENFRESH=YES, tell user.
			bkbeep
			exit
		else
			bkstatus Second kernel build succeeded. /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE exists.
		fi
	fi
else
	bkstatus First build succeeded. /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE exists.
fi
#We get here if the first or second build succeeded.

callkids POSTKERNELBUILD


cd /usr/src/linux-$BKKERNELTOBUILD
eval `cat /usr/src/linux-$BKKERNELTOBUILD/.config | \
 grep 'CONFIG_MODULES' | tr a-z A-Z`

cd /usr/src/linux-$BKKERNELTOBUILD
if [ "$CONFIG_MODULES" = "Y" ]; then
	#-----------------------------------------------------------------------
	bkheader Compiling modules
	#-----------------------------------------------------------------------
	BKTIMEBASE=$SECONDS
	$BKLOAD make -k modules MAKE="make -j $BKNUMBUILDS"  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus Creating the kernel modules took $[$SECONDS-$BKTIMEBASE] seconds.
else
	bkstatus Kernel configuration does not include modules, so no modules built, no problem.
fi

if [ -f /usr/src/linux-$BKKERNELTOBUILD/.version ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/.version /usr/src/configs/.version.$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
fi
if [ -f /usr/src/linux-$BKKERNELTOBUILD/.config ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/.config /usr/src/configs/.config.$BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/.config /usr/src/configs/.config 2>&1 | tee -a /tmp/BKlog.$$
fi

callkids POSTMODULEBUILD


#FIXME should we clear out BKINSTALLROOT?  For the moment, we won't.  We're keeping track of all files to be tarred.
#If there's other stuff there, tough.


FILELIST=""

cd /usr/src/linux-$BKKERNELTOBUILD
if [ "$CONFIG_MODULES" = "Y" ]; then
	#-----------------------------------------------------------------------
	bkheader Installing modules
	#-----------------------------------------------------------------------
	bkstatus Installing modules in module directory
	export INSTALL_PATH=$BKINSTALLROOT INSTALL_MOD_PATH=$BKINSTALLROOT
	INSTALL_PATH=$BKINSTALLROOT INSTALL_MOD_PATH=$BKINSTALLROOT $BKLOAD make modules_install  2>&1 | tee -a /tmp/BKlog.$$

	cd $BKINSTALLROOT
	for ONEMOD in `ls -A1 lib/modules/$BKKERNELTOBUILD$EXTRAVERSION/*/*` ; do
		FILELIST="$FILELIST $ONEMOD"
	done
fi


cd /usr/src/
#-----------------------------------------------------------------------
bkheader Building and installing ksymoops.
#-----------------------------------------------------------------------
if [ -f /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops.cc ]; then
	bkstatus Building ksymoops
	cd /usr/src/linux-$BKKERNELTOBUILD/scripts
	g++ -o ksymoops ksymoops.cc
	bkstatus Installing ksymoops
	cp -bfp /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops $BKINSTALLROOT/usr/bin/ksymoops
	FILELIST="$FILELIST usr/bin/ksymoops"
	cp -bfp /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops $BKINSTALLROOT/boot/ksymoops
	FILELIST="$FILELIST boot/ksymoops"
elif [ -f /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops/Makefile ]; then
	bkstatus Building ksymoops
	cd /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops
	$BKLOAD make MAKE="make -j $BKNUMBUILDS"  2>&1 | tee -a /tmp/BKlog.$$
	bkstatus Installing ksymoops
	cp -bfp /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops/ksymoops $BKINSTALLROOT/usr/bin/ksymoops
	FILELIST="$FILELIST usr/bin/ksymoops"
	cp -bfp /usr/src/linux-$BKKERNELTOBUILD/scripts/ksymoops/ksymoops $BKINSTALLROOT/boot/ksymoops
	FILELIST="$FILELIST boot/ksymoops"
else
	bkstatus No ksymoops in the kernel tree to compile, skipping.
fi


#-----------------------------------------------------------------------
bkheader Installing new kernel.
#-----------------------------------------------------------------------
bkstatus Copying kernel to boot directory.
cd /usr/src/linux-$BKKERNELTOBUILD

# By including .version, BKIMAGEVER hopefully makes a unique filename
BKIMAGEVER=$BKKERNELTOBUILD-`cat /usr/src/linux-$BKKERNELTOBUILD/.version`

if [ -f /boot/$BKBUILDTYPE-$BKIMAGEVER ] || [ -f /boot/System.map-$BKIMAGEVER ]; then
	bkwarn Ugh. I had attempted to create a unique filename for the new kernel that was just created, but that file already exists.
	bkbeep
fi
while [ -f /boot/$BKBUILDTYPE-$BKIMAGEVER ] || [ -f /boot/System.map-$BKIMAGEVER ]; do
	#This technically makes the output from uname and the file names not match.  At least we don't overwrite old kernels.
	BKKERNELVERSION=$[`cat /usr/src/linux-$BKKERNELTOBUILD/.version` + 1]
	echo "$BKKERNELVERSION" >/usr/src/linux-$BKKERNELTOBUILD/.version
	bkwarn Incrementing the kernel version to $BKKERNELVERSION.
	BKIMAGEVER=$BKKERNELTOBUILD-$BKKERNELVERSION
done
unset BKKERNELVERSION

if [ -e /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
	FILELIST="$FILELIST boot/$BKBUILDTYPE-$BKIMAGEVER"
elif [ -e /usr/src/linux-$BKKERNELTOBUILD/$BKBUILDTYPE ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/$BKBUILDTYPE $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
	FILELIST="$FILELIST boot/$BKBUILDTYPE-$BKIMAGEVER"
else
	bkwarn Cannot find $BKBUILDTYPE in /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/ or /usr/src/linux-$BKKERNELTOBUILD/ . You will need to manually copy it.
fi

bkstatus Copying System.map to boot directory.
if [ -e /usr/src/linux-$BKKERNELTOBUILD/System.map ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/System.map $BKINSTALLROOT/boot/System.map-$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
	FILELIST="$FILELIST boot/System.map-$BKIMAGEVER"
else
	bkwarn Cannot find System.map in /usr/src/linux-$BKKERNELTOBUILD/ . You will need to manually copy it.
fi

bkstatus Copying .config to boot directory.
if [ -f /usr/src/linux-$BKKERNELTOBUILD/.config ]; then
	cp -b -f --preserve /usr/src/linux-$BKKERNELTOBUILD/.config $BKINSTALLROOT/boot/.config.$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
	FILELIST="$FILELIST boot/.config.$BKIMAGEVER"
else
	bkwarn Cannot find .config in /usr/src/linux-$BKKERNELTOBUILD/ . You will need to manually copy it.
fi


if type -path rdev >/dev/null; then
	if [ -n "$BKROOTDEVICE" ]; then
		bkstatus Forcing root device
		rdev $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER $BKROOTDEVICE  2>&1 | tee -a /tmp/BKlog.$$
	fi

	bkstatus Telling kernel to mount root read-only
	rdev -R $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER 1 2>&1 | tee -a /tmp/BKlog.$$

	case "$BKVIDMODE" in
	"-2")	bkstatus Setting 80x50 line video				;;
	"-1")	bkstatus Setting 80x25 line video				;;
	*)	bkstatus Setting video mode number $BKVIDMODE			;;
	esac
	rdev -v $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER $BKVIDMODE 2>&1 | tee -a /tmp/BKlog.$$
else
	bkwarn rdev utility not available. I cannot force boot settings into the kernel. Please note that this may not be a problem, but is worth investigating if you have problems booting. In particular, this tool does not appear to be necessary on Sun Sparcs.
fi

if [ "$BKCOPYTOFLOPPY" = "YES" ]; then
	if [ "$BKINTERACTIVE" = "YES" ]; then
		bkstatus Copying to floppy - make sure a floppy is in the drive,
		if [ "`mount | grep '^/dev/fd0' | wc -l | sed -e 's/\ //g'`" != "0" ]; then
			bkwarn WARNING!!!!!!!!!!!!
			bkwarn The floppy appears to be mounted. If we proceed with copying the kernel, it will overwrite what is probably a floppy with your data. I STRONGLY recommend that you unmount the floppy. If you wish to do so, switch to another console, unmount the floppy, place the correct floppy in the drive without mounting it, switch back, and
		fi
		if [ "$BKVERBOSE" != "NO" ]; then
			bkstatus Press enter when ready.
			bkbeep
			read BKJUNK
		fi
		cp $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER /dev/fd0 2>&1 | tee -a /tmp/BKlog.$$
	else
		bkstatus User has asked for no interaction.
		if [ ! "`mount | grep '^/dev/fd0' | wc -l | sed -e 's/\ //g'`" = "0" ]; then
			bkwarn WARNING!!!!!!!!!!!!
			bkwarn The floppy appears to be mounted. If we proceed with copying the kernel, it will overwrite what is probably a floppy with your data. To avoid a tragedy, this script WILL NOT copy the kernel to the floppy. You can copy /boot/$BKBUILDTYPE-$BKIMAGEVER to /dev/fd0 later once you have unmounted the current floppy and inserted the floppy on which the kernel should be placed. The command
			if [ -e /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE ]; then
				bkwarn cp /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/$BKBUILDTYPE /dev/fd0
			elif [ -e /usr/src/linux-$BKKERNELTOBUILD/$BKBUILDTYPE ]; then
				bkwarn cp /usr/src/linux-$BKKERNELTOBUILD/$BKBUILDTYPE /dev/fd0
			fi
			bkwarn will accomplish that task.
		else
			bkstatus Script will blindly try to copy image to a floppy. I hope there is a floppy in the drive...
#FIXME how to test whether there is a floppy in the drive?
			cp $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER /dev/fd0 2>&1 | tee -a /tmp/BKlog.$$
		fi #floppy mounted
	fi #INTERACTIVE=YES
else
	bkstatus Current configuration does not request making a bootable floppy, no problem.
fi #copytofloppy=yes


bkstatus Creating load information block for lilo
cat >>$BKINSTALLROOT/etc/lilo.conf-$BKIMAGEVER <<EOTEXT
#The following boot section was added by buildkernel on
#`date`
#Please feel free to move this section, edit it, and remove these comments.
image=/boot/$BKBUILDTYPE-$BKIMAGEVER
	label=$BKIMAGEVER
	root=$BKROOTDEVICE
	read-only
#End of autoinstall
EOTEXT
FILELIST="$FILELIST etc/lilo.conf-$BKIMAGEVER"

bkstatus Creating load information block for silo
cat >>$BKINSTALLROOT/etc/silo.conf-$BKIMAGEVER <<EOTEXT
#The following boot section was added by buildkernel on
#`date`
#Please feel free to move this section, edit it, and remove these comments.
image=/boot/$BKBUILDTYPE-$BKIMAGEVER
	label=$BKIMAGEVER
	root=$BKROOTDEVICE
	read-only
#End of autoinstall
EOTEXT
FILELIST="$FILELIST etc/silo.conf-$BKIMAGEVER"

bkstatus Creating load information block for grub
#FIXME - and make this more general
#FIXME - mixed IDE/scsi.  Ugh.
case $BKROOTDEVICE in
/dev/hda1|/dev/sda1)		BKGRUBROOT='(hd0,0)'		;;
/dev/hda2|/dev/sda2)		BKGRUBROOT='(hd0,1)'		;;
/dev/hda3|/dev/sda3)		BKGRUBROOT='(hd0,2)'		;;
/dev/hda4|/dev/sda4)		BKGRUBROOT='(hd0,3)'		;;
/dev/hda5|/dev/sda5)		BKGRUBROOT='(hd0,4)'		;;
/dev/hda6|/dev/sda6)		BKGRUBROOT='(hd0,5)'		;;
/dev/hda7|/dev/sda7)		BKGRUBROOT='(hd0,6)'		;;
/dev/hda8|/dev/sda8)		BKGRUBROOT='(hd0,7)'		;;
/dev/hda9|/dev/sda9)		BKGRUBROOT='(hd0,8)'		;;
/dev/hda10|/dev/sda10)		BKGRUBROOT='(hd0,9)'		;;
/dev/hda11|/dev/sda11)		BKGRUBROOT='(hd0,10)'		;;
/dev/hda12|/dev/sda12)		BKGRUBROOT='(hd0,11)'		;;
/dev/hda13|/dev/sda13)		BKGRUBROOT='(hd0,12)'		;;
/dev/hda14|/dev/sda14)		BKGRUBROOT='(hd0,13)'		;;
/dev/hda15|/dev/sda15)		BKGRUBROOT='(hd0,14)'		;;
/dev/hda16|/dev/sda16)		BKGRUBROOT='(hd0,15)'		;;
/dev/hdb1|/dev/sdb1)		BKGRUBROOT='(hd1,0)'		;;
/dev/hdb2|/dev/sdb2)		BKGRUBROOT='(hd1,1)'		;;
/dev/hdb3|/dev/sdb3)		BKGRUBROOT='(hd1,2)'		;;
/dev/hdb4|/dev/sdb4)		BKGRUBROOT='(hd1,3)'		;;
/dev/hdb5|/dev/sdb5)		BKGRUBROOT='(hd1,4)'		;;
/dev/hdb6|/dev/sdb6)		BKGRUBROOT='(hd1,5)'		;;
/dev/hdb7|/dev/sdb7)		BKGRUBROOT='(hd1,6)'		;;
/dev/hdb8|/dev/sdb8)		BKGRUBROOT='(hd1,7)'		;;
/dev/hdb9|/dev/sdb9)		BKGRUBROOT='(hd1,8)'		;;
/dev/hdb10|/dev/sdb10)		BKGRUBROOT='(hd1,9)'		;;
/dev/hdb11|/dev/sdb11)		BKGRUBROOT='(hd1,10)'		;;
/dev/hdb12|/dev/sdb12)		BKGRUBROOT='(hd1,11)'		;;
/dev/hdb13|/dev/sdb13)		BKGRUBROOT='(hd1,12)'		;;
/dev/hdb14|/dev/sdb14)		BKGRUBROOT='(hd1,13)'		;;
/dev/hdb15|/dev/sdb15)		BKGRUBROOT='(hd1,14)'		;;
/dev/hdb16|/dev/sdb16)		BKGRUBROOT='(hd1,15)'		;;
/dev/hdc1|/dev/sdc1)		BKGRUBROOT='(hd2,0)'		;;
/dev/hdc2|/dev/sdc2)		BKGRUBROOT='(hd2,1)'		;;
/dev/hdc3|/dev/sdc3)		BKGRUBROOT='(hd2,2)'		;;
/dev/hdc4|/dev/sdc4)		BKGRUBROOT='(hd2,3)'		;;
/dev/hdc5|/dev/sdc5)		BKGRUBROOT='(hd2,4)'		;;
/dev/hdc6|/dev/sdc6)		BKGRUBROOT='(hd2,5)'		;;
/dev/hdc7|/dev/sdc7)		BKGRUBROOT='(hd2,6)'		;;
/dev/hdc8|/dev/sdc8)		BKGRUBROOT='(hd2,7)'		;;
/dev/hdc9|/dev/sdc9)		BKGRUBROOT='(hd2,8)'		;;
/dev/hdc10|/dev/sdc10)		BKGRUBROOT='(hd2,9)'		;;
/dev/hdc11|/dev/sdc11)		BKGRUBROOT='(hd2,10)'		;;
/dev/hdc12|/dev/sdc12)		BKGRUBROOT='(hd2,11)'		;;
/dev/hdc13|/dev/sdc13)		BKGRUBROOT='(hd2,12)'		;;
/dev/hdc14|/dev/sdc14)		BKGRUBROOT='(hd2,13)'		;;
/dev/hdc15|/dev/sdc15)		BKGRUBROOT='(hd2,14)'		;;
/dev/hdc16|/dev/sdc16)		BKGRUBROOT='(hd2,15)'		;;
/dev/hdd1|/dev/sdd1)		BKGRUBROOT='(hd3,0)'		;;
/dev/hdd2|/dev/sdd2)		BKGRUBROOT='(hd3,1)'		;;
/dev/hdd3|/dev/sdd3)		BKGRUBROOT='(hd3,2)'		;;
/dev/hdd4|/dev/sdd4)		BKGRUBROOT='(hd3,3)'		;;
/dev/hdd5|/dev/sdd5)		BKGRUBROOT='(hd3,4)'		;;
/dev/hdd6|/dev/sdd6)		BKGRUBROOT='(hd3,5)'		;;
/dev/hdd7|/dev/sdd7)		BKGRUBROOT='(hd3,6)'		;;
/dev/hdd8|/dev/sdd8)		BKGRUBROOT='(hd3,7)'		;;
/dev/hdd9|/dev/sdd9)		BKGRUBROOT='(hd3,8)'		;;
/dev/hdd10|/dev/sdd10)		BKGRUBROOT='(hd3,9)'		;;
/dev/hdd11|/dev/sdd11)		BKGRUBROOT='(hd3,10)'		;;
/dev/hdd12|/dev/sdd12)		BKGRUBROOT='(hd3,11)'		;;
/dev/hdd13|/dev/sdd13)		BKGRUBROOT='(hd3,12)'		;;
/dev/hdd14|/dev/sdd14)		BKGRUBROOT='(hd3,13)'		;;
/dev/hdd15|/dev/sdd15)		BKGRUBROOT='(hd3,14)'		;;
/dev/hdd16|/dev/sdd16)		BKGRUBROOT='(hd3,15)'		;;
*)
	bkwarn "Eeep!  I do not know how to convert $BKROOTDEVICE into a GRUB partition."
	BKGRUBROOT="Please convert this by hand from $BKROOTDEVICE"
	;;
esac

cat >>$BKINSTALLROOT/boot/grub/menu.lst-$BKIMAGEVER <<EOTEXT
#The following boot section was added by buildkernel on
#`date`
#Please feel free to move this section, edit it, and remove these comments.
title=$BKIMAGEVER
root=$BKGRUBROOT
kernel=/boot/$BKBUILDTYPE-$BKIMAGEVER root=$BKROOTDEVICE
#Not sure if the boot line is necessary.
#boot
#End of autoinstall
EOTEXT
FILELIST="$FILELIST boot/grub/menu.lst-$BKIMAGEVER"

#FIXME genesis, syslinux, milo, linload, mattsoft boot manager.

if [ "$BKINSTALLHERE" = "YES" ]; then
	bkstatus setting up boot loader configuration files on the local machine.
	if [ -w /etc/lilo.conf ]; then
		cat $BKINSTALLROOT/etc/lilo.conf-$BKIMAGEVER >>/etc/lilo.conf 2>&1 | tee -a /tmp/BKlog.$$
	fi
	if [ -w /etc/silo.conf ]; then
		cat $BKINSTALLROOT/etc/silo.conf-$BKIMAGEVER >>/etc/silo.conf 2>&1 | tee -a /tmp/BKlog.$$
	fi
	if [ -w /boot/grub/menu.lst ]; then
		cat $BKINSTALLROOT/boot/grub/menu.lst-$BKIMAGEVER >>/boot/grub/menu.lst 2>&1 | tee -a /tmp/BKlog.$$
	fi

	LoaderImageCount=0
	if [ -e "$LoaderConfigFile" ]; then
		LoaderImageCount=`cat $LoaderConfigFile | egrep -i '(^image|^title)' | wc -l | sed -e s/\ //g`
	fi
	if [ "$BKINTERACTIVE" = "YES" ]; then
		bkstatus The build process has added a section to $LoaderConfigFile that will boot the kernel that was just built. Please feel free to move or edit this section. Once you have made any changes, please save and exit.
		if [ $LoaderImageCount -gt 13 ]; then
			bkwarn Your $LoaderConfigFile is getting close to the limit of 16 image= sections. Please remove any that are no longer needed.
		else
			bkstatus $LoaderConfigFile is safely under 13 image lines.
		fi
		bkbeep
		if [ "$BKVERBOSE" != "NO" ]; then
			bkstatus Please press enter when ready to edit.
			read BKJUNK
		fi
		$EDITOR $LoaderConfigFile
	elif [ $LoaderImageCount -gt 16 ]; then
		bkwarn Your $LoaderConfigFile has reached the limit of 16 image= sections. Please remove any that are no longer needed. Any images beyond 16 cannot be booted.
	fi

	bkstatus Installing boot images
	case "$LOADER" in
	lilo|silo)
		$LOADER 2>&1 | tee -a /tmp/BKlog.$$
		;;
	grub)
		bkwarn My best understanding is that one does not need to run anything to register the new kernel with grub.  Please let me know if I\'m wrong.
		;;
	*)
		bkwarn What boot loader are you using \($LOADER, perhaps\)?  Are you using a different boot manager? The author of this script would like to know what boot system you are using so that he could find out how to register the new kernel with it. Please contact William Stearns at wstearns@pobox.com.
		;;
	esac
	unset LoaderImageCount
else
	bkstatus User has requested the kernel not be installed here so we\'re not installing into the boot loader.
fi


bkstatus Kernel and modules sucessfully installed. Kernel is in $BKINSTALLROOT/boot/$BKBUILDTYPE-$BKIMAGEVER


callkids POSTINSTALL


if [ "$BKMAKECLEANAFTER" = "YES" ]; then
	#-----------------------------------------------------------------------
	bkheader Cleaning out kernel build files.
	#-----------------------------------------------------------------------
	cd /usr/src/linux-$BKKERNELTOBUILD
	$BKLOAD make clean  2>&1 | tee -a /tmp/BKlog.$$
else
	bkstatus Current configuration chooses to not make clean.
fi


callkids POSTCLEAN


cd /usr/src
if [ "$BKPCMCIATOBUILD" != "NO" ]; then
	#-----------------------------------------------------------------------
	bkheader Building PCMCIA drivers.
	#-----------------------------------------------------------------------
	bkstatus Attempting version $BKPCMCIATOBUILD
	if [ -f ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz ]; then
		cd /usr/src
		if [ -d pcmcia-cs-$BKPCMCIATOBUILD ]; then
			if [ -f pcmcia-cs-$BKPCMCIATOBUILD/config.mk ]; then
				bkstatus Backing up config.mk
				cp -b -f --preserve pcmcia-cs-$BKPCMCIATOBUILD/config.mk configs/config.mk.$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$
			else
				bkstatus No config.mk to back up, no problem.
			fi
			if [ -f pcmcia-cs-$BKPCMCIATOBUILD/config.out ]; then
				bkstatus Backing up config.out
				cp -b -f --preserve pcmcia-cs-$BKPCMCIATOBUILD/config.out configs/config.out.$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$
			else
				bkstatus No config.out to back up, no problem.
			fi
#FIXME Remove once you are sure
#Unfortunately, pcmcia-cs does not seem to do well if we are compiling a 
#different kernel version than last time.  We toast it.
#if [ "$BKMAKECLEANBEFORE" = "YES" ]; then
			bkstatus Removing existing pcmcia-cs-$BKPCMCIATOBUILD directory.
			#This rm uses a complete path, so we don't need to check pwd.
			rm -rf /usr/src/pcmcia-cs-$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$
#else
#	bkstatus Not removing pcmcia-cs-$BKPCMCIATOBUILD because MAKECLEANBEFORE ne YES
#fi
		else
			bkstatus No existing pcmcia-cs-$BKPCMCIATOBUILD to remove.
		fi
		mkdir --parents pcmcia-cs-$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$


#FIXME Move up to download after kernel download
#FIXME Implement pcmcia-cs autodownload
#FIXME Check for existance of file w/ right size before autodownload
#elif [ "$BKAUTOGET" = "YES" ]; then
#		bkstatus Pcmcia-cs source does not exist here. We will go get it. Opening anonymous connection to hyper.stanford.edu . Attempting to download pcmcia-cs-$BKPCMCIATOBUILD.tar.gz from pub/pcmcia
#
#		cd $BKSOURCEDIR
#		bkgetfile ftp hyper.stanford.edu anonymous "$USER@$HOSTNAME" "pub/pcmcia" "linux-$BKPCMCIATOBUILD.tar.gz"
#
#		if [ -f ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz ]; then
#			bkstatus ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz successfully downloaded.
#		else
#			bkwarn ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz was not successfully downloaded. Is your Internet connection down? Out of disk space? hyper.stanford.edu overloaded?
#			bkbeep
#			exit
#		fi

#FIXME check BKOPENFRESH
		#-----------------------------------------------------------------------
		bkheader Opening pcmcia-cs-$BKPCMCIATOBUILD
		#-----------------------------------------------------------------------
		cd /usr/src
		$BKLOAD tar -xzf ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz  2>&1 | tee -a /tmp/BKlog.$$

		if [ ! -f /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/Makefile ]; then
			bkwarn Hmmm... there does not seem to be a Makefile in /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/. That would seem to indicate that the source files are incomplete or missing. Please check on this and run $BKORIGPARAMS again.
#FIXME onetime: set BKOPENFRESH=YES, tell user.
			bkbeep
			exit
		fi

		#-----------------------------------------------------------------------
		bkheader Restoring and setting configuration
		#-----------------------------------------------------------------------
		cd /usr/src
		if [ -f configs/config.mk.$BKPCMCIATOBUILD ]; then
			bkstatus Restoring config.mk
			cp --preserve configs/config.mk.$BKPCMCIATOBUILD pcmcia-cs-$BKPCMCIATOBUILD/config.mk 2>&1 | tee -a /tmp/BKlog.$$
		else
			bkstatus No config.mk to restore, no problem.
		fi
		if [ -f configs/config.out.$BKPCMCIATOBUILD ]; then
			bkstatus Restoring config.out
			cp --preserve configs/config.out.$BKPCMCIATOBUILD pcmcia-cs-$BKPCMCIATOBUILD/config.out 2>&1 | tee -a /tmp/BKlog.$$
		else
			bkstatus No config.out to restore, no problem.
		fi
		cd /usr/src/pcmcia-cs-$BKPCMCIATOBUILD
		if [ "$BKINTERACTIVE" = "YES" ]; then
			#We replaced "make config" with the actual lines from make config so we can force ./Configure options.
			#bkstatus When asked 
			#bkstatus How would you like to set kernel-specific options?
			#bkstatus please answer 
			#bkstatus Read from the Linux source tree.
			#bkbeep
			#make config
			touch config.mk
			make -s clean
			./Configure "--target=/$BKINSTALLROOT" --kernel=/usr/src/linux --srctree
		fi

		cd /usr/src/pcmcia-cs-$BKPCMCIATOBUILD
#FIXME - pcmcia config to BKINSTALLROOT as well?
		if [ -f config.mk ]; then
			cp -b -f --preserve config.mk ../configs/config.mk.$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$
		fi
		if [ -f config.out ]; then
			cp -b -f --preserve config.out ../configs/config.out.$BKPCMCIATOBUILD 2>&1 | tee -a /tmp/BKlog.$$
		fi
		bkstatus Saving the pcmcia config.mk to the log...
		cat /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/config.mk 2>&1 >>/tmp/BKlog.$$
		bkstatus Saving the pcmcia config.out to the log...
		cat /usr/src/pcmcia-cs-$BKPCMCIATOBUILD/config.out 2>&1 >>/tmp/BKlog.$$
		bkstatus


		#-----------------------------------------------------------------------
		bkheader Building PCMCIA modules
		#-----------------------------------------------------------------------
		cd /usr/src/pcmcia-cs-$BKPCMCIATOBUILD
		BKTIMEBASE=$SECONDS
		$BKLOAD make -k all MAKE="make -j $BKNUMBUILDS"  2>&1 | tee -a /tmp/BKlog.$$
		bkstatus Compiling the pcmcia-cs package took $[$SECONDS-$BKTIMEBASE] seconds.
#The target dir should have been set with the --target param above.
		make install 2>&1 | tee -a /tmp/BKlog.$$

		cd $BKINSTALLROOT
		for OneFile in usr/X11R6/bin/cardinfo sbin/cardmgr sbin/cardctl sbin/ifport \
		 sbin/ifuser sbin/scsi_info sbin/ide_info sbin/pcinitrd sbin/ftl_format \
		 sbin/ftl_check sbin/dump_cis sbin/pack_cis sbin/lspnp sbin/setpnp \
		 usr/share/pnp.ids usr/man/man4/*.4 usr/man/man5/*.5 usr/man/man8/*.8 \
		 etc/pcmcia/* etc/pcmcia/cis/*.dat \
		 lib/modules/$BKKERNELTOBUILD$EXTRAVERSION/pcmcia/* ; do
			if [ -f "$OneFile" ]; then
				FILELIST="$FILELIST $OneFile"
			fi
		done
	else
		bkwarn ${BKSOURCEDIR}pcmcia-cs-$BKPCMCIATOBUILD.tar.gz not found. Not installing Pcmcia drivers.
	fi
fi


#FIXME move up into above section?
if [ "$BKMAKECLEANAFTER" = "YES" ]; then 
	if [ -d /usr/src/pcmcia-cs-$BKPCMCIATOBUILD ]; then
		#-----------------------------------------------------------------------
		bkheader Cleaning out pcmcia-cs build files.
		#-----------------------------------------------------------------------
		cd /usr/src/pcmcia-cs-$BKPCMCIATOBUILD
		$BKLOAD make clean  2>&1 | tee -a /tmp/BKlog.$$
	else
		bkstatus no /usr/src/pcmcia-cs-$BKPCMCIATOBUILD to clean out
	fi
else
	bkstatus Current configuration chooses to not make clean.
fi


#FIXME should we run depmod at all?  It always seems to barf "unresolved 
#dependencies.
cd /usr/src

eval `cat /usr/src/linux-$BKKERNELTOBUILD/.config | \
 grep 'CONFIG_MODULES' | tr a-z A-Z`
if [ "$CONFIG_MODULES" = "Y" ]; then
	#-----------------------------------------------------------------------
	bkheader Checking module dependencies
	#-----------------------------------------------------------------------
	depmod -a $BKKERNELTOBUILD 2>&1 | tee -a /tmp/BKlog.$$
	bkstatus If you had modules for this kernel version on the system before this build, do not be too concerned if Unresolved Dependencies showed up just above. If your system does not run 
	bkstatus depmod -a
	bkstatus at boot, it may be necessary to run this once when you reboot into the new kernel.
else
	bkstatus No modules, no dependencies to check.
fi

touch $BKINSTALLROOT/boot/bkinstall-$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
chmod 755 $BKINSTALLROOT/boot/bkinstall-$BKIMAGEVER 2>&1 | tee -a /tmp/BKlog.$$
cat >>$BKINSTALLROOT/boot/bkinstall-$BKIMAGEVER <<EOTEXT
if [ "$BKROOTDEVICE" != `cat /etc/mtab | grep ' / ' | cut -f 1 -d ' '` ]; then
	echo WARNING.  You must change the root filesystem specification by hand when you get into the editor.
	echo Change the \"root=\" to use \"`cat /etc/mtab | grep ' / ' | cut -f 1 -d ' '`\" if
	echo using lilo or silo, or the appropriate boot device for grub.
	sleep 20
fi
if [ -w /etc/lilo.conf ]; then
	cat /etc/lilo.conf-$BKIMAGEVER >>/etc/lilo.conf
	\${EDITOR:-vi} /etc/lilo.conf
	lilo
fi
if [ -w /etc/silo.conf ]; then
	cat /etc/silo.conf-$BKIMAGEVER >>/etc/silo.conf
	\${EDITOR:-vi} /etc/silo.conf
	silo
fi
if [ -w /boot/grub/menu.lst ]; then
	cat /boot/grub/menu.lst-$BKIMAGEVER >>/boot/grub/menu.lst
	\${EDITOR:-vi} /boot/grub/menu.lst
	#No need to run grub
fi
if type -path depmod >/dev/null; then
	depmod -a
else
	echo Missing depmod, cannot make module dependencies.
fi
#handle case of different root device on target system.
if type -path rdev >/dev/null; then
	echo Forcing root device
	rdev /boot/$BKBUILDTYPE-$BKIMAGEVER `cat /etc/mtab | grep ' / ' | cut -f 1 -d ' '`
	echo Telling kernel to mount root read-only
	rdev -R /boot/$BKBUILDTYPE-$BKIMAGEVER 1
else
	echo No rdev to set root filesystem or read-only mode, probably not a problem.
fi
EOTEXT
FILELIST="$FILELIST boot/bkinstall-$BKIMAGEVER"


if [ "$BKINSTALLTAR" = "YES" ]; then
	cd $BKINSTALLROOT
	tar -czvf /boot/linux-$BKIMAGEVER.tar.gz $FILELIST 2>&1 | tee -a /tmp/BKlog.$$
	bkstatus A tar file with all the needed kernel files has been placed in /boot/linux-$BKIMAGEVER.tar.gz .  You should send this file over to the target system and run the following commands:
	bkstatus
	bkstatus cd /\<Enter\>
	bkstatus tar -xzvf /path/to/linux-$BKIMAGEVER.tar.gz\<Enter\>
	bkstatus /boot/bkinstall-$BKIMAGEVER\<Enter\>
	bkstatus
	bkstatus Finally, when it\'s convenient for the target system you should reboot it.  \"shutdown -r now\" will reboot it immediately.  See the man page for shutdown for other forms.
fi

#FIXME rpm version of the above.

#-----------------------------------------------------------------------
bkheader Log information about the build
#-----------------------------------------------------------------------
bkstatus Directory of /boot:
ls -al /boot  2>&1 | tee -a /tmp/BKlog.$$
if [ "$BKINSTALLROOT" != "/" ]; then
	bkstatus Directory of $BKINSTALLROOT:
	ls -AlR $BKINSTALLROOT  2>&1 | tee -a /tmp/BKlog.$$
fi
bkstatus
bkstatus Directory of /usr/src/linux/arch/$HOSTTYPE/boot/
ls -al /usr/src/linux-$BKKERNELTOBUILD/arch/$HOSTTYPE/boot/ 2>&1 | tee -a /tmp/BKlog.$$
bkstatus


#-----------------------------------------------------------------------
bkheader Cleanup...
#-----------------------------------------------------------------------
cd /usr/src
BKBUILDCOUNT=$[$BKBUILDCOUNT+1]


if [ -f ~/.bkrc.tmp ] ; then
	#This rm uses a (semi) complete path, so we don't need to check pwd.
	rm -f ~/.bkrc.tmp
fi

bksplitversion $BKKERNELTOBUILD
case $VERSION.$PATCHLEVEL in
1.0)
	BKLASTBUILT10=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT10' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT10' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
1.1)
	BKLASTBUILT11=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT11' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT11' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
1.2)
	BKLASTBUILT12=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT12' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT12' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
1.3)
	BKLASTBUILT13=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT13' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT13' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.0)
	BKLASTBUILT20=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT20' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT20' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.1)
	BKLASTBUILT21=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT21' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT21' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.2)
	BKLASTBUILT22=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT22' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT22' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.3)
	BKLASTBUILT23=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT23' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT23' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.4)
	BKLASTBUILT24=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT24' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT24' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.5)
	BKLASTBUILT25=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT25' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT25' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.6)
	BKLASTBUILT26=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT26' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT26' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
2.7)
	BKLASTBUILT27=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT27' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT27' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
3.0)
	BKLASTBUILT30=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT30' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT30' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
3.1)
	BKLASTBUILT31=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT31' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT31' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
3.2)
	BKLASTBUILT32=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT32' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT32' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
3.3)
	BKLASTBUILT33=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT33' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT33' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
4.0)
	BKLASTBUILT40=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT40' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT40' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
4.1)
	BKLASTBUILT41=$BKKERNELTOBUILD
	cat ~/.bkrc | grep -v -E '^BKBUILDCOUNT|^BKLASTBUILT41' >~/.bkrc.tmp
	set | grep -E '^BKBUILDCOUNT|^BKLASTBUILT41' >>~/.bkrc.tmp
	cat ~/.bkrc.tmp >~/.bkrc ; rm -f ~/.bkrc.tmp
	;;
*)
	bkwarn I am not aware of the kernel series $VERSION.$PATCHLEVEL . Please get a new version of the buildkernel script at http://www.pobox.com/~wstearns/buildkernel/
	;;
esac


bkbeep
bkstatus All done with kernel build number $BKBUILDCOUNT with $0! You may wish to look through the logfile to see if any errors showed up during the build. If all went well, you should be able to reboot into your new kernel when it is convenient to reboot the machine. If you have problems, see the documentation in /usr/share/doc/buildkernel-$BKSCRIPTVER, the buildkernel homepage at http://www.pobox.com/~wstearns/buildkernel/ , or the kernel howto at http://metalab.unc.edu/linux/HOWTO/  .

if [ "$BKINSTALLTAR" = "YES" ] || [ "$BKINSTALLRPM" = "YES" ]; then
	bkstatus You may wish to remove the temporary files with the following:
	bkstatus
	bkstatus cd $BKINSTALLROOT\<Enter\>
	bkstatus rm $FILELIST\<Enter\>
	bkstatus
fi

#These rm's use complete paths, so we don't need to check pwd.
rm -f /tmp/BKfingerkernel  2>&1 | tee -a /tmp/BKlog.$$
rm -f /tmp/BKrpmqa  2>&1 | tee -a /tmp/BKlog.$$

cp -b -f --preserve /tmp/BKlog.$$ /usr/src/configs/bklog.$BKKERNELTOBUILD
if [ "$BKLOGFILE" = "/tmp/BKlog.$$" ]; then
	bkstatus The build log can be found at /tmp/BKlog.$$ and at /usr/src/configs/bklog.$BKKERNELTOBUILD
else
	bkstatus The build log can be found at $BKLOGFILE and at /usr/src/configs/bklog.$BKKERNELTOBUILD
	mv -bf /tmp/BKlog.$$ $BKLOGFILE
fi


# Do not use bkstatus, bkwarn or bkheader after this point.  The log file 
# has been moved or copied.  If you need to pass anything to the 
# user, use "echo ..." instead.

# No need to unset variables; bash does this for us unless we're source'ing
# this script, which there's no reason to be doing.

#Variable summary
#User option variables
# BKAUTOGET BKBUILDTYPE BKCOPYTOFLOPPY BKDEBUG BKFTPSITE BKINTERACTIVE BKKERNELTOBUILD BKPCMCIATOBUILD BKLOAD BKLOGFILE BKMAKECLEANBEFORE BKMAKECLEANAFTER BKMAKETYPE BKNUMBUILDS BKOPENFRESH BKROOTDEVICE BKSOURCEDIR BKVERBOSE BKVIDMODE
#Not implemented yet
# BKINSTALLHERE BKINSTALLRPM BKINSTALLTAR
#Internal variables
# BKBUILDSUCCEEDED BKCHECKLINK BKCHECKPATCH BKFREESPACE BKGETEXT BKIMAGEVER BKJUNK BKKERNELTYPE BKLASTBUILT BKLEGALVER BKNEWVER BKORIGPARAMS BKSCRIPTVER BKTIMEBASE
#Internal variables that should have been cleared right after use, but 
#just in case...
#FIXME Move some more of the above down here.
# BKARCHI BKKERNELVERSION LoaderImageCount BKMAKEDIR BKPATCH BKPATCHESAPPLIED BKSHOWHELP BKSHOWVER BKTRYEDITOR BKVALUE
#Persistent internal variables, stored in ~/.bkrc
# BKBUILDCOUNT BKLASTBUILT10 BKLASTBUILT11 BKLASTBUILT12 BKLASTBUILT13 BKLASTBUILT20 BKLASTBUILT21 BKLASTBUILT22 BKLASTBUILT23 BKLASTBUILT24 BKLASTBUILT25 BKLASTBUILT26 BKLASTBUILT27 BKLASTBUILT30 BKLASTBUILT31 BKLASTBUILT32 BKLASTBUILT33 BKLASTBUILT40 BKLASTBUILT41
#System variables
# Do not unset EDITOR, HOSTNAME, HOSTTYPE, or USER; I believe they are 
# created by the OS
# Kernel source tree variables, internal
# CONFIG_MODULES PATCHLEVEL SUBLEVEL VERSION
