#! /bin/sh
# ---------------------------------------------------------- 
#   (C) 1994, 1995 Institute for New Generation Computer Technology 
#	(Read COPYRIGHT for detailed information.) 
# ----------------------------------------------------------

if test $# -ge 1 -a "$1" = '-d';
  then shift; def_ask=false;
  else def_ask=true
fi
def_setting=./config.sh
if test $# -eq 1 -a "$1" = builddist; then
  BUILDDIST=true
else
  BUILDDIST=false
fi

cat <<GAZONK
#
# WELCOME TO KLIC SYSTEM CONFIGURAION SCRIPT
#
#  Copyright 1994 Institute for New Generation Computer Technology
#  Read COPYRIGHT for detailed information
#
# In this script, I'll ask you for several options you can make.
# Default values are presented in brackets as in [default].
# If the default is OK, simply hit CR.
# Otherwise, type in whatever is your option.
# When you want to specify nothing, type in "none".
#

GAZONK

: ::::::::::::::::::: HELPER FUNCTIONS ::::::::::::::::::::
: Find how to suppress newline after echo
if ( echo "test line\c"; echo " ") | grep c >/dev/null 2>/dev/null ; then
    n='-n'
    c=''
else
    n=''
    c='\c'
fi

: Define some handy functions

clean_up_config () {
  rm -f nmout.tmp nmout.list nmout.all 2>/dev/null
}

give_up_config () {
    echo " "
    for message do
	echo "!!! $message !!!"
    done
    clean_up_config
    echo "Aborting KLIC configuration script"
    echo ' '
    exit 1
}

ask_yes_or_no () {
    set_var=$2;
    do_ask=true;
    if [ -n "$set_var" ]; then eval "default=\$$set_var"; do_ask=$def_ask;
    else default=""; fi
    if [ -z "$default" ]; then default=$1; do_ask=true; fi
    if [ "$set_var" = USE_DEFAULT ]; then set_var=''; do_ask=$def_ask; fi
    echo $n " (yes or no)"$c
    while true ; do
	echo $n " [$default] "$c
	if $do_ask ; then read ans; else echo $default; ans=$default; fi
	if test -z "$ans" ; then ans=$default; fi
	if [ -n "$set_var" ]; then echo "$set_var='$ans'" >> config.tmp; fi
	case $ans in
	yes) return 0;;
	no) return 1;;
	*) echo $n "Please input yes or no"$c;;
	esac
	do_ask=true
    done
}

ask_with_default () {
    message=$1
    macroname=$2
    do_ask=true;
    eval def_value=\$"DEF_$2"
    if [ -n "$def_value" ]; then
	eval "$macroname='$def_value'"; do_ask=$def_ask
    fi
    eval default=\$$macroname
    echo $n "Which $message to use? [$default] "$c
    if $do_ask ; then read ans; else echo $default; ans=$default; fi
    case $ans in
    none) ans='';;
    '') {
	if test "$default" = "none"; then
	    ans=''
	else
	    ans=$default
	fi
	} ;;
    esac
    eval $macroname=\$ans
    if [ -z "$ans" ]; then ans=none; fi
    echo "DEF_$macroname='$ans'" >>config.tmp
}

locate_file () {
file=$1
shift
kind=$1
shift
for dir in $*; do
    if test -$kind $dir/$file; then
	where=$dir/$file
	return 0
    fi
done
return 1
}

locate_executable () {
    file=$1
    shift
    user_path=$*
    locate_file "$file" x $user_path
    return
}

locate_directory () {
    located_dir=$1
    test -d $located_dir ||
    {
	echo $n "Cannot find $located_dir; Create it now? "$c
	if ask_yes_or_no yes; then
	    if mkdir $located_dir ; then
		return 0
	    else
		echo "!!! Couldn't create $located_dir !!!"
		return 1
	    fi
	else
	    return 1
	fi
    }
}

ask_additional () {
message=$1
macroname=$2
    do_ask=true;
    eval def_value=\$"DEF_$2"
    if [ -n "$def_value" ]; then
	eval "$macroname='$def_value'"; do_ask=$def_ask
    fi

eval default=\$$macroname
case $default in
  '') default="none" ;;
esac
echo "# Default $message are: $default"
echo ' '
echo $n "Do you want to change the default? "$c
eval retval="\$DEF_$macroname";
use_default=''
if [ $def_ask="false" -a -n "$retval" ];
then
  ask_or_not=no
  use_default=USE_DEFAULT
else
  ask_or_not=no
fi

if ask_yes_or_no $ask_or_not $use_default; then
  echo ' '
  if test '$default'!=none; then
    echo $n "Do you want to delete any of them? "$c
    if ask_yes_or_no no; then
      echo ' '
      echo "# OK, then I'll clear the default."
      eval $macroname=\"\"
      default=
      echo "# Now there are no default $message."
      echo ' '
    fi
  fi
  echo $n "Specify additions if any: "$c
  while true; do
     if $do_ask ; then read ans; else echo $default; ans=$default; fi
      case $ans in
      '') eval retval="\$$macroname" 
	  echo "DEF_$macroname='$retval'" >>config.tmp
	  return 0;;
      *) eval $macroname=\"\$$macroname \$ans\"
      esac
      echo $n "More? "$c
      do_ask=true
  done
else
  eval retval="\$$macroname" 
  echo "DEF_$macroname='$retval'" >>config.tmp
fi
}

ask_executable () {
what=$1
macroname=$2
do_ask=true
eval "def_value=\$DEF_$2"
if [ -n "$def_value" ];
   then eval "$macroname='$def_value'"; do_ask=$def_ask;
fi

while true; do
    eval default=\$$macroname
    echo $n "Which $what to use? [$default] "$c
    if $do_ask ; then read ans; else echo $default; ans=$default; fi
    case $ans in
    '') eval result=\$$macroname;;
    *) result=$ans;;
    esac
    case $result in
    /*) {
	  if test -x $result; then
	      eval $macroname=\$result
	      echo "DEF_$macroname='$result'" >>config.tmp
	      return 0
	  else
	      echo !!! Can\'t find an executable file \"$result\" !!!
	  fi
	};;
    *)	{
	  if locate_executable $result $user_path; then
	      eval $macroname=\$result
	      echo "DEF_$macroname='$result'" >>config.tmp
	      return 0
	  else
	      echo !!! Can\'t find \"$result\" along your path !!!
	      echo ' '
	  fi
	} ;;
    esac
    do_ask=true
done
}

ask_directory () {
what=$1
macroname=$2
do_ask=true;
eval "def_value=\$DEF_$2"
if [ -n "$def_value" ]; then eval $macroname="$def_value"; do_ask=$def_ask; fi
while true; do
    eval default=\$$macroname
    echo $n "Which $what directory to use? [$default] "$c
    if $do_ask ; then read ans; else echo $default; ans=$default; fi
    case $ans in
    '') eval result=\$$macroname;;
    *) result=$ans;;
    esac
    if locate_directory "$result"; then
	result=`cd $result; pwd`
        eval $macroname=\$result
        echo "DEF_$macroname='$result'" >> config.tmp
	return 0
    fi
done
}

check_writable () {
what=$1
macroname=$2
eval filename=\$$macroname
if test -d $filename; then
    if test -w $filename; then
	return
    else
	echo "!!! You do not  have write access to $filename !!!"
    fi
elif test -f $filename; then
    echo "!!! $filename is not a directory"
else
    echo "!!! Directory $filename does not exist !!!"
fi
ask_directory "$what" $macroname
}

: ::::::::::::::::::: HELPER FUNCTIONS ::::::::::::::::::::

if [ -f $def_setting ]; then
  echo  "Previous configuration setting '$def_setting' exists."
  echo  $n "Do I read default setting from it?"$c
	if ask_yes_or_no no;
	  then  . $def_setting
	  else def_ask=true
	fi
	echo ' '
else def_ask=true
fi

rm -f config.tmp

ARCHIVES="libklic.a libklict.a"

cat <<GAZONK
# This is version of KLIC contains its sequential core,
# a distributed parallel implementation that requires PVM 3.3 or newer,
# and a shared memory parallel implementation that works either on
# Sparc-based system with Solaris 2.X or Alpha-based system with DEC OSF/1.
#
# The sequential core is mandatory.
# Installation of parallel implementations is optional.

GAZONK

: ::: CPU Type
CPU="Unknown_cpu"

echo $n "Configure also for parallel implementations? "$c
if ask_yes_or_no no DEF_PARALLEL; then

: ::: BEGIN PARALLEL :::
: ::::::::::::::::::: BEGIN PVM SETTING ::::::::::::::::::::
echo $n "Configure for distributed memory parallel KLIC? "$c
DISTSYSTEM=false
if ask_yes_or_no no DEF_DISTSYSTEM; then
DISTSYSTEM=true
ARCHIVES="$ARCHIVES libklicd.a"
cat <<GAZONK

# This KLIC needs a PVM3.3 system installed.

GAZONK

if test "$PVM_ROOT" = ""; then
	PVM_ROOT="/usr/local/src/pvm3/pvm3.3"
fi
ask_directory "PVM_ROOT" "PVM_ROOT" || exit 1
if test "$PVM_ARCH" = ""; then
	if test -x $PVM_ROOT/lib/pvmgetarch; then
		PVM_ARCH=`$PVM_ROOT/lib/pvmgetarch`
	else
		PVM_ARCH="SUN4SOL2"
	fi
fi
ask_with_default "PVM_ARCH" "PVM_ARCH" || exit 1
if test "$PVMLIB" = ""; then
	PVMLIB="pvm3"
fi
ask_with_default "PVM library" "PVMLIB" || exit 1
fi

: ::::::::::::::::::: END PVM SETTING ::::::::::::::::::::

: ::::::::::::::::::: SHARED Memory SETTING ::::::::::::::::::::
echo ' '
echo $n "Configure for shared memory parallel KLIC? "$c
if ask_yes_or_no no DEF_SHMSYSTEM; then
 if ( uname -m ) | grep "sun4d" >/dev/null 2>/dev/null ; then 
  CPU="SPARC"
  ARCHIVES="$ARCHIVES libklics.a"
 elif ( uname -m ) | grep "sun4m" >/dev/null 2>/dev/null ; then 
  CPU="SPARC"
  ARCHIVES="$ARCHIVES libklics.a"
 elif ( uname -m ) | grep "alpha" >/dev/null 2>/dev/null ; then
   CPU="ALPHA"
   ARCHIVES="$ARCHIVES libklics.a"
 else
cat <<GAZONK
# I do not know this machine.
# Sorry I cannot install Shared-memory parallel KLIC.

GAZONK
 fi
fi

: ::: END PARALLEL :::
fi

: ::::::::::::::::::: CLEARING UP ::::::::::::::::::::
echo ' '
if test -f Makefile; then
cat <<GAZONK
# I found Makefile in this directory.  This might mean that you already
# have installed the KLIC system before.

GAZONK
echo $n "May I clean-up the directory by \"make clean\" first? "$c
if ask_yes_or_no yes; then
  echo ' '
  make clean ||
  give_up_config "Make clean ends with non-zero return code"
  echo ' '
else
  give_up_config "OK, then I'll stop reconfiguration."
fi
fi
: ::::::::::::::::::: END CLEARING UP ::::::::::::::::::::

user_path=`echo $PATH | sed -e 's/:/ /g'`
export user_path

if $BUILDDIST; then
  cat <<GAZONK
# You specified to rebuild the distribution.   To rebuild the distribution,
# an already working KL1 to C compiler (kl1cmp) is required.

GAZONK
  OLDLIBDIR="/usr/local/lib/klic"
  ask_directory "KLIC Library Directory" "OLDLIBDIR" || exit 1
  echo ' '
  KL1CMP="$OLDLIBDIR/kl1cmp"
  ask_executable "KL1 to C compiler (kl1cmp)" "KL1CMP" || exit 1
  echo ' '
else
  KL1CMP="false"
fi

if locate_executable "gcc" $user_path; then
    CC="gcc"
    cat <<GAZONK
# Found gcc in $where.
# I'd recommend using gcc for installing KLIC, as it's been tested
# mainly with gcc.

GAZONK
else
    CC=cc
fi

ask_executable "C compiler" "CC" || exit 1
echo ' '

case $CC in
  gcc)
	OPTLEVEL="2"
	DEBUGFLAGS="-g"
	UOPTFLAGS="-fomit-frame-pointer";;
  cc)	OPTLEVEL=none
	DEBUGFLAGS="-g"
        UOPTFLAGS=none;;
  *)	OPTLEVEL=none
	DEBUGFLAGS=none
        UOPTFLAGS=none;;
esac

cat <<GAZONK
# During the installation procedure, the runtime library for the KLIC system
# will be compiled using $CC.
#
# You can choose optimization and debug flags for this compilation now
# (I'll ask you about setting for compiling user programs later).

GAZONK
ask_with_default "optimization level" OPTLEVEL
ask_with_default "debug flags" DEBUGFLAGS

rm -f Configure.ln
LN="ln -s"
if $LN Configure Configure.ln 2>/dev/null
  then true
  else LN=ln
fi
rm -f Configure.ln
echo ' '

cat <<GAZONK
# When KL1 programs are compiled, they are compiled into C code and then
# compiled into executable using $CC.  You can specify optimization level
# and debug flag for $CC in the command line argument for the KLIC compiler.
#
# You can here select flags that'll be only set for optimized compilation,
# that is, flags that'll be supplied only when you specified -O# when you
# run the KLIC compiler.

GAZONK
ask_with_default "additional optimization flags" UOPTFLAGS
echo " "

case $OPTLEVEL in
  "none" | "" | 0)
	OPTFLAGS=""
	KLICOPT="";;
  1|2|3|4|5|6|7|8|9)
	OPTFLAGS="-O$OPTLEVEL $UOPTFLAGS"
	KLICOPT="-O$OPTLEVEL";;
  *)	OPTFLAGS=""
	KLICOPT=""
	OPTLEVEL=0
	echo "Illegal optimization level ($OPTLEVEL); set to 0";;
esac

KLICCFLAGS=none
cat <<GAZONK
# You can specify addtional flags for $CC.
# If "-pipe" is available, specify it here.

GAZONK
ask_with_default "C compiler flags" KLICCFLAGS
echo ' '

cat <<GAZONK
# KLIC will normally use $CC for linkage also,
# but you can specify a different linker.

GAZONK
echo $n "May I use $CC for linkage also? "$c
if ask_yes_or_no yes DEF_USECC_AS_LD; then
  LD=$CC
  KLICLDFLAGS=""
else
  LD=ld
  ask_executable "link editor" "LD" || exit 1
  echo ' '
cat <<GAZONK
# You can specify addtional flags for $LD.

GAZONK
  KLICLDFLAGS=none
  ask_with_default "linker flags" KLICLDFLAGS
fi
echo ' '

if locate_executable "ranlib" $user_path; then
    RANLIB=ranlib
else
    RANLIB=true
    cat <<GAZONK
# I couldn't find "ranlib" along your path.
# Use "true" instead if your system doesn't need running ranlib.

GAZONK
    ask_executable "library indexer" "RANLIB" || exit 1
    echo ' '
fi

cat <<GAZONK
# link command is used during the system compilation.
# I'd recommend to use sumbolic link if your system supports.

GAZONK
ask_with_default "link command" LN
echo ' '

if locate_executable "make" $user_path; then
    MAKE=make
else
    MAKE=""
    cat <<GAZONK
# I couldn't find "make" along your path.

GAZONK
    ask_executable "make" "MAKE" || exit 1
    echo ' '
fi

INSTALL="/bin/cp -p"
INSTDIR="/bin/mkdir -p"

cat <<GAZONK
# For KLIC to support asynchronous stream I/O, the system should 
# deliver signals such as SIGIO or SIGPOLL when I/O becomes possible.
# However, some systems (such as Linux 1.0) does not support this.

GAZONK
echo $n "Does your system support I/O ready signals?"$c
if ask_yes_or_no yes DEF_ASYNCIO; then
    ASYNCIO="#define"
else
    ASYNCIO="#undef"
fi
echo ' '

cat <<GAZONK
# The KLIC system will be installed under certain directories.
# You have to be able to write into that directory for installation.
# Also, the installed files under the directory should be visible from
# all the KLIC users (only yourself for a private installation).
#
# First, the root of the installation directory tree.

GAZONK

DIRPREFIX="/usr/local"
ask_directory "intallation root" DIRPREFIX ||
give_up_config "Coudn't determine installation root directory"

cat <<GAZONK

# By default, KLIC system will be installed in the subdirectories of
# the directory you specified ($DIRPREFIX).
# In the default setting, subdirectories used are:
#    $DIRPREFIX/bin:     for user command (klic)
#    $DIRPREFIX/lib:     for subprograms and libraries
#    $DIRPREFIX/include: for header files

GAZONK

KLICBIN=$DIRPREFIX/bin
KLICLIB=$DIRPREFIX/lib
KLICINCLUDE=$DIRPREFIX/include

echo $n  "May I use the default setting?"$c
ask_yes_or_no yes DEF_USE_DEFAULT_SETTING ||
{
    ask_directory "user command" KLICBIN
    ask_directory "library" KLICLIB
    ask_directory "include file" KLICINCLUDE
}
check_writable "user command" KLICBIN
check_writable "library" KLICLIB
check_writable "include file" KLICINCLUDE
echo " "

LIBPATH=""
for dir in /lib /usr/lib /usr/local/lib; do
    if test -d $dir; then
	LIBPATH="$LIBPATH $dir"
    fi
done
# for NEC EWS4800 Rev 8.1
case $HOSTTYPE in
  mips|ews_mips)
	for dir in /usr/necccs/lib ; do
	 if test -d $dir; then
	   LIBPATH="$dir $LIBPATH"
	  fi
	done ;;
  *)
esac

echo "# Specify directories for library archives."
ask_additional "library directories" LIBPATH
echo ' '

INCLUDEDIR=/usr/include
ask_directory "default include file directory" INCLUDEDIR
while test ! -d $INCLUDEDIR; do
    echo "# I couldn't find $INCLUDEDIR"
    ask_directory "default include file directory" INCLUDEDIR
done
echo ' '

LIBNAMES=""
locate_file libc_s.a f $LIBPATH && LIBNAMES="$LIBNAMES libc_s"
locate_file libm.a f $LIBPATH && LIBNAMES="$LIBNAMES libm"
locate_file libelf.a f $LIBPATH && LIBNAMES="$LIBNAMES libelf"
( locate_file libsocket.a f $LIBPATH || locate_file libnsl.so f $LIBPATH ) \
	&& LIBNAMES="$LIBNAMES libsocket"
( locate_file libnsl.a f $LIBPATH || locate_file libnsl.so f $LIBPATH ) \
	&& LIBNAMES="$LIBNAMES libnsl"
locate_file libmld.a f $LIBPATH && LIBNAMES="$LIBNAMES libmld"

echo "# Specify library archive files."
ask_additional "library archives" LIBNAMES
echo ' '

locate_file libc.a f $LIBPATH ||
give_up_config "Couldn't locate libc.a in directories $LIBPATH"
LIBFILES="$where"
echo $n "# Using library file(s):"$c
for lib in $LIBNAMES; do
  if locate_file $lib.a f $LIBPATH; then
    LIBFILES="$LIBFILES $where"
    echo $n " $where"$c
  elif locate_file $lib.so f $LIBPATH; then
# for SGI IRIS IRIX 5.2
    LIBFILES="$LIBFILES $where"
    echo $n " $where"$c
  else
    give_up_config "Couldn't find $lib.a in $LIBPATH"
  fi
done
echo ' '
echo ' '

LIBSWITCHES=`echo " " $LIBNAMES | sed -n -e "s/ lib/ -l/gp"`

for dir in $LIBPATH ; do
   LIBSWITCHES="-L$dir $LIBSWITCHES"
done

echo "# Specify directories for header files at KL1 program compilation."
ask_additional "include directory for compiling KLIC" KLICINCLUDE
echo ' '

XTERM=""
if locate_executable "kterm" $user_path; then
  XTERM=$where
elif locate_executable "xterm" $user_path; then
  XTERM=$where
fi

echo "# KLIC has a multi-window tracer using X window."
echo ' '

echo $n "Do you want to use X-based multi-window tracer?" $c
if ask_yes_or_no no; then
  ask_executable "Terminal program for X" "XTERM"
  if test "$XTERM" = ""; then
    echo "Given up to install the multi-window tracer"
  fi
else
  XTERM=""
fi
echo ' '

USER=${USER:-unknown}
case $USER in
  unknown)
    if locate_executable "whoami" $user_path; then
	USER=`$where`
    fi
esac

locate_executable "nm" $user_path ||
give_up_config "Couldn't find "nm" along your path"

echo "# I'll make a summary of libraries for later use in analysis."
echo $n "# It may take a while."$c

rm -f nmout.all 2>/dev/null
#for NEC EWS4800 /bin/sh
case $HOSTTYPE in
  mips|ews_mips)
   touch nmout.all;;
  *)
esac

for libfile in $LIBFILES; do
  echo $n .$c
  nm $libfile 2>/dev/null >>nmout.all
done
grep printf <nmout.all >nmout.tmp

try_sed()
{
    echo $n .$c
    sedargs=''
    for arg do
	sedargs="$sedargs -e '$arg'"
    done
    eval sed -n $sedargs <nmout.tmp >nmout.list
    grep '^printf$' nmout.list >/dev/null 2>/dev/null
    return
}

try_sed 's/^.* [ATDSI]  *[_.]*//p' 's/^.* [ATDSI] //p' ||
try_sed 's/^__*//' 's/^\([a-zA-Z_0-9$]*\).*xtern.*/\1/p' ||
try_sed '/|UNDEF/d' '/FUNC..GL/s/^.*|__*//p' ||
try_sed 's/^.* D __*//p' 's/^.* D //p' ||
try_sed 's/^_//' 's/^\([a-zA-Z_0-9]*\).*xtern.*text.*/\1/p' ||
try_sed '/|COMMON/d' '/|DATA/d' '/ file/d' 's/^\([^     ]*\).*/\1/p' ||
try_sed '/|UNDEF/d' 's/^.*|FUNC |GLOB .*|//p' 's/^.*|FUNC |WEAK .*|//p' ||
try_sed 's/^.*|FUNC |GLOB .*|//p' 's/^.*|FUNC |WEAK .*|//p' ||
try_sed 's/^[         ]*[0-9][0-9a-f]*[       ]*Def. Text[    ]*//p' ||
try_sed 's/^.* [AT]  *_[_.]*//p' 's/^.* [AT] //p' ||
try_sed 's/^.*|Proc.*| //p' ||
give_up_config "nm didn't seem to work right or libraries are clobbered "

eval sed -n $sedargs <nmout.all >nmout.list

cat <<GAZONK
 done
#
# I now start testing availability of features I might use...

GAZONK

find_label ()
{
    grep "^$1\$" nmout.list >/dev/null 2>/dev/null
    return
}

define="#define"
undef="#undef"

test_label()
{
    testedlabel=$1
    macroname=$2
    if find_label $testedlabel; then
	echo "     found \"$testedlabel\""
	eval $macroname=\$define
	return 0
    else
	echo "     cannot find \"$testedlabel\"; I'll try without it."
	eval $macroname=\$undef
	return 1
    fi
}

test_either()
{
    onelabel=$1
    anotherlabel=$2
    macroname=$3
    test_label $onelabel $macroname ||
    test_label $anotherlabel DUMMY ||
    give_up_config "Can't find either $onelabel or $anotherlabel"
}

test_include()
{
    file=$1
    macro=$2
    shift
    if locate_file "$file" f $INCLUDEDIR; then
	echo "     found \"$file\""
	eval $macro=\$define
	return 0
    else
	echo "     cannot find \"$file\"; I'll try without it."
	eval $macro=\$undef
	return 1
    fi
}

test_include string.h STRINGH
test_include stddef.h STDDEFH

test_label setlinebuf SETLINEBUF
test_label lockf USELOCKF
test_label sigaction USESIG
test_label getrusage GETRUSAGE
test_label ulimit USEULIMIT
test_label getdtablesize USEGETDTABLESIZE
test_label setitimer USETIMER
test_label nrand48 USENRAND48
test_either bcmp memcmp USEBCMP
test_either bcopy memcpy USEBCOPY
test_either bzero memset USEBZERO
test_either strchr index USESTRCHR
test_either usleep sleep USEUSLEEP

if test "$XTERM" = ""; then
  USEXTERM="#undef XTERM"
else
  USEXTERM="#define XTERM \"$XTERM\""
fi

if locate_file stropts.h f $INCLUDEDIR; then
    USESTREAMINCLUDEDIR=$undef
else
    USESTREAMINCLUDEDIR=$define
fi

test_include sys/processor.h USEPROCBIND

cat <<GAZONK

# KLIC can run C compilers and other programs used for program compilation
# in parallel.  Maximum parallelism (maximum number of processes forked by
# the compiler driver) can be specified by -P option at compilation time.
#
# You can also use this feature during the installation procedure.
# Note that, too much parallelism may slow down the installation.
# I'd recommend using no parallelism on uniprocessor systems.

GAZONK
INSTPARALLEL=0
ask_with_default "default parallelism in installation" "INSTPARALLEL" || exit 1

echo ""
echo "# Writing out config.h file."
cat <<GAZONK >config.h
/*
  Copyright 1994, 1995 Institute for New Generation Computer Technology
  Read COPYRIGHT for detailed information

  KLIC System Configuration Setting
  This file was created by KLIC configuration script.
  Date of Confuguration: `date`
  Configured by: ${USER:-unknown}
*/

/* CPU name for locking shared-memory parallel */

#define $CPU

/* Directories for installation */

#define KLICBIN "$KLICBIN"
#define KLICLIB "$KLICLIB"
#define KLICINCLUDE "$KLICINCLUDE"
#define KLIC_COMPILER "$KLICLIB/klic/kl1cmp"
#define KLIC_DBMAKER "$KLICLIB/klic/klicdb"

/* Laguage and program processing systems */

#define CC "$CC"
#define LD "$LD"
#define RANLIB "$RANLIB"

/* Additional CC flags for optimized compilation of KL1 programs */

#define UOPTFLAGS "$UOPTFLAGS"

/* Additional flags for LD */

#define LIBRARIES "-lklic $LIBSWITCHES"
#define LIBRARIES_T "-lklict $LIBSWITCHES"
#define LIBRARIES_D "-lklicd $LIBSWITCHES"
#define LIBRARIES_S "-lklics $LIBSWITCHES"
#define KLIC_CC_OPTIONS "$KLICCFLAGS"
#define KLIC_LD_OPTIONS "$KLICLDFLAGS"

/* Usual C macros depending on availability */

$ASYNCIO ASYNCIO
$STRINGH STRINGH
$STDDEFH STDDEFH
$SETLINEBUF SETLINEBUF
$USELOCKF USELOCKF
$USESIG USESIG
$GETRUSAGE GETRUSAGE
$USEBCMP USEBCMP
$USEBCOPY USEBCOPY
$USEBZERO USEBZERO
$USESTRCHR USESTRCHR
$USEUSLEEP USEUSLEEP
$USEULIMIT USEULIMIT
$USEGETDTABLESIZE USEGETDTABLESIZE
$USETIMER USETIMER
$USENRAND48 NRAND48
$USESTREAMINCLUDEDIR USESTREAMINCLUDEDIR
$USEPROCBIND USEPROCBIND
$USEXTERM

#ifdef USEBCMP
#define BCMP(x,y,len)		bcmp(x,y,len)
#else
#define BCMP(x,y,len)		memcmp(x,y,len)
#endif

#ifdef USEBCOPY
#define BCOPY(from,to,len)	bcopy(from,to,len)
#else
#define BCOPY(from,to,len)	memcpy(to,from,len)
#endif

#ifdef USEBZERO
#define BZERO(from,len)		bzero(from,len)
#else
#define BZERO(from,len)		memset(from,0,len)
#endif

#ifdef USESTRCHR
#define STRCHR			strchr
#else
#define STRCHR			index
#endif
GAZONK
echo ' '

echo "# Writing out Makefile"
cat <<GAZONK >Makefile
# Copyright 1993, 1994, 1995 Institute for New Generation Computer Technology
# Read COPYRIGHT for detailed information
#
# Top-level Makefile for KLIC system
# This file was created by KLIC configuration script.
# Date of Confuguration: `date`
# Configured by: ${USER:-unknown}

########################################
# Directory Names
########################################

# Include file directory
KLICINCLUDE = $KLICINCLUDE

# Library directory
KLICLIB = $KLICLIB

# Executable directory
KLICBIN = $KLICBIN

########################################
# PVM Information
########################################

PVM_ROOT = $PVM_ROOT
PVM_ARCH = $PVM_ARCH
PVMLIB   = $PVMLIB

########################################
# Libraries to build
########################################

ARCHIVES = $ARCHIVES

########################################
# System-dependent switches
########################################

# LN: link command to use for system compilation.
LN = $LN

# KL1CMP: KL1 to C compiler used for compiling the self compiler.
# Only for system developers and not for normal users.
KL1CMP = $KL1CMP

# C compiler
CC = $CC
LD = $LD

# Optimization flags for compiling the runtime system by CC.
OPTLEVEL = $OPTLEVEL
OPTFLAGS = $OPTFLAGS
KLICOPT = $KLICOPT $DEBUGFLAGS
DEBUGFLAGS = $DEBUGFLAGS

# Additional CC flags for optimized compilation of KL1 programs.
UOPTFLAGS = $UOPTFLAGS

# Library search flags
LIBSWITCHES = $LIBSWITCHES

# Library archive indexer
RANLIB = $RANLIB

# Make program
MAKE = $MAKE

# Installer
INSTALL = $INSTALL
INSTDIR = $INSTDIR
INSTALLHDR = /bin/cp -p

# Maximum parallelism in compilation
PARALLEL = $INSTPARALLEL
 
GAZONK
cat <Makefile.tail >>Makefile ||
give_up_config "Can't create makefile properly"
echo ' '

mv config.h include/klic ||
give_up_config "Can't move config.h to an appropriate place"

clean_up_config

echo "# Writing out $def_setting"
rm -f $def_setting;mv config.tmp $def_setting
echo ' '

if $DISTSYSTEM; then
(cd runtime; LN=$LN ./setupcomm pvm)
fi

cat <<GAZONK

# All set!  Now you can compile the KLIC system by typing in "make all".
# If compilation is OK, I'd recommend testing it by running "make tests".
# You can then install the system by "make install".
#
# Good Luck!  Please report problems to "klic-bugs@icot.or.jp".

GAZONK

