#! /bin/sh
#
# This script is used to configure the linux kernel.
#
# It was inspired by a desire to not have to hit <enter> 9 million times
# or startup the X server just to change a single kernel parameter.  
#
# This script attempts to parse the configuration files, which are
# scattered throughout the kernel source tree, and creates a temporary
# set of mini scripts which are in turn used to create nested menus and
# radiolists.
#
# It uses a very modified/mutilated version of the "dialog" utility
# written by Savio Lam (lam836@cs.cuhk.hk). Savio is not responsible
# for this script or the version of dialog used by this script.
# Please do not contact him with questions. The official version of 
# dialog is available at sunsite.unc.edu or a sunsite mirror.
#
# Portions of this script were borrowed from the original Configure
# script.
#
# Please send comments / questions / bug fixes to roadcapw@titus.org
#
#----------------------------------------------------------------------------

#
# Make sure we're really running bash.
#
[ -z "$BASH" ] && { echo "Menuconfig requires bash" 1>&2; exit 1; }

#
# Cache function definitions, turn off posix compliance
#
set -h +o posix


#
# If you prefer all kernel options listed in a single menu rather than
# the standard menu hierarchy, set SINGLE_MENU_MODE to "TRUE" in your
# environment.
#
single_menu_mode="${SINGLE_MENU_MODE:-FALSE}"


#
# Load the functions used by the config.in files.
#
# I do this because these functions must be redefined depending
# on whether they are being called for interactive use or for
# saving a configuration to a file.
#
# Thank the heavens bash supports nesting function definitions.
#
load_functions () {

#
# Additional comments
#
function comment () {
	comment_ctr=$[ comment_ctr + 1 ]
	echo -ne "': $comment_ctr' '--- $1' " >>MCmenu
}

#
# Don't need this yet, but we don't want to puke either.
#
function define_bool () {
	:	
}

#
# Create a boolean (Yes/No) function for our current menu
# which calls our local bool function.
#
function bool () {
	eval $2=\${$2:-'n'}  x=\$$2

	case $x in
	y|m)	flag="*" ;;
	n)	flag=" " ;;
	esac

	echo -ne "'$2' '[$flag] $1' " >>MCmenu

	echo -e "function $2 () { l_bool '$2' \"\$1\" ;}\n" >>MCradiolists
}

#
# Create a tristate (Yes/No/Module) radiolist function
# which calls our local tristate function.
#
# Collapses to a boolean (Yes/No) if module support is disabled.
#
function tristate () {
	if [ "$CONFIG_MODULES" != "y" ]
	then
		bool "$1" "$2"
	else
		eval $2=\${$2:-'n'}  x=\$$2
	
		case $x in
		y) flag="*" ;;
		m) flag="M" ;;
		*) flag=" " ;;
		esac
	
		echo -ne "'$2' '<$flag> $1' " >>MCmenu
	
		echo -e "
		function $2 () { l_tristate '$2' \"\$1\" ;}" >>MCradiolists
	fi
}

#
# Create a tristate radiolist function which is dependent on
# another kernel configuration option.
#
# Quote from the original configure script:
#
#       If the option we depend upon is a module,
#       then the only allowable options are M or N.  If Y, then
#       this is a normal tristate.  This is used in cases where modules
#       are nested, and one module requires the presence of something
#       else in the kernel.
#
function dep_tristate () {
	if [ "$CONFIG_MODULES" != "y" ]
	then
		bool "$1" "$2"
	else
		if  eval [ "_$3" != "_m" ]
		then
			tristate "$1" "$2" $3
		else
			mod_bool "$1" "$2"
		fi
	fi
}

#
# Add a menu item which will call our local int function.
# 
function int () {
	eval $2=\${$2:-"$3"} x=\$$2

	echo -ne "'$2' '($x) $1' " >>MCmenu

	echo -e "function $2 () { l_int '$1' '$2' '$3' '$x' ;}" >>MCradiolists
}

#
# Add a menu item which will call our local int function.
# 
function hex () {
	eval $2=\${$2:-"$3"} x=\${$2##*[x,X]}

	echo -ne "'$2' '($x) $1' " >>MCmenu

	echo -e "function $2 () { l_hex '$1' '$2' '$3' '$x' ;}" >>MCradiolists
}

#
# Add a menu item which will call our local One-of-Many choice list.
#
function choice () {
	#
	# Need to remember params cause they're gonna get reset.
	#
	title=$1
	choices=$2
	default=$3
	current=

	#
	# Find out if one of the choices is already set.
	# If it's not then make it the default.
	#
	set -- $choices
	firstchoice=$2

	while [ -n "$2" ]
	do
		if eval [ "_\$$2" = "_y" ]
		then
			current=$1
			break
		fi
		shift ; shift
	done

	: ${current:=$default}

	echo -ne "'$firstchoice' '($current) $title' " >>MCmenu

	echo -e "
	function $firstchoice () \
		{ l_choice '$title' \"$choices\" $current ;}" >>MCradiolists
}

} # END load_functions()





#
# Extract available help for an option from Configure.help
# and send it to standard output.
#
# Most of this function was borrowed from the original kernel
# Configure script.
#
function extract_help () {
  if [ -f Documentation/Configure.help ]
  then
     #first escape regexp special characters in the argument:
     var=$(echo "$1"|sed 's/[][\/.^$*]/\\&/g')
     #now pick out the right help text:
     text=$(sed -n "/^$var[ 	]*\$/,\${
                        /^$var[ 	]*\$/d
                        /^#.*/d
			/^[ 	]*\$/q
                        s/^  //
                        p
                    }" Documentation/Configure.help)

     if [ -z "$text" ]
     then
          echo "There is no help available for this kernel option."
	  return 1
     else
	  echo "$text"
     fi
  else
	 echo "There is no help available for this kernel option."
         return 1
  fi
}

#
# Activate a help dialog.
#
function help () {
	if extract_help $1 >help.out
	then
		$DIALOG	--backtitle "$backtitle" --title "$2"\
			--textbox help.out $ROWS $COLS
	else
		$DIALOG	--backtitle "$backtitle" \
			--textbox help.out $ROWS $COLS
	fi
	rm help.out
}

#
# Show the README file.
#
function show_readme () {
	$DIALOG --backtitle "$backtitle" \
		--textbox scripts/README.Menuconfig $ROWS $COLS
}

#
# Begin building the dialog menu command and Initialize the 
# Radiolist function file.
#
function menu_name () {
	echo -ne "$DIALOG --title '$1'\
			--backtitle '$backtitle' \
			--menu '$menu_instructions' \
			$ROWS $COLS $((ROWS-10)) \
			'$default' " >MCmenu
	>MCradiolists
}

#
# Add a submenu option to the menu currently under construction.
#
function submenu () {
	echo -ne "'activate_menu $2' '$1  --->' " >>MCmenu
}

#
# Create a menu entry to handle the traditional sound configuration.
#
function soundcfg () {
	echo -ne "'l_soundcfg' "\
		 "'Old configuration script "\
		 "(For: SM Wave, PSS & AudioTrix Pro) -->' " >>MCmenu
}

#
# Startup the traditional sound configuration program.
#
function l_soundcfg () {
	clear
	$MAKE -C drivers/sound config
}

#
# Handle a boolean (Yes/No) option.
#
function l_bool () {
	if [ -n "$2" ]
	then
		case "$2" in
		y|m)	eval $1=y ;;
		c)	eval x=\$$1
		   	case $x in
		   	y) eval $1=n ;;
		   	n) eval $1=y ;;
		   	esac ;;
		*)	eval $1=n ;;
		esac
	else
		echo -ne "\007"
	fi
}

#
# Same as bool() except options are (Module/No)
#
function mod_bool () {
	eval $2=\${$2:-'n'}  x=\$$2

	case $x in
	y|m) flag='M' ;;
	*)   flag=' ' ;;
	esac

	echo -ne "'$2' '<$flag> $1' " >>MCmenu

	echo -e "function $2 () { l_mod_bool '$2' \"\$1\" ;}" >>MCradiolists
}

#
# Same as l_bool() except options are (Module/No)
#
function l_mod_bool() {
	if [ -n "$2" ]
	then
		case "$2" in
		y)	echo -en "\007"
			${DIALOG} --backtitle "$backtitle" \
				  --infobox "\
This feature depends on another which has been configured as a module.  \
As a result, this feature will be built as a module." 4 70
			sleep 5
			eval $1=m ;;
		m)	eval $1=m ;;
		c)	eval x=\$$1
			case $x in
			m) eval $1=n ;;
			n) eval $1=m ;;
			esac ;;
		*)	eval $1=n ;;
		esac
	else
		echo -ne "\007"
	fi
}

#
# Handle a tristate (Yes/No/Module) option.
#
function l_tristate () {
	if [ -n "$2" ]
	then
		eval x=\$$1

		case "$2" in
		y) eval $1=y ;;
		m) eval $1=m ;;
		c) eval x=\$$1
		   case $x in
		   y) eval $1=n ;;
		   n) eval $1=m ;;
		   m) eval $1=y ;;
		   esac ;;
		*) eval $1=n ;;
		esac
	else
		echo -ne "\007"
	fi
}

#
# Create a dialog for entering an integer into a kernel option.
#
function l_int () {
	while true
	do
		if $DIALOG --title "$1" \
			--backtitle "$backtitle" \
			--inputbox "$inputbox_instructions_int" \
			10 75 "$4" 2>MCdialog.out
		then
			answer="`cat MCdialog.out`"
			answer="${answer:-$3}"

			# Avoid problems with GNU vs POSIX expr semantics.
			if expr "$answer" : '0$\|-[1-9][0-9]*$\|[1-9][0-9]*$' >/dev/null
			then
				eval $2="$answer"
			else
				eval $2="$3"
				echo -en "\007"
				${DIALOG} --backtitle "$backtitle" \
					--infobox "You have made an invalid entry." 3 43
				sleep 2
			fi

			break
		fi

		help "$2" "$1"
	done
}

#
# Create a dialog for entering a hexadecimal into a kernel option.
#
function l_hex () {
	while true
	do
		if $DIALOG --title "$1" \
			--backtitle "$backtitle" \
			--inputbox "$inputbox_instructions_hex" \
			10 75 "$4" 2>MCdialog.out
		then
			answer="`cat MCdialog.out`"
			answer="${answer:-$3}"
			answer="${answer##*[x,X]}"

			# Avoid problems with GNU vs POSIX expr semantics.
			if expr "$answer" : '[0-9a-fA-F][0-9a-fA-F]*$' >/dev/null
			then
				eval $2="$answer"
			else
				eval $2="$3"
				echo -en "\007"
				${DIALOG} --backtitle "$backtitle" \
					--infobox "You have made an invalid entry." 3 43
				sleep 2
			fi

			break
		fi

		help "$2" "$1"
	done
}

#
# Handle a one-of-many choice list.
#
function l_choice () {
	#
	# Need to remember params cause they're gonna get reset.
	#
	title="$1"
	choices="$2"
	current="$3"

	#
	# Scan current value of choices and set radiolist switches.
	#
	list=
	set -- $choices
	firstchoice=$2
	while [ -n "$2" ]
	do
		case "$1" in
		"$current")	list="$list $2 $1 ON "  ;;
		*)		list="$list $2 $1 OFF " ;;
		esac
			
		shift ; shift
	done

	while true
	do
		if $DIALOG --title "$title" \
			--backtitle "$backtitle" \
			--radiolist "$radiolist_instructions" \
			15 70 6 $list 2>MCdialog.out
		then
			choice=`cat MCdialog.out`
			break
		fi

		help "$firstchoice" "$title"
	done

	#
	# Now set the boolean value of each option based on
	# the selection made from the radiolist.
	#
	set -- $choices
	while [ -n "$2" ]
	do
		if [ "$2" = "$choice" ]
		then
			eval $2="y"
		else
			eval $2="n"
		fi
		
		shift ; shift
	done
}


#
# A faster awk based recursive parser. (I hope)
#
function parser1 () {
awk '
BEGIN {
	menu_no = 0
	comment_is_option = 0
	parser("'$CONFIG_IN'","MCmenu0")
}

function parser(ifile,menu) {

	while (getline <ifile) {
		if ($1 == "mainmenu_option") {
			comment_is_option = "1"
		}
		else if ($1 == "comment" && comment_is_option == "1") {
			comment_is_option= "0"
			sub($1,"",$0)
			++menu_no

			printf("submenu %s MCmenu%s\n", $0, menu_no) >>menu

			printf( "function MCmenu%s () {\n"\
				"default=$1\n"\
				"menu_name %s\n",\
				 menu_no, $0) >"MCmenu"menu_no

			parser(ifile, "MCmenu"menu_no)
		}
		else if ($1 ~ "endmenu") {
			printf("}\n") >>menu
			return
		} 
		else if ($0 ~ /^#|\$MAKE|mainmenu_name/) {
			printf("") >>menu
		}
		else if ($1 == "source") {
			# Yuk!  Blah!  Phooey!
			if ($2 ~ "drivers/sound") {
				printf("soundcfg\n") >>menu
			}

			parser($2,menu)
		}
		else {
			print >>menu
		}
	}
}'
}

#
# Secondary parser for single menu mode.
#
function parser2 () {
awk '
BEGIN {
	parser("'$CONFIG_IN'","MCmenu0")
}

function parser(ifile,menu) {

	while (getline <ifile) {
		if ($1 ~ /mainmenu_option|endmenu/) {
			printf("") >>menu
		} 
		else if ($0 ~ /^#|\$MAKE|mainmenu_name/) {
			printf("") >>menu
		}
		else if ($1 == "source") {
			if ($2 ~ "drivers/sound") {
				printf("soundcfg\n") >>menu
			}
			parser($2,menu)
		}
		else {
			print >>menu
		}
	}
}'
}

#
# Parse all the config.in files into mini scripts.
#
function parse_config_files () {
	rm -f MCmenu*

	echo "function MCmenu0 () {" >MCmenu0
	echo 'default=$1' >>MCmenu0
	echo "menu_name 'Main Menu'" >>MCmenu0

	if [ "_$single_menu_mode" = "_TRUE" ]
	then
		parser2
	else
		parser1
	fi

	echo "comment ''"	>>MCmenu0
	echo "g_alt_config" 	>>MCmenu0
	echo "s_alt_config" 	>>MCmenu0

	echo "}" >>MCmenu0

	#
	# These mini scripts must be sourced into the current
	# environment in order for all of this to work.  Leaving
	# them on the disk as executables screws up the recursion
	# in activate_menu(), among other things.  Once they are
	# sourced we can discard them.
	#
	for i in MCmenu*
	do
		source ./$i
	done

	rm -f MCmenu*
}

#
# This is the menu tree's bootstrap.
#
# Executes the parsed menus on demand and creates a set of functions,
# one per configuration option.  These functions will in turn execute
# dialog commands or recursively call other menus.
#
function activate_menu () {
	while true
	do
		comment_ctr=0		#So comment lines get unique tags

		$1 "$default"		#Create the lxdialog menu & functions

		if [ "$?" != "0" ]
		then
			clear
			cat <<EOM

Menuconfig has encountered a possible error in one of the kernel's
configuration files and is unable to continue.

Please report this to the author <roadcapw@titus.org>.  You may also
send a problem report to linux-kernel@vger.rutgers.edu or post a
message to the linux.dev.kernel news group.

Please indicate the kernel version you are trying to configure and
which menu you were trying to enter when this error occurred.

EOM
			cleanup
			exit 1
		fi

		. ./MCradiolists		#Source the menu's functions

		. ./MCmenu 2>MCdialog.out	#Activate the lxdialog menu
		ret=$?

		read selection <MCdialog.out

		case "$ret" in
		0|3|4|5|6)
			defaults="$selection$defaults"  #pseudo stack
			case "$ret" in
			0) eval $selection   ;;
			3) eval $selection y ;;
			4) eval $selection n ;;
			5) eval $selection m ;;
			6) eval $selection c ;;
			esac
			default="${defaults%%*}" defaults="${defaults#*}"
			;;
		2)	
			default="${selection%%\ *}"

			case "$selection" in
			*"-->"*|*"alt_config"*)
				show_readme ;;
			*)
				eval help $selection ;;
			esac
			;;
		255|1)
			break
			;;
		139)
			stty sane
			clear
			cat <<EOM

There seems to be a problem with the lxdialog companion utility which is
built prior to running Menuconfig.  Usually this is an indicator that you
have upgraded/downgraded your ncurses libraries and did not remove the 
old ncurses header file(s) in /usr/include or /usr/include/ncurses.

It is VERY important that you have only one set of ncurses header files
and that those files are properly version matched to the ncurses libraries 
installed on your machine.

You may also need to rebuild lxdialog.  This can be done by moving to
the /usr/src/linux/scripts/lxdialog directory and issuing the 
"make clean all" command.

If you have verified that your ncurses install is correct, you may email
the author <roadcapw@titus.org> or post a message on the linux.dev.kernel
news group for additional assistance. 

EOM
			cleanup
			exit 139
			;;
		esac
	done
}

#
# Create a menu item to load an alternate configuration file.
#
g_alt_config () {
	echo -n "get_alt_config 'Load an Alternate Configuration File' "\
		>>MCmenu
}

#
# Get alternate config file name and load the 
# configuration from it.
#
get_alt_config () {
	set -f ## Switch file expansion OFF

	while true
	do
		ALT_CONFIG="${ALT_CONFIG:-$_CONFIG}"

		$DIALOG --backtitle "$backtitle" \
			--inputbox "\
Enter the name of the configuration file you wish to load.  \
Accept the name shown to restore the configuration you \
last retrieved.  Leave blank to abort."\
			11 55 "$ALT_CONFIG" 2>MCdialog.out

		if [ "$?" = "0" ]
		then
			ALT_CONFIG=`cat MCdialog.out`

			[ "_" = "_$ALT_CONFIG" ] && break

			if eval [ -r "$ALT_CONFIG" ]
			then
				eval load_config_file "$ALT_CONFIG"
				break
			else
				echo -ne "\007"
				$DIALOG	--backtitle "$backtitle" \
					--infobox "File does not exist!"  3 38
				sleep 2
			fi
		else
			cat <<EOM >help.out

For various reasons, one may wish to keep several different kernel
configurations available on a single machine.  

If you have saved a previous configuration in a file other than the
kernel's default, entering the name of the file here will allow you
to modify that configuration.

If you are uncertain, then you have probably never used alternate 
configuration files.  You should therefor leave this blank to abort.

EOM
			$DIALOG	--backtitle "$backtitle"\
				--title "Load Alternate Configuration"\
				--textbox help.out $ROWS $COLS
		fi
	done

	set +f ## Switch file expansion ON
	rm -f help.out MCdialog.out
}

#
# Create a menu item to store an alternate config file.
#
s_alt_config () {
	echo -n "save_alt_config 'Save Configuration to an Alternate File' "\
		 >>MCmenu
}

#
# Get an alternate config file name and save the current
# configuration to it.
#
save_alt_config () {
	set -f  ## Switch file expansion OFF
			
	while true
	do

		$DIALOG --backtitle "$backtitle" \
			--inputbox "\
Enter a filename to which this configuration should be saved \
as an alternate.  Leave blank to abort."\
			10 55 "$ALT_CONFIG" 2>MCdialog.out

		if [ "$?" = "0" ]
		then
			ALT_CONFIG=`cat MCdialog.out`

			[ "_" = "_$ALT_CONFIG" ] && break

			if eval touch $ALT_CONFIG 2>/dev/null
			then
				eval save_configuration $ALT_CONFIG
				load_functions  ## RELOAD
				break
			else
				echo -ne "\007"
				$DIALOG	--backtitle "$backtitle" \
					--infobox "Can't create file!  Probably a nonexistent directory." 3 60
				sleep 2
			fi
		else
			cat <<EOM >help.out

For various reasons, one may wish to keep different kernel
configurations available on a single machine.  

Entering a file name here will allow you to later retrieve, modify
and use the current configuration as an alternate to whatever 
configuration options you have selected at that time.

If you are uncertain what all this means then you should probably
leave this blank.
EOM
			$DIALOG	--backtitle "$backtitle"\
				--title "Save Alternate Configuration"\
				--textbox help.out $ROWS $COLS
		fi
	done

	set +f  ## Switch file expansion ON
	rm -f help.out MCdialog.out
}


#
# Load config file into the environment converting all
# "# OPTION is not set" lines to "OPTION=n".
#
# The $ARCH defaults are loaded first so "new"/previously 
# unconfigured parameters are assigned the proper defaults.
#
function load_config_file () {
	eval "`sed -e 's/# \(.*\) is not set.*/\1=n/' arch/$ARCH/defconfig $1`"
}


#
# Just what it says.
#
save_configuration () {
	${DIALOG} --backtitle "$backtitle" \
		  --infobox "Saving your kernel configuration..."  3 40

	#
	# Now, let's redefine the configuration functions for final
	# output to the config files.
	#
	# Nested function definitions, YIPEE!
	#
	function bool () {
		eval define_bool "$2" "\${$2:-n}"
	}

	function tristate () {
		eval define_bool "$2" "\${$2:-n}"
	}

	function dep_tristate () {
		eval x=\${$2:-n}

		if eval [ "_$3" = "_m" ]
		then
			if [ "$x" = "y" ]
			then
				x="m"
			fi
		fi

		define_bool "$2" "$x"
	}

	function int () {
		eval x=\${$2:-"$3"}
		echo "$2=$x" 		>>$CONFIG
		echo "#define $2 ($x)"	>>$CONFIG_H
	}

	function hex () {
		eval x=\${$2:-"$3"}
		echo "$2=$x" 			 >>$CONFIG
		echo "#define $2 0x${x##*[x,X]}" >>$CONFIG_H
	}

	function define_bool () {
		eval $1="$2"

   		case "$2" in
         	y)
                	echo "$1=y" 		>>$CONFIG
                	echo "#define $1 1"	>>$CONFIG_H
                	;;

         	m)
			if [ "$CONFIG_MODULES" = "y" ]
			then
                		echo "$1=m"		   >>$CONFIG
                		echo "#undef  $1"	   >>$CONFIG_H
                		echo "#define $1_MODULE 1" >>$CONFIG_H
			else
                		echo "$1=y" 		>>$CONFIG
                		echo "#define $1 1"	>>$CONFIG_H
			fi
                	;;

         	n)
			echo "# $1 is not set"	>>$CONFIG
                	echo "#undef  $1"	>>$CONFIG_H
                	;;
        	esac
	}

	function choice () {
		#
		# Find the first choice that's already set to 'y'
		#
		choices="$2"
		default="$3"
		current=

		set -- $choices
		while [ -n "$2" ]
		do
			if eval [ "_\$$2" = "_y" ]
			then
				current=$1
				break
			fi
			shift ; shift
		done

		#
		# Use the default if none were set.  
		#
		: ${current:=$default}

		#
		# Then extract the actual option from the list of choices.
		#
		current=${choices#*$current} ; set $current

		define_bool "$1" "y"
	}

	function mainmenu_name () {
		:
	}

	function mainmenu_option () {
		comment_is_option=TRUE
	}

	function endmenu () {
		:
	}

	function comment () {
		if [ "$comment_is_option" ]
		then
			comment_is_option=
			echo        >>$CONFIG
			echo "#"    >>$CONFIG
			echo "# $1" >>$CONFIG
			echo "#"    >>$CONFIG

			echo         >>$CONFIG_H
			echo "/*"    >>$CONFIG_H
			echo " * $1" >>$CONFIG_H
			echo " */"   >>$CONFIG_H
		fi
	}

	DEF_CONFIG="${1:-$_CONFIG}"
	DEF_CONFIG_H="$AUTOCONF_H"

	CONFIG=.tmpconfig
	CONFIG_H=.tmpconfig.h

	echo "#" >$CONFIG
	echo "# Automatically generated by make menuconfig: don't edit" >>$CONFIG
	echo "#" >>$CONFIG

	echo "/*" >$CONFIG_H
	echo " * Automatically generated by make menuconfig: don't edit" >>$CONFIG_H
	echo " */" >>$CONFIG_H

	MAKE=:	#To prevent sound Makefile from running.
	
	if . $CONFIG_IN >>.menuconfig.log 2>&1
	then
		#
		# Skip these steps if we are saving to an
		# alternate configuration file.
		#
		if [ "$DEF_CONFIG" = "$_CONFIG" ]
		then
			#
			# Create the sound driver's config files for cards
			# Which are compatible with the new config method.
			#
			if [ "_$CONFIG_TRIX"   != "_y" -a\
			     "_$CONFIG_PSS"    != "_y" -a\
			     "_$CONFIG_SMWAVE" != "_y"   ]
			then
				make -C drivers/sound kernelconfig >>.menuconfig.log 2>&1
			fi

			mv $CONFIG_H $DEF_CONFIG_H
		fi

		if [ -f "$DEF_CONFIG" ]
		then
			rm -f ${DEF_CONFIG}.old
			mv $DEF_CONFIG ${DEF_CONFIG}.old
		fi

		mv $CONFIG $DEF_CONFIG
			
		return 0
	else
		return 1
	fi
}


#
# Remove temporary files
#
cleanup () {
	cleanup1
	cleanup2
	stty $S_TERMIO
}

cleanup1 () {
	rm -f MCmenu* MCradiolists MCdialog.out help.out
}

cleanup2 () {
	rm -f .tmpconfig .tmpconfig.h
}

set_geometry () {
	# Some distributions export these with incorrect values
	# which can really screw up some ncurses programs.
	LINES=  COLUMNS=

	ROWS=${1:-24}  COLS=${2:-80} 

	# Just in case the nasty rlogin bug returns.
	#
	[ $ROWS = 0 ] && ROWS=24
	[ $COLS = 0 ] && COLS=80

	if [ $ROWS -lt 19 -o $COLS -lt 80 ]
	then
		echo -e "\n\007Your display is too small to run Menuconfig!"
		echo "It must be at least 19 lines by 80 columns."
		exit 0
	fi 

	ROWS=$((ROWS-4))  COLS=$((COLS-5))
}

S_TERMIO=`stty -g`

set_geometry `stty size 2>/dev/null`

menu_instructions="\
Arrow keys navigate the menu.  \
<Enter> selects submenus --->.  \
Highlighted letters are hotkeys.  \
Pressing <Y> includes, <N> excludes, <M> modularizes features.  \
Press <Esc><Esc> to exit, <?> for Help.  \
Legend: [*] built-in  [ ] excluded  <M> module  < > module capable"

radiolist_instructions="\
Use the arrow keys to navigate this window or \
press the hotkey of the item you wish to select \
followed by the <SPACE BAR>.
Press <?> for additional information about this option."

inputbox_instructions_int="\
Please enter a decimal value. \
Fractions will not be accepted.  \
Use the <TAB> key to move from the input field to the buttons below it."

inputbox_instructions_hex="\
Please enter a hexadecimal value. \
Use the <TAB> key to move from the input field to the buttons below it."

DIALOG="${DIALOG:-./scripts/lxdialog/lxdialog}"

kernel_version="${VERSION}.${PATCHLEVEL}.${SUBLEVEL}"

backtitle="Linux Kernel v$kernel_version Configuration"

trap "cleanup ; rm -f .menuconfig ; exit 1" 1 2 15


#
# Locate default files.
#
DEFAULTS="arch/$ARCH/defconfig"

CONFIG_IN="${1:-./config.in}"

_CONFIG="${2:-.config}"

if [ -f "$_CONFIG" ]; then
  DEFAULTS=$_CONFIG
fi

AUTOCONF_H="${3:-include/linux/autoconf.h}"

if [ -f $DEFAULTS ]
then
  echo
  echo "Using defaults found in" $DEFAULTS
  load_config_file $DEFAULTS
else
  echo
  echo "No defaults found"
fi

# Fresh new log.
>.menuconfig.log

$DIALOG	--backtitle "$backtitle" \
	--infobox "Preparing configuration scripts..." 3 40

#
# Check kernel version of previous menuconfig build.
# If it's different then we should tell the sound driver
# to rebuild its Config.in file.
#
rebuildsound=TRUE
if [ -e .menuconfig ]
then
	read x <.menuconfig
	if [ "$x" = "# $kernel_version" ]
	then
		rebuildsound=
	fi
fi

if [ "$rebuildsound" ]
then
	# Activate the Linux compatible sound configuration.
	# This may not work for all sound cards.  (See sound docs)
	#
	make -C drivers/sound mkscript kernelconfig >>.menuconfig.log 2>&1

	echo "# $kernel_version" >.menuconfig
fi

# Load the functions used by the config.in files.
load_functions

#
# Read config.in files and parse them into one shell function per menu.
#
parse_config_files $CONFIG_IN

#
# Start the ball rolling from the top.
#
activate_menu MCmenu0

#
# All done!
#
cleanup1

#
# Confirm and Save
#
if $DIALOG --backtitle "$backtitle" \
	   --yesno "Do you wish to save your new kernel configuration?" 5 60
	   
then
	save_configuration
	stty $S_TERMIO
	clear

	cat <<EOM


The linux kernel is now hopefully configured for your setup.
Check the top-level Makefile for additional configuration,
and do a 'make dep ; make clean' if you want to be sure all
the files are correctly re-made.

EOM
else
	clear
	stty $S_TERMIO
	echo -e "Your kernel configuration changes were NOT saved.\n"
fi


exit 0