# @configure_input@
#
# This script is used to configure the Undernet Daemon.
# It was copied from the linux kernel distribution and only slightly
# changed.
#
# 170897 (carlo@runaway.xs4all.nl) -
# Changed the default configuration file to be defconfig.
# Changed the location of autoconf.h to be 'include/'.
# Changed the final text printed at the end.
# Changed replacement to only match 'is not set' (and not 'is not').
# Added function string() and define_string().
# Added function define_macro()
#
# 180897 (carlo@runaway.xs4all.nl) -
# Changed bool() so it can take a default argument.
# Introduced hard link ../.config, to inherit config files over new
# distributions.
# Got rid of 'is not set' and using '=n' instead.
# Changed configuration help file to be doc/Configure.help.
# Changed DEFAULT into USE_DEFAULT, which is asked for in config.in.
#
# 210897 (carlo@runaway.xs4all.nl) -
# Changed the location of autoconf.h and .config to be 'configure/'.
# Fixed the script to work with a general 'sh' and not only with bash.
#	(removing 'function', using `` instead of $(), using \\$ inside
#	 that instead of \$, using 'eval set -- $choices' rather then
#	 'set -- $choices', introduction of function 'expr' instead of
#	 using external executable 'expr', using set -fh rather then
#	 set -f -h, using "" around reg.exp rather then '' and removing
#	 unnecessary backslashes from it).
# Removed tristate and hex functions.
# Making the hardlink to ../.config only when configure/.config doesn't exist.
#
# 280897 (carlo@runaway.xs4all.nl) -
# Added CONFIG_NEW: Ask if one wants to use this configuration as main
#	configuration.
# Changed /bin/rm into $RM, so now this environment variable must be set!
#
# 290897 (carlo@runaway.xs4all.nl) -
# Added support for the solaris Born shell.
# Changed the directory configure/ to config/ in order not to collide with
# GNU autoconf in the future.
#
# 020997 (carlo@runaway.xs4all.nl) -
# The future is here... renamed 'config.in' to 'config-sh', reserving
# 'file.in' for input files of autoconfs' 'configure' script.
# 'autoconf.h' has been renamed to 'config.h', because it would be confusing
# otherwise with autoconf.
# Changed Configure to be run from within config/.
#
# 050997 (carlo@runaway.xs4all.nl) -
# Don't use 'def={$old:-$3}' but instead the equivalent ': ${def=$3}' because
# some sh don't understand the first expression.
# Pressing a 'c' at any question continues in 'batch' mode.
#
# 220199 (carlo@runaway.xs4all.nl) -
# Put MACRO name at front, making it easier to find it when scanning
# through the defaults to change just one.
# Make pressing 'c' stop at every paragraph, and pressing 'C' finish the
# whole script.
#
# -----------------------------------------------------------------------------
#
# This script is used to configure the linux kernel.
#
# It was inspired by the challenge in the original Configure script
# to ``do something better'', combined with the actual need to ``do
# something better'' because the old configure script wasn't flexible
# enough.
#
# Please send comments / questions / bug fixes to raymondc@microsoft.com.
#
#              ***** IMPORTANT COMPATIBILITY NOTE ****
# If configuration changes are made which might adversely effect 
# Menuconfig or xconfig, please notify the respective authors so that 
# those utilities can be updated in parallel.
#
# Menuconfig:  <roadcapw@cfw.com>
# xconfig:     <apenwarr@foxnet.net>  <eric@aib.com>
#              ****************************************
#
# Each line in the config file is a command.
#
# 050793 - use IFS='@' to get around a bug in a pre-version of bash-1.13
# with an empty IFS.
#
# 030995 (storner@osiris.ping.dk) - added support for tri-state answers,
# for selecting modules to compile.
#
# 180995 Bernhard Kaindl (bkaindl@ping.at) - added dummy functions for
# use with a config.in modified for make menuconfig.
#
# 301195 (boldt@math.ucsb.edu) - added help text support
#
# 281295 Paul Gortmaker - make tri_state functions collapse to boolean
# if module support is not enabled.
#
# 010296 Aaron Ucko (ucko@vax1.rockhurst.edu) - fix int and hex to accept
# arbitrary ranges
#
# 150296 Dick Streefland (dicks@tasking.nl) - report new configuration
# items and ask for a value even when doing a "make oldconfig"
#
# 200396 Tom Dyas (tdyas@eden.rutgers.edu) - when the module option is
# chosen for an item, define the macro <option_name>_MODULE
#
# 090397 Axel Boldt (boldt@math.ucsb.edu) - avoid ? and + in regular 
# expressions for GNU expr since version 1.15 and up use \? and \+.

#### Start of system configuration section. ####

SHELL=@SHPROG@
RM=@RMPROG@
LN_S="@LN_S@"
unet_cv_sys_set_h=@unet_cv_sys_set_h@

#### End of system configuration section. ####

# Disable filename globbing
set -f

if test $unet_cv_sys_set_h = yes; then
	set -h
fi

# Figure out how to do 'echo' without newline:
c=''
n=''
if [ "`eval echo -n 'a'`" = "-n a" ] ; then
	c='\c'
else
	n='-n'
fi

# Check if ${VAR:-def} works
DUMMY_VAR=""
DUMMY_VAR2="some value"
DUMMY_VAR=${DUMMY_VAR:-test}
DUMMY_VAR2=${DUMMY_VAR2:-test}
if [ "$DUMMY_VAR" != "test" -o "$DUMMY_VAR2" != "some value" ]; then
  echo "You /bin/sh doesn't understand '\${VAR:-default}'"
  exit 1
fi

#
# Dummy functions for use with a config.in modified for menuconf
#
mainmenu_option () {
	:
}
mainmenu_name () {
	:
}
endmenu () {
	:
}

#
# help prints the corresponding help text from Configure.help to stdout
#
#       help variable
#
help () {
  if [ -f ../doc/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[ 	]*\\$/b
        /^#.*/b
        /^[ 	]*\\$/q
        p
    }" ../doc/Configure.help`
    if [ -z "$text" ]
    then
      echo; echo "  Sorry, no help available for this option yet.";echo
    else
      (echo; echo "$text"; echo) | ${PAGER:-more}
    fi
  else
    echo;
    echo "  Can't access the file doc/Configure.help which"
    echo "  should contain the help texts."
    echo
  fi
}


#
# readln reads a line into $ans.
#
#	readln macro prompt default oldval
#
readln () {
  echo $1 | awk '{ printf("%-20.20s: ", $1); }'
  if [ "$USE_DEFAULT" != "n" -a -n "$4" ]; then
    echo $2
    ans=$3
  else
    echo $n "$2$c"
    [ -z "$4" ] && echo $n "(NEW) $c"
    IFS='@' read ans </dev/tty || exit 1
    [ -z "$ans" ] && ans=$3
    if [ "$ans" = "c" -o "$ans" = "C" ]; then
      USE_DEFAULT=$ans
      ans=$3
    fi
  fi
}

#
# comment does some pretty-printing
#
#	comment 'xxx'
# 
comment () {
  echo ""
  echo "*-----------------------------------------------------------------------------";
  echo "* $1" ;
  echo "*"
  (echo "" ; echo "#"; echo "# $1" ; echo "#") >>$CONFIG
  (echo "" ; echo "/*"; echo " * $1" ; echo " */") >>$CONFIG_H
  if [ "$USE_DEFAULT" = "c" ]; then
    USE_DEFAULT=n
  fi
}

#
# define_bool sets the value of a boolean argument
#
#	define_bool define value
#
define_bool () {
  case "$2" in
    "y")
      echo "$1=y" >>$CONFIG
      echo "#define $1" >>$CONFIG_H
      ;;

    "n")
      echo "$1=n" >>$CONFIG
      echo "#undef  $1" >>$CONFIG_H
      ;;
  esac
  eval "$1=$2"
}

#
# expr determines if string matches a regular expression
#
# 	expr string reg_exp
#
expr () {
  MATCH=`echo $1 | egrep -e $2`
  if [ -z "$MATCH" ]; then
    return 1
  else
    return 0
  fi
}

#
# bool processes a boolean argument
#
#	bool question define
#
bool () {
  old=`eval echo "\\${$2}"`
  if [ -z "$old" -a -n "$3" ]; then
    def=$3
  else
    def=${old:-n}
  fi
  case "$def" in
    "y")
      defprompt="Y/n/?"
      def="y"
      ;;

    "n")
      defprompt="N/y/?"
      ;;
  esac
  while :; do
    readln $2 "$1 [$defprompt] " "$def" "$old"
    case "$ans" in
      [yY] | [yY]es )
        define_bool "$2" "y"
        break
	;;

      [nN] | [nN]o )
        define_bool "$2" "n"
	break
	;;

      * )
        help "$2"
        ;;
    esac
  done
}

#
# define_int sets the value of a integer argument
#
#	define_int define value
#
define_int () {
  echo "$1=$2" >>$CONFIG
  echo "#define $1 ($2)" >>$CONFIG_H
  eval "$1=$2"
}

#
# int processes an integer argument
#
#	int question define default
#
int () {
  old=`eval echo "\\${$2}"`
  def=${old:-$3}
  while :; do
    readln $2 "$1 [$def] " "$def" "$old"
    if expr "$ans" "^0$|^(-[1-9]|[1-9])[0-9]*$"; then
      define_int "$2" "$ans"
      break
    else
      help "$2"
    fi
  done
}

#
# choice processes a choice list (1-out-of-n)
#
#	choice question choice-list default
#
# The choice list has a syntax of:
#	NAME WHITESPACE VALUE { WHITESPACE NAME WHITESPACE VALUE }
# The user may enter any unique prefix of one of the NAMEs and
# choice will define VALUE as if it were a boolean option.
# VALUE must be in all uppercase.  Normally, VALUE is of the
# form CONFIG_<something>.  Thus, if the user selects <something>,
# the CPP symbol CONFIG_<something> will be defined and the
# shell variable CONFIG_<something> will be set to "y".
#
choice () {
  question="$1"
  choices="$2"
  old=
  def=$3

  # determine default answer:
  names=""
  eval set -- $choices
  firstvar=$2
  while [ -n "$2" ]; do
    if [ -n "$names" ]; then
      names="$names, $1"
    else
      names="$1"
    fi
    if [ "`eval echo "\\${$2}"`" = "y" ]; then
      old=$1
      def=$1
    fi
    shift; shift
  done

  val=""
  while [ -z "$val" ]; do
    ambg=n
    readln $names "$question [$def] " "$def" "$old"
    ans=`echo $ans | tr a-z A-Z`
    eval set -- $choices
    while [ -n "$1" ]; do
      name=`echo $1 | tr a-z A-Z`
      case "$name" in
        "$ans"* )
	  if [ "$name" = "$ans" ]; then
	    val="$2"
	    break	# stop on exact match
	  fi
	  if [ -n "$val" ]; then
	    echo;
	    echo \
	        "  Sorry, \"$ans\" is ambiguous; please enter a longer string."
	    echo
	    val=""
	    ambg=y
	    break
	  else
	    val="$2"
	  fi
	  ;;
      esac
      shift; shift
    done
    if [ "$val" = "" -a "$ambg" = "n" ]; then
      help "$firstvar"
    fi
  done
  eval set -- $choices
  while [ -n "$2" ]; do
    if [ "$2" = "$val" ]; then
      echo "  defined $val"
      define_bool "$2" "y"
    else
      define_bool "$2" "n"
    fi
    shift; shift
  done
}

#
# define_macro sets the value of a macro argument
#
#	define_macro define value
#
define_macro () {
  if [ -n "$2" ]; then
    echo "$1=\"$2\"" >>$CONFIG
    echo "#define $1 $2" >>$CONFIG_H
  fi
  eval "$1=\"$2\""
}

#
# define_string sets the value of a string argument
#
#	define_string define value
#
define_string () {
  if [ -n "$2" ]; then
    echo "$1=\"$2\"" >>$CONFIG
    echo "#define $1 \"$2\"" >>$CONFIG_H
  fi
  eval "$1=\"$2\""
}

#
# string processes a string argument
#
#	string question define default
#
string () {
  old=`eval echo "\\${$2}"`
  def=${old:-$3}
  while :; do
    readln $2 "$1 [$def] " "$def" "$old"
    if expr "$ans" "^[^\"].*[^\"]$"; then
      define_string "$2" "$ans"
      break
    else
      help "$2"
    fi
  done
}

CONFIG=.tmpconfig
CONFIG_H=.tmpconfig.h
trap "$RM -f $CONFIG $CONFIG_H ; exit 1" 1 2 15

#
# Make sure we start out with a clean slate.
#
echo "#" > $CONFIG
echo "# Configuration default generated for:" > $CONFIG
echo '# `pwd`' > $CONFIG
echo "#" > $CONFIG
echo "# Automatically generated by 'make config': don't edit" >> $CONFIG
echo "#" >> $CONFIG

echo "/*" > $CONFIG_H
echo " * Automatically generated C config: don't edit" >> $CONFIG_H
echo " */" >> $CONFIG_H
echo "#ifndef INCLUDED_config_h" >> $CONFIG_H
echo "#define INCLUDED_config_h" >> $CONFIG_H
echo "#ifndef INCLUDED_setup_h" >> $CONFIG_H
echo "#include \"setup.h\"">> $CONFIG_H
echo "#endif" >> $CONFIG_H
echo "#define AUTOCONF_INCLUDED" >> $CONFIG_H

CONFIG_IN=./config-sh
if [ "$1" != "" ] ; then
  CONFIG_IN=$1
fi

DEFAULTS=none
if [ -r .config ]; then
  DEFAULTS=./.config
else
  if [ -r ../../.config ]; then
    echo "*"
    echo "* WARNING: Inheriting .config from previous version!"
    DEFAULTS=../../.config
  fi
fi

if [ -r $DEFAULTS ]; then
  echo "*"
  echo "* Using defaults found in" $DEFAULTS
  echo "* If you type a 'C' the script will finish using all defaults."
  echo "* If you type a 'c' the script will skip to the next paragraph."
  echo "*"
  . $DEFAULTS
else
  echo "*"
  echo "* No defaults found"
  echo "*"
fi

. $CONFIG_IN

echo "#endif /* INCLUDED_config_h */" >> $CONFIG_H

mv $CONFIG_H config.h
$RM -f .config.old
if [ -f .config ]; then
  # Keep the inode of .config (hardlink to ../../.config) intact:
  cp .config .config.old
  cat $CONFIG > .config
  $RM -f $CONFIG
fi
if test ! -f .config -o ! -r ../../.config; then
  if [ "$CONFIG_BATCH" = "y" ]; then
    CONFIG_NEW=n
  else
    CONFIG_NEW=y
    USE_DEFAULT=n
    echo "*"
    echo "*"
    echo "*"
    bool 'Use .config of THIS source tree as your upgrade default' CONFIG_NEW
  fi
  if [ ! -f .config ]; then
    mv $CONFIG .config
  fi
  if [ "$CONFIG_NEW" = "y" ]; then
    CONFIG_CURDIR=`pwd`
    cd ../..
    $RM -f .config .config.cache
    ln $CONFIG_CURDIR/.config .config
    ln $CONFIG_CURDIR/config.cache .config.cache
    echo
    echo "NOTE: Linking ../.config to THIS source tree's configuration !"
    echo "(The configuration of this source tree will be used as default when you upgrade)"
  fi
fi

echo

exit 0
