#!/bin/bash
#ls1=`mktemp /tmp/kernelcompareorigXXXXXX`
#ls2=`mktemp /tmp/kernelcomparenewXXXXXX`
. /usr/lib/ctcs/lib/sh/ctcslib.sh

kconfig="yes ''"

if [ -z "$1" ] ; then
	makej=""
else
	makej="-j $1"
fi

if [ -z "$2" ] ; then
	dir=/tmp/linux
else
	dir="$2"
fi


function interrupt () {
	trap interrupt SIGUSR1
        trap interrupt SIGTERM
	trap interrupt SIGINT
#        echo $failed fail $succeed succeed $count count
	rm -f $ls1 $ls2
	rm -rf $dir
        # this just grabs the process id and all children.  and kills them
	# until they're dead
	x=1
	while [ $x -gt 0 ] ; do {
		kill_children --KILL
		x=$?
	} ; done
	# untrap the exit before we exit :)
        exit 255
}
trap interrupt SIGUSR1
trap interrupt SIGTERM
trap interrupt SIGINT

TRYLINUXDIR="/usr/src/linux /usr/src/linux-2.4 /usr/src/linux-2.6 /usr/src/linux-source-*"
for lds in $TRYLINUXDIR ; do {
        LINUXDIR=$lds
        if [ -d "$LINUXDIR" ] ; then {
                break;
        } ; fi
} ; done

if [ -d "$LINUXDIR" ]; then
	echo ]] init
	rm -rf $dir >&2
	sync
	mkdir -p $dir >&2
	cd $dir >&2
	cp -dpR $LINUXDIR/* . >&2
else
	# Let's try for a source tarball
	TARBALL=$(ls /usr/src/linux-{source-,}*.tar.* |head -n 1)
	if [ ! -z "$TARBALL" ]; then
		echo ]] init
		rm -rf $dir >&2
		sync
		mkdir -p $dir >&2
		cd $dir >&2
		tar xjf $TARBALL --strip 1 -C $dir >&2 || tar xzf $TARBALL --strip 1 -C $dir >&2
	fi
fi

if [ ! -f "$dir/arch/Kconfig" ] ; then {
	echo $dir
	echo test configuration error >&2
	exit 2
} ; fi

cd $dir

echo ]] cleanup
make -sw clean >> /dev/null 2>&1
make -sw mrproper >> /dev/null 2>&1

if [ -e ./makefile ] ; then {
	echo warning: I seem to have found a VA makefile.
	echo I am going to proceed as if there is one.  Errors
	echo here can be ignored.
	echo ]] make restore
	$kconfig | make -sw restore >> /dev/null 2>&1
#	echo ]] restore configuration
#	cp configs/kernel*i686.config .config
	rm -f ./makefile
} ; fi

if [ ! -s '.config' ] ; then {
	echo ]] make oldconfig
	# just in case.  Redirect 
	$kconfig | make -sw oldconfig >> /dev/null 2>&1
	if [ $? != 0 ] ; then exit 1 ; fi
} ; fi

echo ]] make dep
# make dep still displays commands despite -sw
make -sw dep >> /dev/null
if [ $? != 0 ] ; then exit 1 ; fi

echo ]] make clean
make -sw clean >> /dev/null
if [ $? != 0 ] ; then exit 1 ; fi

echo ]] make vmlinux
make -sw $makej vmlinux >> /dev/null
if [ $? != 0 ] ; then exit 1 ; fi

echo ]] make $makej modules
make -sw $makej modules >> /dev/null
if [ $? != 0 ] ; then exit 1 ; fi

rm -rf $dir >&2

exit 0
