#!/bin/sh
#$Id: configure,v 6.46 2003/08/01 19:06:25 cher Exp $

# Copyright (C) 1996-2003 Alexander Chernov <cher@ispras.ru>
# Created: Sat Dec  7 16:19:27 1996 by cher (Alexander Chernov) 

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.

write_help () {
  if [ -f ./etc/version.sh ]
  then
    . ./etc/version.sh
  else
    VERSION=unknown
  fi

[ -f ./etc/features.sh ] && . ./etc/features.sh

  cat <<EOF
This is configure utility (Rev. $CONFIGURE_REVISION) for ${PACKAGE} ${VERSION}.
Usage: configure [options] [host]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message
  --version               print the version of configure script
Directory and file names:
  --prefix=PREFIX         install all files to PREFIX
  --disable-wrappers      do not generate wrapper scripts, executables
                          are installed directly into bin
  --enable-runtime-libexec install the compiled binaries into
                          libexec/$\{CONFIG_RUNTIME\} directory instead of
                          libexec/$\{config\} directory.
Features and packages:
  --disable-FEATURE       do not include FEATURE (same as --enable-FEATURE=0)
  --enable-FEATURE[=ARG]  include FEATURE [ARG=1]
  --with-PACKAGE[=ARG]    use PACKAGE [ARG=1]
  --without-PACKAGE       do not use PACKAGE (same as --with-PACKAGE=0)
--enable and --with options recognized:
  --with-mingw            use Mingw32 instead of Cygwin32 on Windows
  --with-bcc              use Borland C/C++ compiler (NOT RECOMMENDED)
  --with-vc               use Visual C/C++ compiler
  --with-vc-proxy         use vc-proxy as compiler driver for VC`[ x"${configure_cocktail_tools}" != xno ] && echo '
  --with-cocktail[=PATH]  specify location of Cocktail executables
  --without-cocktail      disable use of Cocktail'`
  --disable-mode-files    do not generate/overwrite existing mode files
                          in etc/.cfg-$\{config\}/mode-*.make
  --disable-gen-remake    disable attempts to regenerate .c,.h files
                          make will fail if such files must be regenerated
  --disable-dep-remake    disable attempts to regenerate dependency files
                          such attempts will be ignored, no deps files
                          will be regenerated
  --disable-arch-gendir   disable configuration specific directory with
                          generated files. All files will be generated
                          in 'gen' directory. Use with caution.
  --disable-arch-depdir   disable configuration specific directory with
                          dependency files. All files will be generated
                          in 'dep' directory. Use with caution.
  --disable-arch-objdir   disable configuration specific directory with
                          object files. All object files will be in
                          'obj' and 'pic' directory. Use with caution.
  --with-full-path        always use full paths to access tools
  --enable-shared         enable generation of shared library, if the host
                          supports it
  --enable-rpath          hardcode the path to the shared libraries into
                          generated executables, if the host supports it
  --enable-debug          set the debugging mode as the default
  --with-reuse=PATH       specify path to REUSE library
  --enable-reuse-debug    use the REUSE library from source/reuse directory
                          instead of include and lib directories
  --enable-cvm-backend    compile-in the CVM backend
  --with-dmalloc          use dmalloc library`[ x"${configure_cpp}" != xno ] && echo '
  --enable-builtin-cpp    link C preprocessor library into C/C++ frontend
  --enable-cpp-debug      use the C preprocessor from source/c_cpp directory
                          instead of libexec directory'``[ x"${configure_cpp_option_list}" != xno ] && echo '
  --with-cpp-options=OPTS pass the given options to the cpp upon invokation'`
EOF
  exit 0
}

write_version () {
  if [ -f ./etc/version.sh ]
  then
    . ./etc/version.sh
  else
    VERSION=unknown
  fi

  cat <<EOF
This is configure utility (Rev. $CONFIGURE_REVISION).
The package ${PACKAGE} version is ${VERSION}.
EOF
  exit 0
}

cleanup() {
  rm -f ./aconf*
  rm -f ./ACONF*
}

tool_req_error() {
  echo "NO!"
  echo $1 is required >&2
  cleanup
  exit 1
}

# Attempts to find files specified as arguments in PATH search list
find_in_path() {
  fip_extra_path=
  if [ x"$1" = x-e ]
  then
    fip_extra_path=$2
    shift 2;
  fi
  find_args=$*
  find_result=
  [ x"${fip_extra_path}" != x ] && fip_extra_path="${fip_extra_path}${PATH_SEPARATOR}"
  fip_path="${fip_extra_path}${PATH}"
  fip_IFS="$IFS"
  for name in $find_args
  do
    IFS="${PATH_SEPARATOR}"
    for path in $fip_path
    do
      IFS="$fip_IFS"
      if [ -x "$path/$name" ]
      then
        [ x"${find_result}" != x ] && find_result="${find_result}${PATH_SEPARATOR}"
        find_result="${find_result}${path}/${name}"
      fi
      IFS="${PATH_SEPARATOR}"
    done
    IFS="$fip_IFS"
  done
}

# Possibly stripts
strip_full_path() {
  strip_args="$@"
  strip_result=
  sfp_IFS="$IFS"
  IFS="${PATH_SEPARATOR}"
  for fn in "$strip_args"
  do
    IFS="$sfp_IFS"
    fdn=`dirname "$fn"`
    fbn=`basename "$fn"`
    strip_flag=0
    if [ $option_full_path = 0 ]
    then
      IFS="${PATH_SEPARATOR}"
      for path in $PATH
      do
        IFS="$sfp_IFS"
        if [ -d "$fdn" -a -d "$path" -a x"$fdn" = x"$path" ]
        then
          [ x"${strip_result}" != x ] && strip_result="${strip_result}${PATH_SEPARATOR}"
          strip_result="${strip_result}${fbn}"
          strip_flag=1
          break
        fi
        if [ -x "$path/$fbn" ]
        then
          break
        fi
        IFS="${PATH_SEPARATOR}"
      done
      IFS="$sfp_IFS"
    fi
    if [ $strip_flag = 0 ]
    then
      [ x"${strip_result}" != x ] && strip_result="${strip_result}${PATH_SEPARATOR}"
      strip_result="${strip_result}${fn}"
    fi
    IFS="${PATH_SEPARATOR}"
  done
  IFS="$sfp_IFS"
}

# Get first argument from list
get_first() {
  gf_args="$@"
  gf_IFS="$IFS"
  IFS="${PATH_SEPARATOR}"
  set -- $gf_args
  first_result="$1"
  IFS="$gf_IFS"
}

# Look for particular utility
# $1 -- list of candidates
# $2 -- env. variable name
# $3 -- error message
# $4 -- additional configuration information
look_for_utility() {
  ch='(cached)'
  lfu_list=$1
  lfu_var=$2
  lfu_error=$3
  lfu_pretend_not_found=0
  lfu_extra_path=
  case x"$4" in
    x): ;;
    x0) lfu_pretend_not_found=1;;
    x1) : ;;
    *)  lfu_extra_path=$4$5;;
  esac
  eval 'lll=$'$lfu_var
  if [ x"$lll" = x -a ${lfu_pretend_not_found} = 0 ]
  then
    ch=
    find_in_path -e "${lfu_extra_path}" $lfu_list
    get_first "$find_result"
    strip_full_path "$first_result"
    lll=$strip_result
    eval $lfu_var'=$lll'
  fi
  if [ x"$lll" = x -a x"$lfu_error" != x -a x"$lfu_error" != x- -a x"$lfu_error" != x+ -a x"$lfu_error" != x@ ]
  then
    tool_req_error "$lfu_error"
  elif [ x"$lll" = x -a x"$lfu_error" = x- ]
  then
    echo "not found - disabled"
    lll='false'
    eval $lfu_var'=$lll'
  elif [ x"$lll" = x -a x"$lfu_error" = x@ ]
  then
    echo "not found - saved files will be used"
    lll='${'${home_var_name}'}/sources/tools/copy_gen '$1' -- $@ --'
    eval $lfu_var'=$lll'
  elif [ x"$lll" = x -a x"$lfu_error" = x+ ]
  then
    echo "not fount - skipped"
    lll='true'
    eval $lfu_var'=$lll'
  else
    echo ${lll:-"not found"}$ch
  fi
}

write_sh_variables() {
  for vv in $*
  do
# we want to print: [ x"${vv}" = x ] && ${vv}='${${vv}}'
    printf '[ x"${'${vv}'}" = x ] && '${vv}'='
    eval 'echo "'"'""$"${vv}"'"'"'
  done
}

write_make_variables() {
  for vv in $*
  do
    eval 'echo "'$vv'=$'$vv'"'
  done
}

# $1 - variable name
# $2 - variable type (string, int)
# $3 - 1, prune if empty
#      2, #undef, if 0
# $4 - value to print (optional)
write_c_config_var() {
  vv="$4"
  [ x"$vv" = x ] && eval vv='$'$1
  vv2="$vv"
  if [ x"$3" = x2 -a x"$vv" = x0 ]
  then
    echo "#undef $1" >> $cbhf
  elif [ x"$3" != x1 -o x"$vv" != x ]
  then
    if [ x"$2" = xstring ] 
    then
      vv='"'${vv}'"'
      vv2='\"'${vv2}'\"'
    fi
    echo "#define $1 $vv" >> $cbhf
    echo '  "'$1'", "'$vv2'",' >> $cbcf
  fi
}

write_mode_file() {
  ff=$1
  printf "Writing $ff..."
  echo "# compilation mode settings" > $ff
  echo "# Automatically generated by configure utility" >> $ff
  echo "# Generation date: "`date` >> $ff
  for ii in MODENAME \
    CFLAGS CXXFLAGS CGENFLAGS LDFLAGS LDLIBS \
    CMODEOPT CXXMODEOPT LDMODEOPT LIBMODEOPT \
    EXE_MODE LIB_MODE \
    ENABLE_LN OBJDIR PICDIR GENDIR DEPDIR LNS
  do
    write_make_variables $ii >> $ff
  done
  echo "done"
}

###########################################################################
###########################################################################
# Configure utility starts here

CONFIGURE_REVISION=`echo '$Revision: 6.46 $' | sed 's/^[^0-9]*\([0-9][0-9]*[.][0-9][0-9]*\)[^0-9]*$/\1/'`
if [ x"${CONFIGURE_VINTAGE}" = x ]
then
  CONFIGURE_VINTAGE=`echo "${CONFIGURE_REVISION}" | sed 's/\.//'`0
fi

# Character that separates entries in the PATH environment variable
[ x"${PATH_SEPARATOR}" = x ] && PATH_SEPARATOR=":"

option_full_path=0
option_mode_files=1
option_debug=0
saved_cmdline=$*

p_config_updated=0
p_config_guessed=1
p_home_updated=0
p_mode_updated=0
unsetlist=

while true
do
  case "$1" in
    --prefix=*)
        CONF_PREFIX=`echo "$1" | sed -e 's/--prefix=//'`
        shift;;
    --without-*)
        varname=option_`echo $1 | sed -e 's/--without-//' -e 's/-/_/g'`
        eval ${varname}'=0'
        #echo "Option: ${varname}=0"
        shift;;
    --with-*=*)
        varname=option_`echo $1 | sed -e 's/^--with-\([A-Za-z0-9_-][A-Za-z0-9_-]*\)=.*$/\1/' -e 's/-/_/g'`
        varvalue=`echo $1 | sed 's/^--with-[A-Za-z0-9_-][A-Za-z0-9_-]*=//'`
        eval ${varname}'=${varvalue}'
        #echo "Option: ${varname}=${varvalue}"
        shift;;
    --with-*)
        varname=option_`echo $1 | sed -e 's/--with-//' -e 's/-/_/g'`
        eval ${varname}'=1'
        #echo "Option: ${varname}=1"
        shift;;
    --enable-*=*)
        varname=option_`echo $1 | sed -e 's/^--enable-\([A-Za-z0-9_-][A-Za-z0-9_-]*\)=.*$/\1/' -e 's/-/_/g'`
        varvalue=`echo $1 | sed 's/^--enable-[A-Za-z0-9_-][A-Za-z0-9_-]*=//'`
        eval ${varname}'=${varvalue}'
        #echo "Option: ${varname}=${varvalue}"
        shift;;
    --enable-*)
        varname=option_`echo $1 | sed -e 's/--enable-//' -e 's/-/_/g'`
        eval ${varname}'=1'
        #echo "Option: ${varname}=1"
        shift;;
    --disable-*)
        varname=option_`echo $1 | sed -e 's/--disable-//' -e 's/-/_/g'`
        eval ${varname}'=0'
        #echo "Option: ${varname}=0"
        shift;;
    --help) write_help;;
    --version) write_version;;
    *) break;;
  esac
done

# Fix certain options
[ x"${option_reuse}" = x1 ] && option_reuse=
[ x"${option_reuse}" = x0 ] && option_reuse=
CONF_DISABLE_REUSE=0
[ x"${option_reuse}" != x ] && CONF_DISABLE_REUSE=1

config_name=
if [ x"$1" != x ]
then
  config_name=$1
  shift
fi

configure_args=$*
configure_path=$0
saved_IFS=$IFS

if [ x"${PROJECT_ENV_PREFIX}" = x ]
then
  if [ -f etc/env_prefix.sh ]; then :
  else
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
  . etc/env_prefix.sh
  if [ x"${PROJECT_ENV_PREFIX}" = x ]
  then
    echo "Cannot get project environment prefix" >&2
    exit 1
  fi
fi

home_var_name=${PROJECT_ENV_PREFIX}'_HOME'
config_var_name=${PROJECT_ENV_PREFIX}'_CONFIG'
mode_var_name=${PROJECT_ENV_PREFIX}'_MODE'
CONF_ENV_PREFIX=${PROJECT_ENV_PREFIX}

if [ -f ./etc/version.sh ]
then
  . ./etc/version.sh
else
  VERSION=unknown
fi
CONF_VERSION=${VERSION}

[ -f ./etc/features.sh ] && . ./etc/features.sh

IFS="${PATH_SEPARATOR}"
set -- $PATH
IFS=$saved_IFS
internal_path="$@"

IFS="._"
set -- $VERSION
v_major=$1
v_minor=$2
v_patch=$3
IFS=$saved_IFS
[ "$v_major" -ge 0 ] 2>/dev/null || v_major=0
[ "$v_minor" -ge 0 ] 2>/dev/null || v_minor=0
[ "$v_patch" -ge 0 ] 2>/dev/null || v_patch=0
if CONF_VERSION_CODE=`printf "%d%02d%02d" $v_major $v_minor $v_patch`
then
    CONF_VERSION_CODE=`expr ${CONF_VERSION_CODE} + 0`
    [ $? != 0 ] && unset CONF_VERSION_CODE
else
    unset CONF_VERSION_CODE
fi

set -- $configure_args

echo "Configure utility (Rev. $CONFIGURE_REVISION) for ${PACKAGE} ${VERSION}."
[ x"${CONF_VERSION_CODE}" != x ] && echo "Version code = ${CONF_VERSION_CODE}"

eval 'prj_home=$'${home_var_name}

#Detect the root of the installation home
if [ x"${prj_home}" = x ]
then
  dn=`dirname $configure_path`
  ddn=`cd $dn; pwd`
  eval ${home_var_name}=$ddn
else
  p1=`cd ${prj_home}; pwd`
  p2=`cd .; pwd`
  if [ x"$p1" != x"$p2" ]
  then
    if [ -t 0 -a -t 1 ]
    then
      echo
      echo "${home_var_name} environment variable is set to ${prj_home}."
      echo "This variable affects compilation and execution of tools."
      echo "The toolkit might use the current value of ${home_var_name},"
      echo "but it can set ${home_var_name} automatically (it's recommended)."
      printf "Set ${home_var_name} automatically?(y/n)[y]"
      while :
      do 
        read choice
        if [ x"$choice" = x ]
        then
          choice=1
          break
        elif [ x"$choice" = xy -o x"$choice" = xY ]
        then
          choice=1
          break
        elif [ x"$choice" = xn -o x"$choice" = xN ]
        then
          choice=0
          break
        fi
        printf "Please answer y or n:"
      done
      if [ x"${choice}" = x1 ]
      then
        p_home_updated=1
        eval ${home_var_name}=$p2
        export ${home_var_name}
      fi
    else
      # Don't ask user, just reset ${home_var_name}
      echo "${home_var_name} variable is set to $p1."
      echo "Current directory is $p2."
      echo "${home_var_name} is reset to $p2."
      echo "User was not prompted since input or output is not a tty."
      p_home_updated=1
      eval ${home_var_name}=$p2
      export ${home_var_name}
    fi
  fi
fi

eval 'prj_home=$'${home_var_name}
echo "${home_var_name} is ${prj_home}"

# Now we use new canonic configuration names (GNU compatible)
NEW_CONFIG=`./etc/config.guess`
if [ x"${NEW_CONFIG}" = xix86-win32-cygwin ]
then
  # Consider various --with-* options
  if [ x"${option_mingw}" = x1 ]
  then
    NEW_CONFIG='ix86-win32-mingw'
    CONFIG_RUNTIME='ix86-win32'
    p_config_guessed=0
  elif [ x"${option_bcc}" = x1 ]
  then
    NEW_CONFIG='ix86-win32-bcc'
    CONFIG_RUNTIME='ix86-win32'
    p_config_guessed=0
  elif [ x"${option_vc}" = x1 ]
  then
    NEW_CONFIG='ix86-win32-vc'
    CONFIG_RUNTIME='ix86-win32'
    p_config_guessed=0
  elif [ x"${option_vc_proxy}" = x1 ]
  then
    NEW_CONFIG='ix86-win32-vc-proxy'
    CONFIG_RUNTIME='ix86-win32'
    p_config_guessed=0
  fi
fi

[ x"${CONFIG_RUNTIME}" = x ] && CONFIG_RUNTIME="${NEW_CONFIG}"

if [ x"${config_name}" != x ]
then
  eval ${config_var_name}=${config_name}
  p_config_updated=1
fi
eval 'prj_config=$'${config_var_name}

if [ x"${prj_config}" != x -a x"${prj_config}" != x"${NEW_CONFIG}" ]
then
  if [ -t 0 -a -t 1 ]
  then
    echo
    echo "${config_var_name} environment variable is set to ${prj_config}."
    echo "${config_var_name} variable affects compilation and execution of tools."
    echo "The toolkit might use the current value of ${config_var_name},"
    echo "but it's recommended to allow to set it automatically."
    printf "Set ${config_var_name} automatically?(y/n)[y]"
    while :
    do
      read choice
      if [ x"$choice" = x ]
      then
        choice=1
        break
      elif [ x"$choice" = xy -o x"$choice" = xY ]
      then
        choice=1
        break
      elif [ x"$choice" = xn -o x"$choice" = xN ]
      then
        choice=0
        break
      fi
      printf "Please, answer y or n:"
    done
    if [ x"$choice" = x1 ]
    then
      eval ${config_var_name}=${NEW_CONFIG}
      p_config_updated=1
    else
      NEW_CONFIG=${prj_config}
    fi
  else
    eval ${config_var_name}=${NEW_CONFIG}
    p_config_updated=1
    echo "${config_var_name} environment variable reset to ${prj_config}."
    echo "User was not prompted, since input or output is not a tty."
  fi
else
  if [ ${p_config_guessed} = 0 -a x"${prj_config}" = x ]
  then
    p_config_updated=1
  fi
  eval ${config_var_name}=$NEW_CONFIG
fi

eval 'prj_config=$'${config_var_name}
echo "Configuration is ${prj_config}"

p_config2=`echo ${prj_config} | sed 's/\./_/g'`
IFS="-"
set -- $p_config2

CONF_CPU=$1
CONF_OS=$2

IFS=$saved_IFS

[ x${CONF_CPU} = x ] && CONF_CPU=unknown
[ x${CONF_OS} = x ]  && CONF_OS=unknown

# Make a guess about API type
case x"${prj_config}" in
  xix86-win32|xix86-win32-bcc|xix86-win32-vc|xix86-win32-vc-proxy|xix86-win32-mingw)
    # Win32 native API
    CONF_API_TYPE=win32
    # disable wrappers
    [ x"${option_wrappers}" = x ] && option_wrappers=0
    ;;
  xix86-msdos-djgpp)
    # DOS + 32 bit extender
    CONF_API_TYPE=dos
    # disable wrappers
    [ x"${option_wrappers}" = x ] && option_wrappers=0
    ;;
  *)
    CONF_API_TYPE=unix
    ;;
esac

echo "CPU is ${CONF_CPU}"
echo "OS is ${CONF_OS}"
echo "API type is ${CONF_API_TYPE}"

eval 'prj_mode=$'${mode_var_name}

if [ x"${prj_mode}" != x ]
then
  if [ -t 0 -a -t 1 ]
  then
    echo
    echo "${mode_var_name} environment variable is set to ${prj_mode}."
    echo "The variable affects compilation of the toolkit."
    echo "It's recommended to allow to set it automatically."
    printf "Set ${mode_var_name} automatically?(y/n)[y]"
    while :
    do
      read choice
      if [ x"$choice" = x ]
      then
        choice=1
        break
      elif [ x"$choice" = xy -o x"$choice" = xY ]
      then
        choice=1
        break
      elif [ x"$choice" = xn -o x"$choice" = xN ]
      then
        choice=0
        break
      fi
      printf "Please, answer y or n:"
    done
    if [ x"$choice" = x1 ]
    then
      p_mode_updated=1
      unset ${mode_var_name}
    fi
  else
    p_mode_updated=1
    unset ${mode_var_name}
    echo "${mode_var_name} environment variable unset."
    echo "User was not prompted, since input or output is not a tty."
  fi
fi

eval 'prj_mode=$'${mode_var_name}
echo "Compilation mode is: ${prj_mode:-not set}"

CONFIG_BASE=${prj_home}/etc/.cfg-${prj_config}
if [ ! -d $CONFIG_BASE ]
then
  if mkdir $CONFIG_BASE; then :
  else
    echo "cannot create directory $CONFIG_BASE" >&2
    exit 1
  fi
fi

echo "Configuring for configuration ${prj_config}"
echo "Runtime configuration is ${CONFIG_RUNTIME}"
if [ -f $CONFIG_BASE/compile.sh ]
then
  # Ask confirmation from the user (if we can read/or write to tty)
  if [ -t 0 -a -t 1 ]
  then
    # Ask user, since we're attached to terminal
    echo
    echo   "Existing configuration files $CONFIG_BASE are found"
    printf "Should these files be used?(y/n)[n]"
    while :
    do
      read choice
      if [ x"$choice" = x ]
      then
        choice=0
        break
      elif [ x"$choice" = xY -o x"$choice" = xy ]
      then
        choice=1
        break
      elif [ x"$choice" = xN -o x"$choice" = xn ]
      then
        choice=0
        break
      else
        printf "Please, type [y/n]"
      fi
    done
    if [ x"$choice" = x1 ]
    then
      . $CONFIG_BASE/compile.sh
    fi
  else
    # Don't ask user (he probably won't see our question)
    echo "Configuration files $CONFIG_BASE are ignored"
    echo "User was not prompted, since input or output is not a tty"
  fi
fi

CONF_NO_GEN_REMAKE=0
GENREM=
if [ x"${option_gen_remake}" = x0 ]
then
  CONF_NO_GEN_REMAKE=1
  GENREM=false
fi
CONF_NO_DEP_REMAKE=0
DEPREM=
if [ x"${option_dep_remake}" = x0 ]
then
  CONF_NO_DEP_REMAKE=1
  DEPREM='@ \#'
fi

CONF_NO_ARCH_GENDIR=0
CONF_NO_ARCH_DEPDIR=0
CONF_NO_ARCH_OBJDIR=0
[ x"${option_arch_gendir}" = x0 ] && CONF_NO_ARCH_GENDIR=1
[ x"${option_arch_depdir}" = x0 ] && CONF_NO_ARCH_DEPDIR=1
[ x"${option_arch_objdir}" = x0 ] && CONF_NO_ARCH_OBJDIR=1

CONF_MAKEDEPS_GENOPT=
[ x"${CONF_NO_ARCH_GENDIR}" = x1 -o x"${CONF_NO_GEN_REMAKE}" = x1 ] && CONF_MAKEDEPS_GENOPT=-G1
CONF_MAKEDEPS_DEPOPT=
[ x"${CONF_NO_ARCH_DEPDIR}" = x1 -o x"${CONF_NO_DEP_REMAKE}" = x1 ] && CONF_MAKEDEPS_DEPOPT=-J1

# Guess the typical executable file suffix
echo "Checking OS file naming convensions:"
case x"${prj_config}" in
  xix86-win32-bcc|xix86-win32-vc|xix86-win32-vc-proxy)
    EXESFX=.exe
    DIRSEP=\\\\
    PATHSEP=\\\;
    CONF_FSTOKEN=CONF_DOS_FILENAMING
    LIBPFX=
    LIBSFX=.lib
    DLLPFX=
# unfortunally, setting DLLSFX to .dll break makefiles
#    DLLSFX=.dll
    DLLSFX=
    OBJSFX=.obj
    ;;
  xix86-msdos-djgpp)
    EXESFX=.exe
    DIRSEP=/
    PATHSEP=\\\;
    CONF_FSTOKEN=CONF_DOS_FILENAMING
    LIBPFX=lib
    LIBSFX=.a
    DLLPFX=
    DLLSFX=
    OBJSFX=.o
    ;;
  xix86-win32|xix86-win32-cygwin|xix86-win32-mingw)
    EXESFX=.exe
    DIRSEP=\\\\
    PATHSEP=\\\;
    CONF_FSTOKEN=CONF_DOS_FILENAMING
    LIBPFX=lib
    LIBSFX=.a
    DLLPFX=
# unfortunally, setting DLLSFX to .dll break makefiles
#    DLLSFX=.dll
    DLLSFX=
    OBJSFX=.o
    ;;
  *)
    EXESFX=
    DIRSEP=/
    PATHSEP=:
    CONF_FSTOKEN=CONF_UNIX_FILENAMING
    LIBPFX=lib
    LIBSFX=.a
    DLLPFX=lib
    DLLSFX=.so
    OBJSFX=.o
    ;;
esac

echo "  library name prefix:    ${LIBPFX}"
echo "  library name suffix:    ${LIBSFX}"
echo "  dynamic library prefix: ${DLLPFX}"
echo "  dynamic library suffix: ${DLLSFX}"
echo "  executable file suffix: ${EXESFX}"
echo "  object file suffix:     ${OBJSFX}"
echo "  directory separator:    ${DIRSEP}"
echo "  path separator:         ${PATHSEP}"
echo "  compilation token:      ${CONF_FSTOKEN}"

# Look for C compiler
printf "Looking for C compiler..."
ch='(cached)'
if [ x"$CC" = x ]
then
  ch=
  case x"${prj_config}" in
    xix86-win32-bcc)      compiler_list='bcc32';;
    xix86-win32-vc)       compiler_list='cl';;
    xix86-win32-vc-proxy) compiler_list='cl-proxy';;
    *)                    compiler_list='gcc c89 cc';;
  esac
  find_in_path $compiler_list
  get_first "$find_result"
  strip_full_path "$first_result"
  CC="$strip_result"
  if [ x"$CC" = x ]; then tool_req_error "C compiler"; fi
fi
echo "$CC"$ch

# Look for C++ compiler
printf "Looking for C++ compiler..."
ch='(cached)'
if [ x"$CXX" = x ]
then
  ch=
  case x"${prj_config}" in
    xix86-win32-bcc)      compiler_list='bcc32';;
    xix86-msdos-djgpp)    compiler_list='gpp';;
    xix86-win32-vc)       compiler_list='cl';;
    xix86-win32-vc-proxy) compiler_list='cl-proxy';;
    *)                    compiler_list='g++ c++';;
  esac
  find_in_path $compiler_list
  get_first "$find_result"
  strip_full_path "$first_result"
  CXX="$strip_result"
  [ x"$CXX" = x ] && tool_req_error "C++ compiler"
fi
echo "$CXX"$ch

# Look for C linker
printf "Looking for C linker..."
ch='(cached)'
if [ x"${CLD}" = x ]
then
  ch=
  case x"${prj_config}" in
    xix86-win32-vc)  CLD='${'${home_var_name}'}/sources/tools/vcld.sh';;
    xix86-win32-bcc) CLD='${'${home_var_name}'}/sources/tools/bccld.sh';;
    *) CLD='${CC}';;
  esac
fi
echo "${CLD}${ch}"

# Look for C++ linker
printf "Looking for C++ linker..."
ch='(cached)'
if [ x"${CXXLD}" = x ]
then
  ch=
  case x"${prj_config}" in
    xix86-win32-vc)  CXXLD='${'${home_var_name}'}/sources/tools/vcld.sh';;
    xix86-win32-bcc) CXXLD='${'${home_var_name}'}/sources/tools/bccld.sh';;
    *) CXXLD='${CXX}';;
  esac
fi
echo "${CXXLD}${ch}"

# Set CGLOBAL for the specified compiler
if [ x"$CGLOBAL" = x ]
then
  if [ x"$CC" = xc89 -a x"$CONF_OS" = xhpux ]
  then
    # Known case of weirdness of C compiler
    CGLOBAL="-D_INCLUDE_POSIX_SOURCE"
    CGLOBAL="${CGLOBAL} -D_INCLUDE_HPUX_SOURCE"
    CGLOBAL="${CGLOBAL} -D_INCLUDE_POSIX2_SOURCE"
    CGLOBAL="${CGLOBAL} -D_INCLUDE_XOPEN_SOURCE"
  elif [ x"${prj_config}" = xix86-win32-mingw ]
  then
    # Use special switch
    CGLOBAL="-mno-cygwin"
  elif [ x"${prj_config}" = xix86-win32-vc ]
  then
    CGLOBAL="-nologo -D_POSIX_"
  elif [ x"${prj_config}" = xix86-win32-vc-proxy ]
  then
    CGLOBAL="-D_POSIX_"
  elif [ x"${prj_config}" = xix86-win32-bcc ]
  then
    CGLOBAL="-w-pro -w-rch -w-par -w-aus -D_POSIX_"
  fi
  [ x"$CGLOBAL" != x ] && echo "CGLOBAL variable is set automatically to ${CGLOBAL}"
else
  echo "CGLOBAL variable is set by user to ${CGLOBAL}, will obey it"
fi

# Set CXXGLOBAL for the specified compiler
if [ x"$CXXGLOBAL" = x ]
then
  if [ x"${prj_config}" = xix86-win32-mingw ]
  then
    CXXGLOBAL="-mno-cygwin"
  elif [ x"${prj_config}" = xix86-win32-vc ]
  then
    CXXGLOBAL="-nologo -D_POSIX_"
  elif [ x"${prj_config}" = xix86-win32-vc-proxy ]
  then
    CGLOBAL="-D_POSIX_"
  elif [ x"${prj_config}" = xix86-win32-bcc ]
  then
    CXXGLOBAL="-w-pro -w-rch -w-par -w-aus -D_POSIX_"
  fi
  [ x"$CXXGLOBAL" != x ] && echo "CXXGLOBAL variable is set automatically to ${CXXGLOBAL}"
else
  echo "CXXGLOBAL variable is set by user to ${CXXGLOBAL}, will obey it"
fi

# Set LDGLOBAL for the specified compiler
if [ x"$LDGLOBAL" = x ]
then
  if [ x"${prj_config}" = xix86-win32-mingw ]
  then
    LDGLOBAL="-mno-cygwin"
  elif [ x"${prj_config}" = xix86-win32-cygwin ]
  then
    LDGLOBAL=
  elif [ x"${prj_config}" = xix86-win32-vc ]
  then
    LDGLOBAL="-nologo"
  fi
  [ x"$LDGLOBAL" != x ] && echo "LDGLOBAL variable is set automatically to ${LDGLOBAL}"
else
  echo "LDGLOBAL variable is set by user to ${LDGLOBAL}, will obey it"
fi

# Set CXXGLOBAL for the specified compiler
if [ x"$LIBGLOBAL" = x ]
then
  case x"${prj_config}" in
    xix86-win32-mingw)
      LIBGLOBAL=;;
    xix86-win32-*)
      LIBGLOBAL=;;
    *)
      LIBGLOBAL=-lm;;
  esac
  [ x"$LIBGLOBAL" != x ] && echo "LIBGLOBAL variable is set automatically to ${LIBGLOBAL}"
else
  echo "LIBGLOBAL variable is set by user to ${LIBGLOBAL}, will obey it"
fi
RCC="${CC} ${CGLOBAL} ${LDGLOBAL} ${LIBGLOBAL}"
RCXX="${CXX} ${CXXGLOBAL} ${LDGLOBAL} ${LIBGLOBAL}"

# Set temporary files name
if [ x"${prj_config}" = xix86-win32-bcc ]
then
  cf=aconf
  cf_s=aconf.c
  cf_c=aconf.c
  cf_o=aconf.obj
  cf_e=aconf.exe
  cf_x=./aconf.exe
  EXE_OUTPUT_FLAG='-e'
  OBJ_OUTPUT_FLAG='-o'
elif [ x"${prj_config}" = xix86-win32-vc ]
then
  cf=aconf
  cf_s=aconf.c
  cf_c=aconf.c
  cf_o=aconf.obj
  cf_e=aconf.exe
  cf_x=./aconf.exe
  EXE_OUTPUT_FLAG='-Fe'
  OBJ_OUTPUT_FLAG='-Fo'
  RCC=${RCC}' -Fo'${cf_o}
elif [ x"${prj_config}" = xix86-win32-vc-proxy  ]
then
  cf=aconf
  cf_s=aconf.c
  cf_c=aconf.c
  cf_o=aconf.obj
  cf_e=aconf.exe
  cf_x=./aconf.exe
  EXE_OUTPUT_FLAG='-o '
  OBJ_OUTPUT_FLAG='-o '
else
  cf=aconf
  cf_s=aconf.c
  cf_c=aconf.c
  cf_o=aconf.o
  cf_e=aconf
  cf_x=./aconf
  EXE_OUTPUT_FLAG='-o '
  OBJ_OUTPUT_FLAG='-o '
fi

# Check it for ANSI compliance (in the first look)
if [ x"$ch" = x ]
then
  printf "Checking whether it compiles ANSI C..."
  cat <<EOF >$cf_s
#include <stdio.h>
int main(int argc, char *argv[], char *envp) { return 0; }
EOF
  $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c 2>/dev/null >/dev/null
  rr=$?
  $cf_x
  rr=$rr"-"$?
  if [ "$rr" != "0-0" ]; then tool_req_error "ANSI C compiler"; fi
  echo "yes"
fi

if [ x"${configure_cpp_option_list}" != xno ]
then
  if [ x"${option_cpp_options}" != x -a x"${option_cpp_options}" != x0 -a x"${option_cpp_options}" != x1 ]
  then
    echo "${option_cpp_options}"
    # Use the given set of preprocessor options
    for i in ${option_cpp_options}
    do
      case x"$i" in
        x-D*)
           cpp_settings="${cpp_settings},"'"'$i'"'
           ;;
        x-I*)
           cpp_settings="${cpp_settings},"'"'$i'"'
           ;;
        *) echo "unknown cpp option '$i'" >& 2
           cleanup
           exit 1
           ;;
      esac
    done
    echo "Standard preprocessor options: ${cpp_settings}"
  else
    cpp_settings=""
    printf "Checking for GNU C compiler..."
    cat >$cf_s <<EOF
int main() {
#ifdef __GNUC__
  return 0;
#else
  return 1;
#endif
}
EOF
    if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c 2>/dev/null >/dev/null
    then
      if $cf_x
      then
        echo "yes"
        echo "Extracting standard preprocessor defines..."
        #if [ x"${prj_config}" = xix86-msdos-djgpp ]
        #then
        #redir -e $cf.t -o nul $CC -v $cf.c -o $cf
        #else
        $CC -v $cf.c -o $cf 2>$cf.t >/dev/null
        #fi
	case x"${prj_config}" in
          *solaris*) mygrep=/usr/xpg4/bin/grep;;
          *) mygrep=grep;;
        esac
        ${mygrep} -E "cpp(${EXESFX})? -lang-c" < $cf.t > $cf.tt
        set -- `cat $cf.tt`
        for i in $*
        do
          case $i in
            -D*) cpp_settings=${cpp_settings}'"'$i'",';;
          esac
        done
        grep -n '#include <...>' < $cf.t | sed 's/^\([0-9][0-9]*\):.*$/\1/' > $cf.st 
        grep -n 'End of search list.' < $cf.t | sed 's/^\([0-9][0-9]*\):.*$/\1/' > $cf.en 
        nstart=`cat $cf.st`; 
        nend=`cat $cf.en`;
        if [ x"$nstart" != x -a x"$nend" != x ]
        then
          nstart=`expr $nstart + 1`
          nend=`expr $nend - 1`
          sed "$nstart,$nend p; d" < $cf.t >$cf.u
          set -- `cat $cf.u`
          for i in $*
          do
            cpp_settings=${cpp_settings}'"-I'$i'",'
          done
        else
          echo "Cannot extract list of include directories" >&2
        fi
        echo $cpp_settings
      else
        echo "no"
        CONF_LANG_C_CPP_OPTIONS_LIST=
      fi
    else
      echo "Failed to compile a simple program!"
    fi
  fi # option_cpp_options not set
  CONF_LANG_C_CPP_OPTIONS_LIST=`echo "${cpp_settings}" | sed 's-\\\\-\\\\\\\\-g'`
fi

# Now set support for shared libraries (for Linux)
if [ x"${CONF_OS}" = xlinux -a x"${option_shared}" = x1 ]
then
  CONF_DLL=1
  CDLLFLAGS=-fPIC
  CXXDLLFLAGS=-fPIC
  LDDLLFLAGS='-shared -Wl,-soname=${SHARED_SONAME}'
  CONF_RPATH=
  if [ x"${option_rpath}" = x1 ]
  then
    if [ x"${option_reuse}" != x ]
    then
      CONF_RPATH="${CONF_RPATH}"' -Wl,-rpath,${INST_LIBEXEC}:'"${option_reuse}/lib/${prj_config}"
    elif [ x"${option_reuse_debug}" = x1 ]
    then
      CONF_RPATH="${CONF_RPATH}"' -Wl,-rpath,../reuse/${REALO}'
    elif [ x"${CONF_PREFIX}" != x -a "${CONF_PREFIX}" != "${prj_home}" ]
    then
      CONF_RPATH="${CONF_RPATH}"' -Wl,-rpath,'"${CONF_PREFIX}/libexec/${prj_config}"
    else
      CONF_RPATH="${CONF_RPATH}"' -Wl,-rpath,${INST_LIBEXEC}'
    fi
  fi
  DLLIBS=
elif [ x"${option_shared}" = x1 -a x"${prj_config}" = xix86-win32-mingw ]
then
  CONF_DLL=2
  CDLLFLAGS=
  CXXDLLFLAGS=
  LDDLLFLAGS=
  CONF_RPATH=
  DLLIBS=
elif [ x"${option_shared}" = x1 -a x"${prj_config}" = xix86-win32-cygwin ]
then
  CONF_DLL=2
  CDLLFLAGS=
  CXXDLLFLAGS=
  LDDLLFLAGS=
  CONF_RPATH=
  DLLIBS=
else
  CONF_DLL=0
  CLLFLAGS=
  CXXDLLFLAGS=
  LDDLLFLAGS=
  CONF_RPATH=
  DLLLIBS=
fi

# Look for sed utility
printf "Looking for sed utility..."
look_for_utility "sed" SED "sed utility"

# Look for awk utility
printf "Looking for awk utility..."
case x"$CONFIG_OS_NAME" in
  hpux) look_for_utility "awk gawk" AWK "awk utility" ;;
  *)    look_for_utility "gawk nawk awk" AWK "awk utility" ;;
esac

# Look for ar utility
printf "Looking for ar utility..."
if [ x"${prj_config}" = xix86-win32-bcc ]
then
  AR='${'${home_var_name}'}/sources/tools/bcclib.sh'
  echo $AR
elif [ x"${prj_config}" = xix86-win32-vc ]
then
  AR='${'${home_var_name}'}/sources/tools/vclib.sh'
  echo $AR
elif [ x"${prj_config}" = xix86-win32-vc-proxy ]
then
  AR='cl-proxy'
  echo $AR
else
  look_for_utility "ar" AR "ar utility"
fi

# Set options to create library (to be configurable)
if [ x"${prj_config}" = xix86-win32-vc-proxy ]
then
  ARFLAGS='-mlib -o'
else
  ARFLAGS=rcu
fi

if [ x"${configure_bison_tools}" != xno ]
then
  # Look for bison utility
  printf "Looking for bison utility..."
  look_for_utility "bison" BISON "@"

  # Look for flex utility
  printf "Looking for flex utility..."
  look_for_utility "flex" FLEX "@"
fi

if [ x"${configure_cocktail_tools}" != xno ]
then
  # Look for rex utility
  printf 'Looking for rex utility (Cocktail)...'
  look_for_utility "rex" REX "@" ${option_cocktail} /bin

  # Look for rpp utility
  printf 'Looking for rpp utility (Cocktail)...'
  look_for_utility "rpp" RPP "@" ${option_cocktail} /bin

  # Look for lalr utility
  printf 'Looking for lalr utility (Cocktail)...'
  look_for_utility "lalr" LALR "@" ${option_cocktail} /bin

  # Look for ell tool
  printf 'Looking for ell utility (Cocktail)...'
  look_for_utility "ell" ELL "@" ${option_cocktail} /bin

  # Look for cg tool
  printf 'Looking for cg tool (Cocktail)...'
  look_for_utility "cg" CG "@" ${option_cocktail} /bin

  # Look for ast tool
  printf 'Looking for ast tool (Cocktail)...'
  look_for_utility "ast" AST "@" ${option_cocktail} /bin

  # Look for ag tool
  printf 'Looking for ag tool (Cocktail)...'
  look_for_utility "ag" AG "@" ${option_cocktail} /bin

  # Look for puma tool
  printf 'Looking for puma tool (Cocktail)...'
  look_for_utility "puma" PUMA "@" ${option_cocktail} /bin
fi # [ x"${configure_cocktail_tools}" != xno ]

if [ x"${configure_purify_tools}" != xno ]
then
  # Checking purify & quantify
  printf "Looking for purify utility..."
  look_for_utility "purify"   PURIFY   ""
  printf "Looking for quantify utility..."
  look_for_utility "quantify" QUANTIFY ""
  printf "Looking for purecov utility..."
  look_for_utility "purecov" PURECOV ""
fi # [ x"${configure_purify_tool}" != x0 ]

# Checking endianness of the host
ch='(cached)'
printf "Checking byte order..."
if [ x"$CONF_ENDIAN" = x ]
then
  ch=
  cat <<EOF > $cf_s
char c[4] = {1, 2, 3, 4};
int *p = (int*) c;
int main()
{
  if (*p == 0x01020304) return 1;
  if (*p == 0x04030201) return 2;
  return 0;
}
EOF
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c >/dev/null 2>&1; then :
  else
    echo "cannot compile or link a program" >&2
    exit 1
  fi
  $cf_x
  CONF_ENDIAN=$?
fi

case x"$CONF_ENDIAN" in
  x1) echo "big endian${ch}";;
  x2) echo "little endian${ch}";;
  *)  echo "unknown byte order${ch}" >&2; cleanup; exit 1;;
esac

# Check size of char
ch='(cached)'
printf "Checking size of char..."
if [ "$CONF_CHAR_BITS" = "" ]
then
  ch=
  cat > $cf_s <<EOF
unsigned char tt = 1;
int main() { int bits=0; for (; tt != 0; tt <<= 1, bits++); return bits; }
EOF
  $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c  1>/dev/null 2>&1
  if [ $? != 0 ]
  then
    echo "Cannot compile or link a program" >&2
    exit 1
  fi
  $cf_x
  CONF_CHAR_BITS=$?
fi
echo "${CONF_CHAR_BITS}${cachstr}"
  
# Check size of short
ch='(cached)'
printf "Checking size of short..."
if [ "$CONF_SHORT_BITS" = "" ]
then
  ch=
  cat > $cf_s <<EOF
unsigned short tt = 1;
int main() { int bits=0; for (; tt != 0; tt <<= 1, bits++); return bits; }
EOF
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c  1>/dev/null 2>&1
  if [ $? != 0 ]
  then
    echo "Cannot compile or link a program" >&2
    exit 1
  fi
  $cf_x
  CONF_SHORT_BITS=$?
fi
echo "${CONF_SHORT_BITS}${cachstr}"
  
# Check size of int
ch='(cached)'
printf "Checking size of int..."
if [ "$CONF_INT_BITS" = "" ]
then
  ch=
  cat > $cf_s <<EOF
unsigned int tt = 1;
int main() { int bits=0; for (; tt != 0; tt <<= 1, bits++); return bits; }
EOF
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  1>/dev/null 2>&1
  if [ $? != 0 ]
  then
    echo "Cannot compile or link a program" >&2
    exit 1
  fi
  $cf_x
  CONF_INT_BITS=$?
fi
echo "${CONF_INT_BITS}${cachstr}"
  
# Check size of long
ch='(cached)'
printf "Checking size of long..."
if [ "$CONF_LONG_BITS" = "" ]
then
  ch=
  cat > $cf_s <<EOF
unsigned long tt = 1;
int main() { int bits=0; for (; tt != 0; tt <<= 1, bits++); return bits; }
EOF
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  1>/dev/null 2>&1
  if [ $? != 0 ]
  then
    echo "Cannot compile or link a program" >&2
    exit 1
  fi
  $cf_x
  CONF_LONG_BITS=$?
fi
echo "${CONF_LONG_BITS}${cachstr}"
  
# Check support for 64 bit integers
ch='(cached)'
printf "Checking for 64 bit long long type..."
if [ x"$CONF_LONGLONG" = x ]
then
  ch=''
  CONF_LONGLONG=0
  echo 'int main() { unsigned long long q = 1; int r = 0; for (; q != 0; q <<= 1, r++); return r; }' > $cf_s
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  >/dev/null 2>&1 
  then
    $cf_x
    rc=$?
    [ $rc = 64 ] && CONF_LONGLONG=1
    CONF_LONG_LONG_BITS=$rc
  fi
fi

[ x"$CONF_LONGLONG" != x1 ] && CONF_LONGLONG=0
case x"$CONF_LONGLONG" in
  x1) echo "ok"; CONF_HAS_INT64=1; CONF_INT64_TYPE="long long";;
  *) echo "not supported";;
esac

# Visual C++ has __int64 type
ch='(cached)'
printf "Checking for 64-bit __int64 type..."
if [ x"${CONF___INT64}" = x ]
then
  ch=''
  CONF___INT64=0
  echo 'int main() { unsigned __int64 q = 1; int r = 0; for (; q != 0; q <<= 1, r++); return r; }' > $cf_s
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  >/dev/null 2>&1 
  then
    $cf_x
    rc=$?
    [ $rc = 64 ] && CONF___INT64=1
    CONF___INT64_BITS=$rc
  fi
fi

[ x"${CONF___INT64}" != x1 ] && CONF___INT64=0
case x"${CONF___INT64}" in
  x1) echo "ok"; CONF_HAS_INT64=1; CONF_INT64_TYPE="__int64";;
  *) echo "not supported";;
esac

# How to specify 64-bit integral constants
if [ x"${CONF_HAS_INT64}" = x1 -a x"${CONF_INT64_SUFFIX}" = x ]
then
  while :
  do
    printf "Checking that LL suffix works..."
    CONF_INT64_SUFFIX="ll"
    echo "int main() { ${CONF_INT64_TYPE} z = 0x0102030405060708ll; return 0; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    printf "Checking that i64 suffix works..."
    CONF_INT64_SUFFIX="i64"
    echo "int main() { ${CONF_INT64_TYPE} z = 0x0102030405060708i64; return 0; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    CONF_INT64_SUFFIX=
    break
  done

  while :
  do
    printf "Checking that ULL suffix works..."
    CONF_UINT64_SUFFIX="ull"
    echo "int main() { unsigned ${CONF_INT64_TYPE} z = 0x0102030405060708ull; return 0; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    printf "Checking that ui64 suffix works..."
    CONF_UINT64_SUFFIX="ui64"
    echo "int main() { ${CONF_INT64_TYPE} z = 0x0102030405060708ui64; return 0; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    CONF_UINT64_SUFFIX=
    break
  done

  while :
  do
    printf 'Checking that %%lld format works...'
    CONF_INT64_FORMAT=ll
    echo "int main() { char buf[128]; ${CONF_INT64_TYPE} x = 0x1122334455667788${CONF_INT64_SUFFIX}; sprintf(buf, "'"%llx"'", x); if (!strcmp(buf,"'"1122334455667788"'")) return 0; return 1; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    printf 'Checking that %%I64d format works...'
    CONF_INT64_FORMAT=I64
    echo "int main() { char buf[128]; ${CONF_INT64_TYPE} x = 0x1122334455667788${CONF_INT64_SUFFIX}; sprintf(buf, "'"%I64x"'", x); if (!strcmp(buf,"'"1122334455667788"'")) return 0; return 1; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    printf 'Checking that %%Ld format works...'
    CONF_INT64_FORMAT=L
    echo "int main() { char buf[128]; ${CONF_INT64_TYPE} x = 0x1122334455667788${CONF_INT64_SUFFIX}; sprintf(buf, "'"%Lx"'", x); if (!strcmp(buf,"'"1122334455667788"'")) return 0; return 1; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    printf 'Checking that %%qd format works...'
    CONF_INT64_FORMAT=q
    echo "int main() { char buf[128]; ${CONF_INT64_TYPE} x = 0x1122334455667788${CONF_INT64_SUFFIX}; sprintf(buf, "'"%qx"'", x); if (!strcmp(buf,"'"1122334455667788"'")) return 0; return 1; }" > ${cf_s}
    $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && $cf_x && echo "Ok" && break
    echo "no"

    CONF_INT64_FORMAT=
    break
  done
else
  CONF_INT64_SUFFIX=
  CONF_UINT64_SUFFIX=
  CONF_INT64_FORMAT=
fi

# Check that there is a way to print 64-bit integers via printf
CONF_PRINTF_INT64=0
[ x"${CONF_INT64_FORMAT}" != x ] && CONF_PRINTF_INT64=1

# Check that printf's "%lld" specifier works
CONF_PRINTF_LLD_WORKS=0
[ x"${CONF_INT64_FORMAT}" = xll ] && CONF_PRINTF_LLD_WORKS=1

# Check that printf's "%a" specifier works
CONF_PRINTF_A_WORKS=0
printf 'Checking that printf("%%a",...) works...'
echo 'int main() { double x = 0.5; char buf[32]; sprintf(buf, "%a", x); return strcmp(buf, "0x1p-1"); }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_PRINTF_A_WORKS=1
if [ x"${CONF_PRINTF_A_WORKS}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

# Check that double and long double are the same types
ch='(cached)'
printf "Checking that long double is actually the same as double..."
if [ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" = x ]
then
  CONF_LONG_DOUBLE_IS_DOUBLE=0
  echo 'int main() { double x = 0.1; long double y = 0.1; if (sizeof(x) == sizeof(y)) return 0; return 1; }' > ${cf_s}
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_LONG_DOUBLE_IS_DOUBLE=1
fi
[ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" != x1 ] && CONF_LONG_DOUBLE_IS_DOUBLE=0
case x"${CONF_LONG_DOUBLE_IS_DOUBLE=0}" in
  x1) echo "yes";;
  *) echo "no";;
esac

# Check that printf's "%Lg" specifier works
CONF_PRINTF_LG_WORKS=0
if [ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" != x1 ]
then
  printf 'Checking that printf("%%Lg",...) works...'
  echo 'int main() { char buf[32]; long double z = 0.1L; sprintf(buf, "%Lg", z); return strcmp(buf, "0.1"); }' > ${cf_s}
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_PRINTF_LG_WORKS=1
  if [ x"${CONF_PRINTF_LG_WORKS}" = x1 ]
  then
    echo "yes"
  else
    echo "no"
  fi
fi

CONF_USE_SYSTEM_PRINTF=0
printf "Checking that system printf is suitable..."
tt="${CONF_PRINTF_LLD_WORKS}${CONF_PRINTF_A_WORKS}${CONF_LONG_DOUBLE_IS_DOUBLE}${CONF_PRINTF_LG_WORKS}"
[ x"${tt}" = x1101 ] && CONF_USE_SYSTEM_PRINTF=1
[ x"${tt}" = x1110 ] && CONF_USE_SYSTEM_PRINTF=1
if [ x"${CONF_USE_SYSTEM_PRINTF}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_HAS_STRTOLD=0
if [ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" != x1 ]
then
  printf "Checking strtold function..."
  echo 'long double strtold(char const *, char **); int main() { long double x = 0.0L; x = strtold("0.5", 0); return x != 0.5L; }' > ${cf_s}
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_HAS_STRTOLD=1
  if [ x"${CONF_HAS_STRTOLD}" = x1 ]
  then
    echo "yes"
  else
    echo "no"
  fi
fi

CONF_HAS_STRTOF=0
printf "Checking strtof function..."
echo 'float strtof(char const *, char **); int main() { float x = 0.0f; x = strtof("0.5", 0); return x != 0.5f; }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_HAS_STRTOF=1
if [ x"${CONF_HAS_STRTOF}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_STRTOD_ACCEPTS_HEX=0
printf "Checking whether strtod accepts hexadecimal floats..."
echo 'double strtod(char const *, char **); int main() { double x = 0.0; x = strtod("0x1p-1", 0); return x != 0.5; }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_STRTOD_ACCEPTS_HEX=1
if [ x"${CONF_STRTOD_ACCEPTS_HEX}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_SCANF_LLD_WORKS=0
printf 'Checking that scanf("%%lld", ...) works...'
echo 'int sscanf(char const *, char const *, ...); int main() { '"${CONF_INT64_TYPE}"' x = 0; if (sscanf("1122334455667788", "%llx", &x) == 1 && x == 0x1122334455667788'"${CONF_INT64_SUFFIX}"') return 0; return 1;}' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_SCANF_LLD_WORKS=1
if [ x"${CONF_SCANF_LLD_WORKS}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_SCANF_INT64_WORKS=0
if [ x"${CONF_INT64_FORMAT}" != x ]
then
  printf 'Checking that scanf("%%'${CONF_INT64_FORMAT}'d", ...) works...'
echo 'int sscanf(char const *, char const *, ...); int main() { '"${CONF_INT64_TYPE}"' x = 0; if (sscanf("1122334455667788", "%'"${CONF_INT64_FORMAT}"'x", &x) == 1 && x == 0x1122334455667788'"${CONF_INT64_SUFFIX}"') return 0; return 1;}' > ${cf_s}
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_SCANF_INT64_WORKS=1
  if [ x"${CONF_SCANF_INT64_WORKS}" = x1 ]
  then
    echo "yes"
  else
    echo "no"
  fi
fi

CONF_SCANF_A_WORKS=0
printf 'Checking that scanf("%%a", ...) works...'
echo 'int sscanf(char const *, char const *, ...); int main() { double x = 0.0; if (sscanf("0x1p-1", "%la", &x) == 1 && x == 0.5) return 0; return 1;}' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_SCANF_A_WORKS=1
if [ x"${CONF_SCANF_A_WORKS}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_SCANF_LG_WORKS=0
if [ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" != x1 ]
then
  printf 'Checking that scanf("%%Lg", ...) works...'
  echo 'int sscanf(char const *, char const *, ...); int main() { long double x = 0.0L; if (sscanf("0.5", "%Lg", &x) == 1 && x == 0.5L) return 0; return 1;}' > ${cf_s}
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_SCANF_LG_WORKS=1
  if [ x"${CONF_SCANF_LG_WORKS}" = x1 ]
  then
    echo "yes"
  else
    echo "no"
  fi
fi

CONF_USE_SYSTEM_SCANF=0
printf "Checking that system scanf is suitable..."
tt="${CONF_SCANF_LLD_WORKS}${CONF_SCANF_A_WORKS}${CONF_LONG_DOUBLE_IS_DOUBLE}${CONF_SCANF_LG_WORKS}"
[ x"${tt}" = x1101 ] && CONF_USE_SYSTEM_SCANF=1
[ x"${tt}" = x1110 ] && CONF_USE_SYSTEM_SCANF=1
if [ x"${CONF_USE_SYSTEM_SCANF}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_HAS_STRCASECMP=0
printf "Checking strcasecmp function..."
echo 'int main() { return strcasecmp("aA", "Aa"); }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_HAS_STRCASECMP=1
if [ x"${CONF_HAS_STRCASECMP}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_HAS_STRNCASECMP=0
printf "Checking strncasecmp function..."
echo 'int main() { return strncasecmp("aAb", "Aac", 2); }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_HAS_STRNCASECMP=1
if [ x"${CONF_HAS_STRNCASECMP}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

CONF_HAS_STRNLEN=0
printf "Checking strnlen function..."
echo 'int main() { return strnlen("aAbc", 2) != 2; }' > ${cf_s}
$RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1 && ${cf_x} && CONF_HAS_STRNLEN=1
if [ x"${CONF_HAS_STRNLEN}" = x1 ]
then
  echo "yes"
else
  echo "no"
fi

if [ x"$CONF_LONGLONG" = x1 ]
then
  # Check that the platform supports strtoull, strtoll
  ch='(cached)'
  printf "Checking for strtoll function..."
  if [ x"$CONF_HAS_STRTOLL" = x ]
  then
    ch=''
    CONF_HAS_STRTOLL=0
    cat <<EOF > $cf_s
#include <stdlib.h>
int main() {
  unsigned long long q;
  char val[] = "0x1234567887654321";
  char *p;
  q = strtoll(val, &p, 0);
  if (q != 0x1234567887654321LL) return 1;
  if (*p) return 1;
  return 0;
}
EOF
    if $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  >/dev/null 2>&1 
    then
      if $cf_x; then CONF_HAS_STRTOLL=1; fi
    fi
  fi
  [ x"$CONF_HAS_STRTOLL" != x1 ] && CONF_HAS_STRTOLL=0
  case x"$CONF_HAS_STRTOLL" in
    x1) echo "yes";;
    *)  echo "no";;
  esac

  ch='(cached)'
  printf "Checking for strtoull function..."
  if [ x"$CONF_HAS_STRTOULL" = x ]
  then
    ch=''
    CONF_HAS_STRTOULL=0
    cat <<EOF > $cf_s
#include <stdlib.h>
int main() {
  unsigned long long q;
  char val[] = "0x1234567887654321";
  char *p;
  q = strtoull(val, &p, 0);
  if (q != 0x1234567887654321LL) return 1;
  if (*p) return 1;
  return 0;
}
EOF
    if $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c}  >/dev/null 2>&1 
    then
      if $cf_x; then CONF_HAS_STRTOULL=1; fi
    fi
  fi
  [ x"$CONF_HAS_STRTOULL" != x1 ] && CONF_HAS_STRTOULL=0
  case x"$CONF_HAS_STRTOULL" in
    x1) echo "yes";;
    *)  echo "no";;
  esac
fi

[ x"${CONF_HAS_STRTOLL}" != x1 ] && CONF_HAS_STRTOLL=0
[ x"${CONF_HAS_STRTOULL}" != x1 ] & CONF_HAS_STRTOULL=0

# Check that #include concatenates strings
ch='(cached)'
printf "Checking that #include concatenates strings..."
if [ x"${CONF_INCLUDE_CONCAT}" = x ]
then
  ch=
  cat >${cf_s} <<EOF
#ifndef __FOO__
#define __FOO__
#define I(X) X ".c"
#include I("./aconf")
int main() { return 0; }
#endif
EOF
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} ${cf_c} >/dev/null 2>&1
  then
    CONF_INCLUDE_CONCAT=1
  fi
fi
[ x"${CONF_INCLUDE_CONCAT}" != x1 ] && CONF_INCLUDE_CONCAT=0
case ${CONF_INCLUDE_CONCAT} in
  0) echo "no"${ch}
     GENDIR_INCLUDE='-I${GENDIR} -I${DEPDIR}'
     ;;
  1) echo "yes"${ch}
     GENDIR_INCLUDE=
     ;;
esac

# Check whether static initializers for FILE are allowed
ch='(cached)'
printf "Checking that std* streams are constant expressions..."
if [ x"${CONF_FILE_STATIC_INIT}" = x ]
then
  ch=
cat >${cf_s} <<EOF
#include <stdio.h>
FILE *f = stdin;
int main() { return 0; }
EOF
  CONF_FILE_STATIC_INIT=0
  if $RCC ${EXE_OUTPUT_FLAG}$cf_e $cf_c  > /dev/null 2>&1 && $cf_x >/dev/null 2>&1
  then
    CONF_FILE_STATIC_INIT=1
  fi
fi
[ x"${CONF_FILE_STATIC_INIT}" != x1 ] && CONF_FILE_STATIC_INIT=0
case ${CONF_FILE_STATIC_INIT} in
  0) echo "no"${ch};;
  1) echo "yes"${ch};;
esac

# Check snprintf function
ch='(cached)'
printf "Looking for snprintf and vsnprintf..."
if [ x"$CONF_HAS_SNPRINTF" = x ]
then
  cat <<EOF >$cf_s
int main() {
  char buf[32];
  snprintf(buf, 32, "%d", 333);
  return 0;
}
EOF
  CONF_HAS_SNPRINTF=0
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c >/dev/null 2>&1 && $cf_x 2>/dev/null
  then
    CONF_HAS_SNPRINTF=1
  fi
fi
[ x"$CONF_HAS_SNPRINTF" != x1 ] && CONF_HAS_SNPRINTF=0
case $CONF_HAS_SNPRINTF in
  0) echo "no";;
  1) echo "yes";;
esac

# Check _snprintf function
ch='(cached)'
printf "Looking for _snprintf and _vsnprintf..."
if [ x"$CONF_HAS__SNPRINTF" = x ]
then
  cat <<EOF >$cf_s
int main() {
  char buf[32];
  _snprintf(buf, 32, "%d", 333);
  return 0;
}
EOF
  CONF_HAS__SNPRINTF=0
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c >/dev/null 2>&1 && $cf_x 2>/dev/null
  then
    CONF_HAS__SNPRINTF=1
  fi
fi
[ x"$CONF_HAS__SNPRINTF" != x1 ] && CONF_HAS__SNPRINTF=0
case $CONF_HAS__SNPRINTF in
  0) echo "no";;
  1) echo "yes";;
esac

# Check mmap function
ch='(cached)'
printf "Looking for mmap..."
if [ x"$CONF_HAS_MMAP" = x ]
then
  cat <<EOF >$cf_s
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
  int   fd;
  void *addr;

  if ((fd = open("/dev/zero", O_RDWR)) < 0) return 1;
  if (!(addr = mmap(0, 1024, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0))) return 1;
  *(int*) addr = 0x12345678;
  if (munmap(addr, 1024) < 0) return 1;
  if (close(fd) < 0) return 1;
  return 0;
}
EOF
  CONF_HAS_MMAP=0
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c >/dev/null 2>&1 && $cf_x 2>/dev/null
  then
    CONF_HAS_MMAP=1
  fi
fi
[ x"$CONF_HAS_MMAP" != x1 ] && CONF_HAS_MMAP=0
case $CONF_HAS_MMAP in
  0) echo "no";;
  1) echo "yes";;
esac

# Check that sscanf "%n" works
ch='(cached)'
printf "Checking that sscanf works..."
if [ x"$CONF_SSCANF_WORKS" = x ]
then
  cat <<EOF >$cf_s
int main() { 
  int r, n = 0, d = 0;
  r = sscanf("32", "%d %n", &d, &n);
  return d == 32 && r == 1 && n == 2;
}  
EOF
  CONF_SSCANF_WORKS=0
  if $RCC ${EXE_OUTPUT_FLAG}$cf_e $cf_c  >/dev/null 2>&1
  then
    $cf_x
    CONF_SSCANF_WORKS=$?
  fi
fi
[ x"$CONF_SSCANF_WORKS" != x1 ] && CONF_SSCANF_WORKS=0
case $CONF_SSCANF_WORKS in
  1) echo "yes";;
  0) echo "no";;
esac

# Check libsocket.a and libnsl.a libraries
if [ x"${CONF_API_TYPE}" = xdos ]
then
  SOCKETLIBS=
  CONF_HAS_SOCKET=0
elif [ x"${CONF_API_TYPE}" != xwin32 ]
then
  ch='(cached)'
  printf "Checking for socket and nsl library..."
  CONF_HAS_SOCKET=1
  if [ x"$SOCKETLIBS" = x ]
  then
    ch=
    cat <<EOF > $cf.c
#include <sys/types.h>
#include <sys/socket.h>
main(){ int sd=socket(PF_INET, SOCK_STREAM, AF_INET); }
EOF
    $RCC $cf.c -c -o $cf.o > $cf.log 2>&1
    if [ $? != 0 ]
    then
      echo "This system lacks BSD-compatible implementation of sockets" >&2
      echo "Cannot compile on this system" >&2
      cat $cf.log
      cleanup
      exit 1
    fi
    SOCKETLIBS='-lsocket -lnsl'
    $RCC $cf.c -o $cf -lsocket -lnsl >/dev/null 2>&1
    if [ $? != 0 ]
    then
      SOCKETLIBS='-lsocket'
      $RCC $cf.c -o $cf -lsocket >/dev/null 2>&1
      if [ $? != 0 ]
      then
        SOCKETLIBS='-lnsl'
        $RCC $cf.c -o $cf -lnsl >/dev/null 2>&1
        if [ $? != 0 ]
        then
          SOCKETLIBS=' '
          $RCC $cf.c -o $cf >/dev/null 2>&1
          if [ $? != 0 ]
          then
            echo "cannot compile socket-using application" >&2
            echo "socket implementation broken?" >&2
            cleanup
            exit 1
          fi
        fi
      fi
    fi
  fi
  if [ x"$SOCKETLIBS" = x" " ]
  then
    msg='(not needed)'
  else
    msg=$SOCKETLIBS
  fi
  echo $msg$ch
else
  # API type is Win32
  ch='(cached)'
  printf "Checking WinSock library..."
  CONF_HAS_SOCKET=1
  if [ x"${SOCKETLIBS}" = x ]
  then
    ch=
    if [ x"${prj_config}" = xix86-win32-mingw ]
    then
      SOCKETLIBS=-lwsock32
      echo "${SOCKETLIBS}"
    else
      # FIXME: feature test?
      echo "(not needed)"
    fi
  else
    echo "${SOCKETLIBS}${ch}"
  fi
fi

# Check alloca function
ch='(cached)'
printf "Looking for alloca function..."
if [ x"$CONF_HAS_ALLOCA" = x ]
then
  ch=
  cat <<EOF > $cf_s
int main() { alloca(128); }
EOF
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c >/dev/null 2>&1
  if [ $? != 0 ]
  then
    CONF_HAS_ALLOCA=0
  else
    CONF_HAS_ALLOCA=1
  fi
fi
case "$CONF_HAS_ALLOCA" in
  0) echo "missed${ch}";;
  1) echo "found${ch}";;
  *) echo "Fatal error"; cleanup; exit 1;;
esac

# Check _alloca function
ch='(cached)'
printf "Looking for _alloca function..."
if [ x"$CONF_HAS__ALLOCA" = x ]
then
  ch=
  cat <<EOF > $cf_s
int main() { _alloca(128); }
EOF
  $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c >/dev/null 2>&1
  if [ $? != 0 ]
  then
    CONF_HAS__ALLOCA=0
  else
    CONF_HAS__ALLOCA=1
  fi
fi
case "$CONF_HAS__ALLOCA" in
  0) echo "missed${ch}";;
  1) echo "found${ch}";;
  *) echo "Fatal error"; cleanup; exit 1;;
esac

# Check strerror()
printf "Looking for strerror..."
if [ x"$CONF_HAS_STRERROR" = x ]
then
  cat > $cf_s <<EOF
int strerror();
int main() { strerror(); }
EOF
  if $RCC ${EXE_OUTPUT_FLAG}${cf_e} $cf_c > /dev/null 2>&1
  then
    CONF_HAS_STRERROR=1
  else
    CONF_HAS_STRERROR=0
  fi
fi
case x"$CONF_HAS_STRERROR" in
  x0) echo "no";;
  x1) echo "yes";;
  *)  echo "Fatal error" 1>&2; cleanup; exit 1;;
esac

# Check strerror in <string.h>
printf "Looking for strerror in <string.h>..."
if [ x"$CONF_HAS_STRERROR_IN_STRING_H" = x ]
then
  cat > $cf_s <<EOF
#include <string.h>
void strerror(void);
int main() { strerror(); }
EOF
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c > /dev/null 2>&1
  then
    CONF_HAS_STRERROR_IN_STRING_H=0
  else
    CONF_HAS_STRERROR_IN_STRING_H=1
  fi
fi
case x"$CONF_HAS_STRERROR_IN_STRING_H" in
  x0) echo "no";;
  x1) echo "yes";;
  *)  echo "Fatal error" 1>&2; cleanup; exit 1;;
esac

# Check sys_errlist and sys_nerr
printf "Looking for sys_errlist and sys_nerr..."
if [ x"$CONF_HAS_SYS_ERRLIST" = x ]
then
  cat > $cf_s <<EOF
#include <stdio.h>

extern int sys_nerr;
extern char *sys_errlist[];

int main() { printf("%d %d\n", sys_nerr, (int) sys_errlist); }
EOF
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c > /dev/null 2>&1
  then
    CONF_HAS_SYS_ERRLIST=1
  else
    CONF_HAS_SYS_ERRLIST=0
  fi
fi
case x"$CONF_HAS_SYS_ERRLIST" in
  x0) echo "no";;
  x1) echo "yes";;
  *)  echo "Fatal error" 1>&2; cleanup; exit 1;;
esac

# Check _sys_errlist and _sys_nerr
printf "Looking for _sys_errlist and _sys_nerr..."
if [ x"$CONF_HAS__SYS_ERRLIST" = x ]
then
  cat > $cf_s <<EOF
#include <stdio.h>

extern int _sys_nerr;
extern char *_sys_errlist[];

int main() { printf("%d %d\n", _sys_nerr, (int) _sys_errlist); }
EOF
  if $RCC ${EXE_OUTPUT_FLAG}$cf_e $cf_c > /dev/null 2>&1
  then
    CONF_HAS__SYS_ERRLIST=1
  else
    CONF_HAS__SYS_ERRLIST=0
  fi
fi
case x"$CONF_HAS__SYS_ERRLIST" in
  x0) echo "no";;
  x1) echo "yes";;
  *)  echo "Fatal error" 1>&2; cleanup; exit 1;;
esac

# Check fchdir function
printf "Looking for fchdir ..."
if [ x"${CONF_HAS_FCHDIR}" = x ]
then
  cat > $cf_s <<EOF
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main() {
  int fd = open(".", O_RDONLY);
  if (fd < 0) return 1;
  if (chdir("/") < 0) return 1;
  if (fchdir(fd) < 0) return 1;
  return 0;
}
EOF
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c >/dev/null 2>&1 && $cf_x
  then
    CONF_HAS_FCHDIR=1
  fi
fi
[ x"${CONF_HAS_FCHDIR}" != x1 ] && CONF_HAS_FCHDIR=0
case ${CONF_HAS_FCHDIR} in
  0) echo "no";;
  1) echo "yes";;
esac

if [ x"${configure_asm_stuff}" != xno ]
then
  # Check support for __attribute__((alias("name")))
  printf "Checking support for __attribute__((alias()))..."
  if [ x"$CONF_ATTRIBUTE_ALIAS" = x ]
  then
    cat > $cf_s <<EOF
static int bar() __attribute__((alias("foo")));
static int foo() { return 0; }
int main() { return bar(); }
EOF
    if $RCC ${EXE_OUTPUT_FLAG}$cf_e $cf_c > /dev/null 2>&1
    then
      CONF_ATTRIBUTE_ALIAS=1
    else
      CONF_ATTRIBUTE_ALIAS=0
    fi
  fi
  case x"$CONF_ATTRIBUTE_ALIAS" in
    x0) echo "no";;
    x1) echo "yes";;
    *)  echo "Fatal error" 1>&2; cleanup; exit 1;;
  esac

  if [ x"${CONF_ATTRIBUTE_ALIAS}" = x1 ]
  then
    # Check whether __attribute__((alias("0"))) works
    printf "checking whether __attribute__((alias(\"0\"))) works..."
    if [ x"${CONF_ATTRIBUTE_ALIAS_0}" = x ]
    then
      cat > ${cf_s} <<EOF
static int bar() __attribute__((alias("0")));
int main() { return (int) (bar != 0); }
EOF
      if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c >/dev/null 2>&1 && $cf_x
      then
        CONF_ATTRIBUTE_ALIAS_0=1
      fi
    fi
    [ x"${CONF_ATTRIBUTE_ALIAS_0}" != x1 ] && CONF_ATTRIBUTE_ALIAS_0=0
    case x"${CONF_ATTRIBUTE_ALIAS_0}" in
      x0) echo "no";;
      x1) echo "yes";;
    esac
  else
    CONF_ATTRIBUTE_ALIAS_0=0
  fi # [ x"${CONF_ATTRIBUTE_ALIAS}" = x1 ]

  # Checking whether any assembler name can be assigned to a C name
  printf "checking for asm(\"name\") construct..."
  if [ x"${CONF_DECLARATION_ASM}" = x ]
  then
    cat > ${cf_s} <<EOF
static int bar() asm("foo");
static int foo() asm("foo");
static int foo() { return 0; }
int main() { return foo(); }
EOF
    if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c >/dev/null 2>&1 && $cf_x
    then
      CONF_DECLARATION_ASM=1
    fi
  fi
  [ x"${CONF_DECLARATION_ASM}" != x1 ] && CONF_DECLARATION_ASM=0
  case ${CONF_DECLARATION_ASM} in
    0) echo "no";;
    1) echo "yes";;
  esac
  if [ x"${CONF_DECLARATION_ASM}" = x1 ]
  then
    # Checking asm("0x0")
    printf "checking for asm(\"0x0\") construct..."
    if [ x"${CONF_DECLARATION_ASM_0}" = x ]
    then
      cat > ${cf_s} <<EOF
static int bar() asm("0x0");
int main() { return (&bar != 0); }
EOF
      if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c
      then
        CONF_DECLARATION_ASM_0=1
      fi
    fi
    [ x"${CONF_DECLARATION_ASM_0}" != x1 ] && CONF_DECLARATION_ASM_0=0
    case ${CONF_DECLARATION_ASM_0} in
      0) echo "no";;
      1) echo "yes";;
    esac
  else
    CONF_DECLARATION_ASM_0=0
  fi
fi # [ x"${configure_asm_stuff}" != xno ]

# Check support for __attribute__((constructor))
printf "Checking support for __attribute__((constructor))..."
ch='(cached)'
if [ x"${CONF_ATTRIBUTE_CONSTRUCTOR}" = x ]
then
  cat > $cf_s <<EOF
static int init() __attribute__((constructor));
static int init() { exit(0); }
int main() { return 1; }
EOF
  ch=
  CONF_ATTRIBUTE_CONSTRUCTOR=0
  if $RCC $EXE_OUTPUT_FLAG$cf_e $cf_c > /dev/null 2>&1 && $cf_x >/dev/null 2>&1
  then
    CONF_ATTRIBUTE_CONSTRUCTOR=1
  fi
fi
[ x"${CONF_ATTRIBUTE_CONSTRUCTOR}" != x1 ] && CONF_ATTRIBUTE_CONSTRUCTOR=0
case ${CONF_ATTRIBUTE_CONSTRUCTOR} in
  0) echo "no"${ch};;
  1) echo "yes"${ch};;
esac

if [ x"${configure_cpp}" != xno ]
then
  CONF_BUILTIN_CPP=0
  if [ x"${option_builtin_cpp}" = x1 ]
  then
    CONF_BUILTIN_CPP=1
  fi

  CONF_CPP_DEBUG=0
  if [ x"${option_cpp_debug}" = x1 ]
  then
    CONF_CPP_DEBUG=1
  fi
fi # [ x"${configure_cpp}" != xno ]

lprefix='${'${home_var_name}'}'
[ x"${CONF_PREFIX}" = x ] && CONF_PREFIX="${lprefix}"
[ x"${CONF_WRAPPERS}" = x ] && CONF_WRAPPERS=1

# internal configuration
printf "Configuring internal variables..."
[ x"$MAKEDEPS"     = x ] && MAKEDEPS='${'${home_var_name}'}/sources/tools/${REALO}/cdeps'
[ x"$INSTALL"        = x ] && INSTALL='${'${home_var_name}'}/sources/tools/instfiles'
[ x"$MAKEBNUM"       = x ] && MAKEBNUM='${'${home_var_name}'}/sources/tools/BuildNumber'
[ x"$MKWRAPPER"     = x ] && MKWRAPPER='${'${home_var_name}'}/sources/tools/mkwrapper.sh'

[ x"$INST_INCL"      = x ] && INST_INCL=${lprefix}'/include'
[ x"$INST_ARCH_INCL" = x ] && INST_ARCH_INCL=${lprefix}'/include/${'${config_var_name}'}'
[ x"$INST_SHARE"     = x ] && INST_SHARE=${lprefix}'/share'
[ x"$INST_LIB"       = x ] && INST_LIB=${lprefix}'/lib/${'${config_var_name}'}'
if [ x"${INST_LIBEXEC}" = x ]
then
  if [ x"${option_wrappers}" = x0 ]
  then
    CONF_WRAPPERS=0 
    INST_LIBEXEC=${lprefix}'/bin'
  elif [ x"${option_runtime_libexec}" = x1 ]
  then
    INST_LIBEXEC=${lprefix}'/libexec/'${CONFIG_RUNTIME}
  else
    INST_LIBEXEC=${lprefix}'/libexec/${'${config_var_name}'}'
  fi
fi
[ x"$INST_BIN"       = x ] && INST_BIN=${lprefix}'/bin'
echo "done"

# Do some linux-specific stuff
case x"${prj_config}" in
  xi?86-linux-libc5)
    CONF_LINUX_LIBC_VERSION=5
    ;;
  xi?86-linux-aout)
    CONF_LINUX_LIBC_VERSION=4
    ;;
  xi?86-linux)
    CONF_LINUX_LIBC_VERSION=6
    ;;
esac

if [ x"${option_cvm_backend}" = x1 ]
then
  CONF_CVM_BACKEND=1
else
  CONF_CVM_BACKEND=0
fi

if [ x"${option_dmalloc}" = x1 ]
then
  CONF_USE_DMALLOC=1
else
  CONF_USE_DMALLOC=0
fi

if [ x"${option_reuse}" != x ]
then
  [ x"${CGLOBAL}" != x ] && CGLOBAL="${CGLOBAL} "
  CGLOBAL="${CGLOBAL} -I${option_reuse}/include"
  [ x"${CXXGLOBAL}" != x ] && CXXGLOBAL="${CXXGLOBAL} "
  CXXGLOBAL="${CXXGLOBAL} -I${option_reuse}/include"
  [ x"${LDGLOBAL}" != x ] && LDGLOBAL="${LDGLOBAL} "
  LDGLOBAL="${LDGLOBAL} -L${option_reuse}/lib/${prj_config}"
elif [ x"${option_reuse_debug}" != x ]
then
  [ x"${CGLOBAL}" != x ] && CGLOBAL="${CGLOBAL} "
#  CGLOBAL="${CGLOBAL}"'-I../reuse/.gen-${'${config_var_name}'} -I../reuse'
  CGLOBAL="${CGLOBAL}"' -I../reuse'
  [ x"${CXXGLOBAL}" != x ] && CXXGLOBAL="${CXXGLOBAL} "
#  CXXGLOBAL="${CXXGLOBAL}"'-I../reuse/.gen-${'${config_var_name}'} -I../reuse'
  CXXGLOBAL="${CXXGLOBAL}"' -I../reuse'
  [ x"${LDGLOBAL}" != x ] && LDGLOBAL="${LDGLOBAL} "
  LDGLOBAL="${LDGLOBAL}"'-L../reuse/${REALO}'
fi

# Write new configuration to configuration files
cbsh=$CONFIG_BASE/compile.sh
printf "Writing $cbsh..."
echo '# Project configuration file (shell script)' > $cbsh
echo "# Automatically generated by configure utility" >> $cbsh
echo "# Generation date: "`date` >> $cbsh
for ii in CONF_CPU CONF_OS CONF_API_TYPE \
  CONFIGURE_VINTAGE \
  CONFIG_RUNTIME \
  EXE_OUTPUT_FLAG OBJ_OUTPUT_FLAG \
  CC CXX CLD CXXLD SED AWK AR ARFLAGS \
  `[ x"${configure_bison_tools}" != xno ] && echo BISON FLEX` \
  `[ x"${configure_cocktail_tools}" != xno ] && echo REX RPP LALR ELL CG AST AG PUMA` \
  `[ x"${configure_purify_tools}" != xno ] && echo PURIFY QUANTIFY PURECOV` \
  CONF_NO_GEN_REMAKE GENREM CONF_NO_DEP_REMAKE DEPREM \
  CONF_NO_ARCH_GENDIR CONF_NO_ARCH_DEPDIR CONF_NO_ARCH_OBJDIR \
  CONF_MAKEDEPS_GENOPT CONF_MAKEDEPS_DEPOPT CONF_DISABLE_REUSE \
  SOCKETLIBS \
  `[ x"${configure_cpp}" != xno ] && echo CONF_BUILTIN_CPP CONF_CPP_DEBUG` \
  CONF_INCLUDE_CONCAT CONF_HAS_ALLOCA CONF_HAS__ALLOCA \
  CONF_HAS_FCHDIR CONF_HAS_SOCKET \
  CONF_HAS_STRERROR CONF_HAS_STRERROR_IN_STRING_H \
  CONF_HAS_SYS_ERRLIST CONF_HAS__SYS_ERRLIST \
  CONF_ATTRIBUTE_CONSTRUCTOR \
  `[ x"${configure_asm_stuff}" != xno ] && echo CONF_ATTRIBUTE_ALIAS CONF_ATTRIBUTE_ALIAS_0 CONF_DECLARATION_ASM CONF_DECLARATION_ASM_0` \
  CONF_HAS_STRTOLL CONF_HAS_STRTOULL \
  CONF_FILE_STATIC_INIT CONF_HAS_SNPRINTF CONF_HAS__SNPRINTF CONF_HAS_MMAP \
  EXESFX OBJSFX DIRSEP PATHSEP LIBPFX LIBSFX DLLPFX DLLSFX CONF_FSTOKEN \
  CONF_LONGLONG CONF_LONG_LONG_BITS CONF___INT64 CONF___INT64_BITS \
  CONF_INT64_SUFFIX CONF_UINT64_SUFFIX CONF_INT64_FORMAT \
  CONF_LONG_DOUBLE_IS_DOUBLE \
  CONF_ENDIAN CONF_SSCANF_WORKS \
  CONF_CHAR_BITS CONF_SHORT_BITS CONF_INT_BITS CONF_LONG_BITS \
  MAKEDEPS INSTALL MAKEBNUM MKWRAPPER \
  CONF_CVM_BACKEND CONF_USE_DMALLOC \
  CGLOBAL CXXGLOBAL LDGLOBAL LIBGLOBAL \
  CONF_DLL CDLLFLAGS CXXDLLFLAGS LDDLLFLAGS DLLLIBS CONF_RPATH \
  CONF_PREFIX CONF_WRAPPERS \
  INST_LIB INST_SHARE INST_BIN INST_INCL INST_ARCH_INCL INST_LIBEXEC
do
  write_sh_variables $ii >> $cbsh
done
echo "done"

cbrt=$CONFIG_BASE/runtime.sh
printf "Writing $cbrt..."
echo '# Project configuration file (shell script)' > $cbrt
echo "# Automatically generated by configure utility" >> $cbrt
echo "# Generation date: "`date` >> $cbrt
write_sh_variables AWK >> $cbrt
write_sh_variables CONFIG_RUNTIME >> $cbrt
write_sh_variables CONF_PREFIX >> $cbrt
write_sh_variables CONF_WRAPPERS >> $cbrt
echo "done"

cbmk=$CONFIG_BASE/config.make
printf "Writing $cbmk..."
echo '# Project configuration file (makefile)' > $cbmk
echo "# Automatically generated by configure utility" >> $cbmk
echo "# Generation date: "`date` >> $cbmk
for ii in \
  CONF_VERSION \
  CONFIGURE_VINTAGE \
  CONF_API_TYPE \
  CONFIG_RUNTIME \
  `[ x"${configure_cpp}" != xno ] && echo CONF_BUILTIN_CPP` \
  EXE_OUTPUT_FLAG OBJ_OUTPUT_FLAG \
  CC CXX CLD CXXLD SED AWK AR ARFLAGS \
  `[ x"${configure_bison_tools}" != xno ] && echo BISON FLEX` \
  `[ x"${configure_cocktail_tools}" != xno ] && echo REX RPP LALR ELL CG AST AG PUMA` \
  `[ x"${configure_purify_tools}" != xno ] && echo PURIFY QUANTIFY PURECOV` \
  CONF_NO_GEN_REMAKE GENREM CONF_NO_DEP_REMAKE DEPREM \
  CONF_NO_ARCH_GENDIR CONF_NO_ARCH_DEPDIR CONF_NO_ARCH_OBJDIR \
  CONF_MAKEDEPS_GENOPT CONF_MAKEDEPS_DEPOPT CONF_DISABLE_REUSE \
  GENDIR_INCLUDE \
  SOCKETLIBS \
  EXESFX OBJSFX DIRSEP PATHSEP LIBPFX LIBSFX DLLPFX DLLSFX \
  MAKEDEPS INSTALL MAKEBNUM MKWRAPPER \
  CONF_CVM_BACKEND CONF_USE_DMALLOC \
  CGLOBAL CXXGLOBAL LDGLOBAL LIBGLOBAL \
  CONF_DLL CDLLFLAGS CXXDLLFLAGS LDDLLFLAGS DLLLIBS CONF_RPATH \
  CONF_PREFIX CONF_WRAPPERS \
  INST_LIB INST_SHARE INST_BIN INST_INCL INST_ARCH_INCL INST_LIBEXEC
do
  write_make_variables $ii >> $cbmk
done
echo "done"

cbhf=$CONFIG_BASE/p_config.h
cbcf=${CONFIG_BASE}/p_config.c
printf "Writing $cbhf..."
echo '/* Project configuration file (.h file) */' > $cbhf
echo "/* Automatically generated by configure utility */" >> $cbhf
echo "/* Generation date: "`date`" */" >> $cbhf
echo "#ifndef __P_CONFIG_H__" >> $cbhf
echo "#define __P_CONFIG_H__" >> $cbhf
echo >> $cbhf
echo '/* Project configuration file (.c file) */' > $cbcf
echo "/* Automatically generated by configure utility */" >> $cbcf
echo "/* Generation date: "`date`" */" >> $cbcf
echo >> $cbcf
echo "static char const * const p_config_values[] =" >> $cbcf
echo "{" >> $cbcf
write_c_config_var CONF_ENV_PREFIX  string 0
write_c_config_var CONF_VERSION     string 0
write_c_config_var CONF_CANONIC_STR string 0 ${prj_config}
write_c_config_var CONF_RUNTIME_STR string 0 ${CONFIG_RUNTIME}
if [ x"${CONF_INCLUDE_CONCAT}" = x1 ]
then
  if [ x"${CONF_NO_GEN_REMAKE}" = x1 -o x"${CONF_NO_ARCH_GENDIR}" = x1 ]
  then
    echo '#define CONF_GENFILE(X) "gen/" X' >> $cbhf
  else
    echo '#define CONF_GENFILE(X) ".gen-" CONF_CANONIC_STR "/" X' >> $cbhf
  fi
else
  echo '#define CONF_GENFILE(X) X' >> $cbhf
fi
if [ x"${CONF_INCLUDE_CONCAT}" = x1 ]
then
  if [ x"${CONF_NO_DEP_REMAKE}" = x1 -o x"${CONF_NO_ARCH_DEPDIR}" = x1 ]
  then
    echo '#define CONF_DEPFILE(X) "dep/" X' >> $cbhf
  else
    echo '#define CONF_DEPFILE(X) ".dep-" CONF_CANONIC_STR "/" X' >> $cbhf
  fi
else
  echo '#define CONF_DEPFILE(X) X' >> $cbhf
fi
echo >> $cbhf
echo >> $cbcf
write_c_config_var CONF_VERSION_CODE int 1
write_c_config_var CONFIGURE_VINTAGE int 1
echo >> $cbhf
echo >> $cbcf
write_c_config_var CONF_CPU_${CONF_CPU}    int 0 1
write_c_config_var CONF_OS_${CONF_OS}      int 0 1
write_c_config_var CONF_LINUX_LIBC_VERSION int 1
[ x"${CONF_API_TYPE}" = xwin32 ] && write_c_config_var CONF_WIN32_API int 0 1
[ x"${CONF_API_TYPE}" = xdos ] && write_c_config_var CONF_DOS_API int 0 1
echo >> $cbhf
echo >> $cbcf
for i in CONF_ENDIAN CONF_CHAR_BITS CONF_SHORT_BITS CONF_INT_BITS CONF_LONG_BITS CONF___INT64_BITS CONF_LONGLONG CONF___INT64 CONF_INT64_SUFFIX CONF_UINT64_SUFFIX CONF_INT64_FORMAT CONF_LONG_DOUBLE_IS_DOUBLE CONF_PRINTF_LG_WORKS CONF_PRINTF_INT64 CONF_PRINTF_LLD_WORKS CONF_PRINTF_A_WORKS CONF_USE_SYSTEM_PRINTF CONF_HAS_STRTOLD CONF_HAS_STRTOF CONF_STRTOD_ACCEPTS_HEX CONF_SCANF_LLD_WORKS CONF_SCANF_INT64_WORKS CONF_SCANF_A_WORKS CONF_SCANF_LG_WORKS CONF_USE_SYSTEM_SCANF CONF_HAS_STRCASECMP CONF_HAS_STRNCASECMP CONF_HAS_STRNLEN
do
  write_c_config_var $i int 0
done
[ x"$CONF_LONGLONG"  = x1 ] && write_c_config_var CONF_LONG_LONG_BITS int 0
[ x"${CONF___INT64}" = x1 ] && write_c_config_var CONF___INT64_BITS int 0
echo >> $cbhf
echo >> $cbcf
for i in `[ x"${configure_cpp}" != xno ] && echo CONF_BUILTIN_CPP CONF_CPP_DEBUG` CONF_INCLUDE_CONCAT CONF_HAS_ALLOCA CONF_HAS__ALLOCA CONF_HAS_FCHDIR CONF_HAS_STRERROR CONF_HAS_STRERROR_IN_STRING_H CONF_HAS_SYS_ERRLIST CONF_HAS__SYS_ERRLIST CONF_SSCANF_WORKS CONF_HAS_STRTOLL CONF_HAS_STRTOULL CONF_HAS_SNPRINTF CONF_HAS__SNPRINTF CONF_HAS_MMAP CONF_HAS_SOCKET `[ x"${configure_asm_stuff}" != xno ] && echo CONF_ATTRIBUTE_ALIAS CONF_ATTRIBUTE_ALIAS_0 CONF_DECLARATION_ASM CONF_DECLARATION_ASM_0` CONF_ATTRIBUTE_CONSTRUCTOR CONF_FILE_STATIC_INIT CONF_CVM_BACKEND CONF_USE_DMALLOC
do
  write_c_config_var $i int 2
done
echo >> $cbhf
echo >> $cbcf
[ x"${configure_cpp_option_list}" != xno ] && echo "#define CONF_LANG_C_CPP_OPTIONS_LIST ${CONF_LANG_C_CPP_OPTIONS_LIST}" >> $cbhf
echo >> $cbhf
write_c_config_var ${CONF_FSTOKEN} int    0 1
write_c_config_var CONF_WRAPPERS   int    0
write_c_config_var CONF_EXE_SUFFIX string 0 "${EXESFX}"
write_c_config_var CONF_DIRSEP     string 0 "${DIRSEP}"
write_c_config_var CONF_PATHSEP    string 0 `eval echo ${PATHSEP}`
write_c_config_var CONF_DLL_PREFIX string 0 "${DLLPFX}"
write_c_config_var CONF_DLL_SUFFIX string 0 "${DLLSFX}"
write_c_config_var CONF_PREFIX     string 0
echo >> $cbhf
echo "#define CONF_HAS_P_CONFIG_VALUES 1" >> $cbhf
echo >> $cbhf
echo "#endif" >> $cbhf
echo "  0, 0," >> $cbcf
echo "};" >> $cbcf
echo "done"

cblf=${CONFIG_BASE}/reuse_import.h
printf "Writing ${cblf}..."
echo '/* Shared library configuration file */' > $cblf
echo "/* Automatically generated by configure utility */" >> $cblf
echo "/* Generation date: "`date`" */" >> $cblf
echo "#ifndef __REUSE_IMPORT_H__" >> $cblf
echo "#define __REUSE_IMPORT_H__" >> $cblf
echo >> $cblf
if [ x"${CONF_DLL}" = x2 ]
then
  echo '#if defined STATIC' >> ${cblf}
  echo '#  define R_DLL' >> ${cblf}
  echo '#elif defined __REUSE__ && defined DYNAMIC' >> ${cblf}
  echo '#  define R_DLL __declspec(dllexport)' >> ${cblf}
  echo '#else' >> ${cblf}
  echo '#  define R_DLL __declspec(dllimport)' >> ${cblf}
  echo '#endif' >> ${cblf}
else
  echo '#define R_DLL' >> ${cblf}
fi
echo >> $cblf
echo "#endif" >> $cblf
echo "done"

cbif=${CONFIG_BASE}/p_integral.h
printf "Writing $cbif..."
echo '/* Project configuration file (C file) */' > $cbif
echo "/* Automatically generated by configure utility */" >> $cbif
echo "/* Generation date: "`date`" */" >> $cbif
echo "#ifndef __P_INTEGRAL_H__" >> $cbif
echo "#define __P_INTEGRAL_H__" >> $cbif
echo >> $cbif
cat >>$cbif <<EOF
typedef signed char    rschar_t;
typedef unsigned char  ruchar_t;
typedef unsigned short rushort_t;
typedef unsigned int   ruint_t;
typedef unsigned long  rulong_t;
EOF
echo >> $cbif
if [ x"${CONF___INT64}" = x1 ]
then
  echo "#define R_HAS___INT64 1" >> $cbif
  echo "typedef __int64          rllong_t;" >> $cbif
  echo "typedef unsigned __int64 rullong_t;" >> $cbif
else
  echo "#undef R_HAS___INT64" >> $cbif
fi

if [ x"$CONF_LONGLONG" = x1 ]
then
  echo "#define R_HAS_LONGLONG 1" >> $cbif
  echo "typedef long long          rllong_t;" >> $cbif
  echo "typedef unsigned long long rullong_t;" >> $cbif
else
  echo "#undef R_HAS_LONGLONG" >> $cbif
fi
echo >> $cbif
reqsizes="8:1:;16:1:;32:1:;64:0:R_HAS_INT64"
hastypes="x:CHAR:char;x:SHORT:short;x:INT:int;x:LONG:long;CONF_LONGLONG:LONG_LONG:long long;CONF___INT64:__INT64:__int64"

IFS=\;
for r in $reqsizes
do
  IFS=\:
  set -- $r
  r_size=$1
  r_mandatory=$2
  r_has_flag=$3
  IFS=\;

  for rr in $hastypes
  do
    IFS=\:
    set -- $rr
    h_has_flag=$1
    [ x"${h_has_flag}" = xx ] & h_has_flag=
    h_size_var=$2
    h_type_name=$3
    IFS=\;

    if [ x"$h_has_flag" = x1 ]
    then
      eval 'h_present=$'${h_has_flag}
      [ "x${h_present}" != 1 ] && continue
    fi
    eval 'h_size=$CONF_'${h_size_var}'_BITS'
#    if [ "x${h_size}" = x ]
#    then
#      echo "bad entry in hastypes: ${h_has_flag}:${h_size_var}:${h_type_name}" >&2
#    fi

    if [ x"${r_size}" = x"${h_size}" ]
    then
      eval 'already_set=$has_type_bits_'${r_size}
      [ x"${already_set}" = x1 ] && continue

      [ x"${r_has_flag}" != x ] && echo "#define ${r_has_flag} 1" >> $cbif
      echo "typedef signed ${h_type_name} rint${r_size}_t;" >> $cbif
      echo "typedef unsigned ${h_type_name} ruint${r_size}_t;" >> $cbif
      eval 'has_type_bits_'${r_size}'=1'
    fi
  done

  eval 'already_set=$has_type_bits_'${r_size}
  if [ x"${r_mandatory}" = x1 -a x"${already_set}" = x ]
  then
    echo "No type ${r_size} bits wide detecting" >&2
    exit
  fi
done
IFS=$saved_IFS

echo >> $cbif
if [ x"${CONF_INT64_TYPE}" != x ]
then
  echo '#define R_I64(x) x##'${CONF_INT64_SUFFIX} >> $cbif
  echo '#define R_U64(x) x##'${CONF_UINT64_SUFFIX} >> $cbif
  echo '#define R_F64    "'${CONF_INT64_FORMAT}'"' >> $cbif
fi

echo >> $cbif
if [ x"${CONF_INT64_TYPE}" != x ]
then
  echo '#define R_LONG_LONG_MAX (9223372036854775807'${CONF_INT64_SUFFIX}')' >> $cbif
  echo '#define R_LONG_LONG_MIN (-9223372036854775807'${CONF_INT64_SUFFIX}'-1'${CONF_INT64_SUFFIX}')' >> $cbif
  echo '#define R_ULONG_LONG_MAX (18446744073709551615'${CONF_UINT64_SUFFIX}')' >> $cbif
fi

echo  >> $cbif
if [ x"${CONF_LONG_DOUBLE_IS_DOUBLE}" = x1 ]
then
  echo '#define R_LONG_DOUBLE_IS_DOUBLE 1' >> $cbif
else
  echo '#undef R_LONG_DOUBLE_IS_DOUBLE' >> $cbif
fi

echo >> $cbif
echo "#endif" >> $cbif
echo "done"


# Check for symlinks
case x"${prj_config}" in
  xix86-win32*)
    [ x"${ENABLE_LN}" = x ] && ENABLE_LN=0
    ;;
  xix86-msdos-djgpp)
    ENABLE_LN=0
    ;;
esac

# Generate mode files
if [ x"${option_mode_files}" != x0 ]
then
  # save original value of ENABLE_LN
  r_enable_ln=$ENABLE_LN

  # generate mode-dbg.make
  MODENAME=DEBUG
  case x"${prj_config}" in
    xix86-win32-bcc)
      CFLAGS='-v'
      CXXFLAGS='-v'
      CGENFLAGS=
      LDFLAGS=
      LDLIBS=
      ;;
    xix86-win32-vc)
      CFLAGS='-Od -Yd -Zi -W3 -MLd'
      CXXFLAGS='-Od -Yd -Zi -W3 -MLd'
      CGENFLAGS=
      LDFLAGS='-g'
      LDLIBS=
      ;;
    *)
      CFLAGS='-g -Wall'
      CXXFLAGS='-g -Wall'
      CGENFLAGS='-Wno-unused'
      LDFLAGS=''
      LDLIBS=
      ;;
  esac
  CMODEOPT='-DDEBUG'
  CXXMODEOPT='-DDEBUG'
  LDMODEOPT=
  LIBMODEOPT=
  EXE_MODE=775
  LIB_MODE=664
  ENABLE_LN=$r_enable_ln
  [ x"$ENABLE_LN" = x ] && ENABLE_LN=1
  if [ x"$ENABLE_LN" = x0 ]
  then
    OBJDIR='.obj-${'${config_var_name}'}'
    [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && OBJDIR=obj
    GENDIR='.gen-${'${config_var_name}'}'
    [ x"${CONF_NO_GEN_REMAKE}" = x1 ] && GENDIR=gen
    [ x"${CONF_NO_ARCH_GENDIR}" = x1 ] && GENDIR=gen
    DEPDIR='.dep-${'${config_var_name}'}'
    [ x"${CONF_NO_DEP_REMAKE}" = x1 ] && DEPDIR=dep
    [ x"${CONF_NO_ARCH_DEPDIR}" = x1 ] && DEPDIR=dep
    PICDIR='.pic-${'${config_var_name}'}'
    [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && PICDIR=pic
    LNS='true'
  else
    OBJDIR='o'
    [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && OBJDIR=obj
    GENDIR='g'
    [ x"${CONF_NO_GEN_REMAKE}" = x1 ] && GENDIR=gen
    [ x"${CONF_NO_ARCH_GENDIR}" = x1 ] && GENDIR=gen
    DEPDIR='d'
    [ x"${CONF_NO_DEP_REMAKE}" = x1 ] && DEPDIR=dep
    [ x"${CONF_NO_ARCH_DEPDIR}" = x1 ] && DEPDIR=dep
    PICDIR='p'
    [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && PICDIR=pic
    LNS='ln -s'
  fi

  ff=${prj_home}/etc/.cfg-${prj_config}/mode-dbg.make
  write_mode_file $ff

  # generate mode-rel.make
  MODENAME=RELEASE
  case x"${prj_config}" in
    xix86-win32-bcc)
      CFLAGS='-O2'
      CXXFLAGS='-O2'
      CGENFLAGS=
      LDFLAGS=
      LDLIBS=
      ;;
    xix86-win32-vc)
      CFLAGS='-O1 -Os -G6 -W0'
      CXXFLAGS='-O1 -Os -G6 -W0'
      CGENFLAGS=
      LDFLAGS=
      LDLIBS=
      ;;
    *)
      CFLAGS='-O2 -Wall'
      CXXFLAGS='-O2 -Wall'
      CGENFLAGS='-Wno-unused'
      LDFLAGS='-s'
      LDLIBS=
      ;;
  esac
  CMODEOPT='-DRELEASE'
  CXXMODEOPT='-DRELEASE'
  LDMODEOPT=
  LIBMODEOPT=
  EXE_MODE=775
  LIB_MODE=664
  ENABLE_LN=0
  OBJDIR='.obj-${'${config_var_name}'}'
  [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && OBJDIR=obj
  GENDIR='.gen-${'${config_var_name}'}'
  [ x"${CONF_NO_GEN_REMAKE}" = x1 ] && GENDIR=gen
  [ x"${CONF_NO_ARCH_GENDIR}" = x1 ] && GENDIR=gen
  DEPDIR='.dep-${'${config_var_name}'}'
  [ x"${CONF_NO_DEP_REMAKE}" = x1 ] && DEPDIR=dep
  [ x"${CONF_NO_ARCH_DEPDIR}" = x1 ] && DEPDIR=dep
  PICDIR='.pic-${'${config_var_name}'}'
  [ x"${CONF_NO_ARCH_OBJDIR}" = x1 ] && PICDIR=pic
  LNS='true'

  ff=${prj_home}/etc/.cfg-${prj_config}/mode-rel.make
  write_mode_file $ff

  # remove/backup old default mode file
  dmf=${prj_home}/etc/.cfg-${prj_config}/mode-.make
  case x"${prj_config}" in
    xix86-win32*|xix86-msdos*)
      # /bin/sh under cygwin32 lacks -h test. So, just remove the file.
      rm -rf ${dmf}
      ;;
    *)
      # backup previous mode file
      if [ -h ${dmf} ]
      then
        # just drop it if it's link
        rm -f ${dmf}
      elif [ -f ${dmf} ]
      then
        # move, if regular
        mv ${dmf} ${prj_home}/etc/.cfg-${prj_config}/mode-old.make
      else
        # remove gracelessly
        rm -rf ${dmf}
      fi
      ;;
  esac

  # set up ${mode_var_name} variable
  case x"$option_debug" in
    x1) eval ${mode_var_name}=dbg;;
    *)  eval ${mode_var_name}=rel;;
  esac
  export ${mode_var_name}
  eval 'prj_mode=$'${mode_var_name}
  
  # Link default mode file
  cfd=${prj_home}/etc/.cfg-${prj_config}
  if [ x"$r_enable_ln" = x0 ]
  then
    # Use cp command
    cp $cfd/mode-${prj_mode}.make $cfd/mode-.make
  else
    # Use ln -s command
    ln -s mode-${prj_mode}.make $cfd/mode-.make
  fi
fi

# Copy configuration header files
printf "Copying configuration headers..."
eval a=$INST_INCL
eval b=$INST_ARCH_INCL
if [ x"${a}" != x ]
then
  mkdir -p $a
  if [ x"${a}" != x ]
  then
    mkdir -p $b
    if [ x"$r_enable_ln" = x0 ]
    then
      # Use cp command
      cp -f $cbhf $b
      cp -f $cbif $b
      cp -f $cbcf $b
      cp -f $cblf $b
    else
      # Use ln command
      # Use of hard-links is preferred
      ln -f $cbhf $b
      ln -f $cbif $b
      ln -f $cbcf $b
      ln -f $cblf $b
    fi  
  fi
fi
echo "done"

printf "Exporting configuration variables..."
export ${home_var_name} ${config_var_name} ${mode_var_name}
echo "done"

if [ $p_home_updated = 1 ]
then
  [ x"$unsetlist" != x ] && unsetlist="${unsetlist} "
  unsetlist="${unsetlist}${home_var_name}"
fi
if [ $p_config_updated = 1 -a ${p_config_guessed} = 1 ]
then
  [ x"$unsetlist" != x ] && unsetlist="${unsetlist} "
  unsetlist="${unsetlist}${config_var_name}"
fi
if [ $p_mode_updated = 1 ]
then
  [ x"$unsetlist" != x ] && unsetlist="${unsetlist} "
  unsetlist="${unsetlist}${mode_var_name}"
fi

echo
echo "The toolkit is configured for compilation successfully."
if [ x"$unsetlist" != x ]
then
  echo "During configuration some environment settings were changed."
  echo "You should issue the following command before compilation."
  echo "If your shell is 'csh' or 'tcsh': unsetenv $unsetlist."
  echo "If your shell is 'sh', 'ksh', 'bash': unset $unsetlist."
fi
if [ $p_config_updated = 1 -a ${p_config_guessed} = 0 ]
then
  echo "You have to set ${config_var_name} environment variable."
  echo "For 'csh' users: setenv ${config_var_name} ${prj_config}"
  echo "For 'sh' users: ${config_var_name}=${prj_config}; export ${config_var_name}"
fi
echo "Now you can say 'make deps; make install' to compile."

cleanup
exit 0
