#!/bin/sh

# kcs (c) 1995 by Joerg Hessdoerfer (joe@amber.dinoco.de)
# Permission to use and re-distribute this software is 
# granted according to the GPL


if [ ! `whoami` = "root" ]; then
	echo 'Only "root" may configure the kernel. Sorry.'
	exit 1
fi

trap 'clear; rm -rf /tmp/ConfDir' EXIT
trap 'clear; rm -rf /tmp/ConfDir' TERM
trap 'clear; rm -rf /tmp/ConfDir' INT

rm -rf /tmp/ConfDir
mkdir /tmp/ConfDir

export CONFTMP='/tmp/ConfDir/ConfTemp'

export CONFIGDIR=/usr/src/linux/kcs-dialog

BUILDFILES=$CONFIGDIR/buildfiles
RECONFIG=$CONFIGDIR/reconfig
ADDCONF=$CONFIGDIR/addconf
MAKENUMS=$CONFIGDIR/makenums
REQUIRE=$CONFIGDIR/require
WINSIZE=$CONFIGDIR/winsize

if [ ! -f $WINSIZE ]; then 
	if [ ! -f $WINSIZE.c ]; then
		echo 'no way to create "winsize"! Please re-install!'
		exit 1
	else
		echo 'Compiling "winsize" from "winsize.c"'
		cc -o $WINSIZE $WINSIZE.c
	fi
fi
		
export LINES=`$WINSIZE`


CONFIG=/usr/src/linux/arch/i386/config.kcs

if [ ! -f $CONFIG ]; then
	echo "No master config file found. Please run make config"
	echo "or re-install."
	exit 1
fi

$BUILDFILES $CONFIG

#
# RunDialog()
#
# Runs through ALL configuration phases
#
function RunDialog()
{
	if source $@ 2> $CONFTMP.nums; then
		$RECONFIG $@ > $@.tmp
		mv $@.tmp $@
		if [ -f $@.req ]; then
			$REQUIRE $@.req
			mv $CONFTMP.reqtmp $@.req
		fi
		return 0
	else
		return 1
	fi
}

#
# ConfigAll()
#
# Runs through ALL configuration phases
#
function ConfigAll () {
	for i in $CONFTMP.0??; do
		if ! RunDialog $i; then
			break
		fi
	done
}


#
# GenConfig()
#
# generates '.config' from config phases
#
function GenConfig () {
	if [ -f .config ]; then  mv -f .config .config.old; fi
	if [ -f .config.h ]; then  mv -f .config.h .config.h.old; fi

	echo "# Automagically generated config file! Don't dare editing!" >.config
	echo "/* Automagically generated config file! Don't dare editing! */" >.config.h

	for i in $CONFTMP.0*; do $ADDCONF $i; done

	cp -f .config.h ./include/linux/autoconf.h
}

#
# SetConfig()
#
# generates config phase defaults from '.config'
#
function SetConfig () {
	$MAKENUMS .config > $CONFTMP.nums

	for i in $CONFTMP.0*; do 
		$RECONFIG $i > $i.tmp
		mv $i.tmp $i
	done
}

#
# BuildKernel()
#
# generates config files, and compiles the kernel!
#
function BuildKernel () {
	GenConfig
	clear
	make dep
	make clean
	make zImage
	echo "Now that your kernel is recompiled, you can find"
	echo "the kernel image in ./arch/i386/boot/zImage"
	exit
}

if [ ! -f .config ]; then
	dialog --msgbox "No former configuration found. Running entire configuration" 8 50
 
	ConfigAll
else
	SetConfig
fi

while source $CONFTMP.menu 2> $CONFTMP.res; do
	case `cat $CONFTMP.res` in
	COF)
		ConfigAll;;
	BUI)
		GenConfig
		BuildKernel;;
	EXI)
		GenConfig
		exit 0;;
	END)
		exit 0;;
	0*)
		RunDialog $CONFTMP.`cat $CONFTMP.res`;;
	esac
done		



