pgplus/bin/
pgplus/help_files/
pgplus/port_redirector/
pgplus/src/configure/makefiles/
#
# This script configures PG+
# Brutually and blatently hacked from the Linux kernal compile :o)
#

# Disable filename globbing once and for all.
# Enable function cacheing.

set -f -h

#
# help prints the corresponding help text from Configure.help to stdout
#
#       help variable
#
function help () {
  if [ -f configure/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
		    }" configure/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 Configure.help which"
     echo "  should contain the help texts."
     echo
  fi
}


#
# readln reads a line into $ans.
#
#	readln prompt default oldval
#
function readln () {
	if [ "$DEFAULT" = "-d" -a -n "$3" ]; then
		echo "$1"
		ans=$2
	else
		echo -n "$1"
		[ -z "$3" ] && echo -n "(NEW) "
		IFS='@' read ans </dev/tty || exit 1
		[ -z "$ans" ] && ans=$2
	fi
}

#
# comment does some pretty-printing
#
#	comment 'xxx'
# 
function comment () {
	echo "*"; echo "* $1" ; echo "*"
	(echo "" ; echo "#"; echo "# $1" ; echo "#") >>$CONFIG
	(echo "" ; echo "/*"; echo " * $1" ; echo " */") >>$CONFIG_H
}

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

	 "m")
		echo "$1=m" >>$CONFIG
		echo "#undef  $1" >>$CONFIG_H
		echo "#define $1_MODULE 1" >>$CONFIG_H
		;;

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

#
# bool processes a boolean argument
#
#	bool question define
#
function bool () {
	old=$(eval echo "\${$2}")
	def=${old:-'n'}
	case "$def" in
	 "y" | "m") defprompt="Y/n/?"
	      def="y"
	      ;;
	 "n") defprompt="N/y/?"
	      ;;
	esac
	while :; do
	  readln "$1 ($2) [$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
#
function define_int () {
	echo "$1=$2" >>$CONFIG
	echo "#define $1 ($2)" >>$CONFIG_H
	eval "$1=$2"
}

#
# int processes an integer argument
#
#	int question define default
#
function int () {
	old=$(eval echo "\${$2}")
	def=${old:-$3}
	while :; do
	  readln "$1 ($2) [$def] " "$def" "$old"
	  if expr "$ans" > /dev/null; then
	    define_int "$2" "$ans"
	    break
          else
   	    help "$2"
          fi
	done
}

#
# define_string sets the value of a string argument
#
#	define_string define value
#
function define_string () {
	echo "$1=\"$2\"" >>$CONFIG
	echo "#define $1 \"$2\"" >>$CONFIG_H
	eval "$1=\"$2\""
}

#
# int processes an integer argument
#
#	string question define default
#
function string () {
	old=$(eval echo "\${$2}")
	def=${old:-$3}
	while :; do
	  readln "$1 ($2) [$def] " "$def" "$old"
	  if expr "$ans" > /dev/null; then
	    define_string "$2" "$ans"
	    break
	  else
	    help "$2"
	  fi
	done
}

#
# define_hex sets the value of a hexadecimal argument
#
#	define_hex define value
#
function define_hex () {
	echo "$1=$2" >>$CONFIG
	echo "#define $1 0x${2#*[x,X]}" >>$CONFIG_H
	eval "$1=$2"
}

#
# hex processes an hexadecimal argument
#
#	hex question define default
#
function hex () {
	old=$(eval echo "\${$2}")
	def=${old:-$3}
	def=${def#*[x,X]}
	while :; do
	  readln "$1 ($2) [$def] " "$def" "$old"
	  ans=${ans#*[x,X]}
	 if expr "$ans" : '[0-9a-fA-F]+$' > /dev/null; then
	   define_hex "$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".
#
function choice () {
	question="$1"
	choices="$2"
	old=
	def=$3

	# determine default answer:
	names=""
	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 "$question ($names) [$def] " "$def" "$old"
		ans=$(echo $ans | tr a-z A-Z)
		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
	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
}

CONFIG=configure/.tmpconfig
CONFIG_H=configure/.tmpconfig.h
trap "rm -f $CONFIG $CONFIG_H ; exit 1" 1 2

#
# Make sure we start out with a clean slate.
#
echo "#" > $CONFIG
echo "# Playground+ Configuration File" >> $CONFIG
echo "# Use 'make config' to edit this" >> $CONFIG
echo "#" >> $CONFIG

echo "/*" > $CONFIG_H
echo " * Playground+ - autoconfig.h" >> $CONFIG_H
echo " * Configuration details. Don't edit this but use \"make config\" to change!" >> $CONFIG_H
echo " * ---------------------------------------------------------------------------" >> $CONFIG_H
echo " */" >> $CONFIG_H
echo " " >> $CONFIG_H

DEFAULT=""
if [ "$1" = "-d" ] ; then
	DEFAULT="-d"
	shift
fi

CONFIG_OPS=./Configure.options
if [ "$1" != "" ] ; then
	CONFIG_OPS=$1
fi

DEFAULTS=configure/Configure.defaults
if [ -f configure/.config ]; then
  DEFAULTS=configure/.config
fi

if [ -f $DEFAULTS ]; then
  echo "#"
  echo "# Using defaults found in" $DEFAULTS
  echo "#"
  . $DEFAULTS
  sed -e 's/# \(.*\) is not.*/\1=n/' < $DEFAULTS > /tmp/conf.$$
  . /tmp/conf.$$
  rm /tmp/conf.$$
else
  echo "#"
  echo "# No defaults found"
  echo "#"
fi

. $CONFIG_OPS

rm -f configure/.config.old
if [ -f configure/.config ]; then
	mv configure/.config configure/.config.old
fi
mv configure/.tmpconfig configure/.config
mv configure/.tmpconfig.h configure/.config.h
cp configure/.config.h include/autoconfig.h

exit 0