#!/bin/bash

# Configure script for CoCoALib
# It accepts a small number of options: run "configure --help" for details.

# Here is what this script does.
# It establishes the location of the GMP library (and the gmp.h file).
# It establishes which C++ compiler to use and the compilation flags;
# both compiler and flags can be user specified.
# After some basic checks, all details are written into a configuration file
# (currently called configuration/autoconf.mk) which is read by make.

# The chosen configuration parameters will be placed in the file CONFIG_FILE:
# we save any prevous CONFIG_FILE by appending suffix ".old",
# the new file is built up in CONFIG_TMP then renamed to CONFIG_FILE at the end.
CONFIG_DIR=configuration
CONFIG_FILE="$CONFIG_DIR/autoconf.mk"
CONFIG_TMP="$CONFIG_FILE.part"

# Auxiliary shell scripts are in this directory; load some useful shell fns.
SCRIPT_DIR=configuration
source "$SCRIPT_DIR/shell-fns.sh"

COCOA_EXTLIB_DIR=`pwd`/configuration/ExternalLibs
export COCOA_EXTLIB_DIR

# Default installation directory and command.
INSTALL_DIR=/usr/local
  INSTALL="install -S"
which install > /dev/null
if [ $? -ne 0 ]
then 
  INSTALL="/bin/cp -f"
fi


##################################################################
# Special handling for the arg "--again" (recalls args from previous invocation)
# This section puts the script args into ARGS.
if [ $# = 1 -a "XXX$1" = "XXX-again" ]
  then
    echo "$0: ERROR!  type  ./configure --again" > /dev/stderr
    exit 1
  fi
if [ $# = 1 -a "XXX$1" = "XXX--again" ]
then
  if [ \! -f "$CONFIG_FILE" ]
  then
    echo "$0: ERROR no previous configuration file found" > /dev/stderr
    exit 1
  fi
  ARGS=`head -5 "$CONFIG_FILE" | tail -1 | cut -c 3- | cut -f 2- -d " " `
  echo
  echo "Recalling previous configure command:"
  echo "$0 $ARGS"
  echo
  sleep 2
  ./configure $ARGS
  exit $?
fi


##################################################################
# Process command line args.

ORIG_CMD="$0 $@"

MODE=OPT  # default optimization/debugging flag

# Below, is there a neater way to do tilde expansion for paths to libraries???
for option in "$@"
do
  case $option in
    ( --again )
       echo "***ERROR***  $0: option '--again' must be used alone"
       exit 1;;

    ( --recall )
       head -5 "$CONFIG_FILE" | tail -1 | cut -c 3-
       exit 0;;

    ( --recall-from=* )
       head -5 "`echo "$option" | cut -f 2- -d=`" | tail -1 | cut -c 3-
       exit 0;;

    ( --with-libgmp=~* )
       GMP_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libgmp=* )
       GMP_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-boost=~* )
       BOOST_INC_DIR="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-boost=* )
       BOOST_INC_DIR=`echo "$option" | cut -f 2- -d=` ;;

    (--no-readline )
	AVOID_READLINE=yes ;;

    ( --with-readline=~* )
       READLINE_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-readline=* )
       READLINE_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libreadline=* )
       READLINE_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libcddgmp=system )
       CDD_LIB=`$SCRIPT_DIR/find-lib-in-std-dirs.sh libcddgmp`
       if [ $? -ne 0 ]
       then
         echo "$0: Could not find libcddgmp"
         exit 1
       fi
       echo "calling $SCRIPT_DIR/find-hdr-from-lib.sh cdd/cdd.h $CDD_LIB"
       CDD_HDR_DIR=`$SCRIPT_DIR/find-hdr-from-lib.sh cdd/cdd.h "$CDD_LIB"`
       if [ $? -ne 0 ]
       then
         echo "$0: $CDD_HDR_DIR"
         exit 1
       fi
       CDD_SYSTEM=yes;;

    ( --with-libcddgmp=~* )
       CDD_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libcddgmp=* )
       CDD_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libfrobby=~* )
       FROBBY_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libfrobby=* )
       FROBBY_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libgsl=~* )
       GSL_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libgsl=* )
       GSL_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libgfan=~* )
       GFAN_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libgfan=* )
       GFAN_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-libnormaliz=~* )
       NORMALIZ_LIB="$HOME`echo "$option" | cut -f 2- -d~`" ;;

    ( --with-libnormaliz=* )
       NORMALIZ_LIB=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-cxx=* )
       CXX=`echo "$option" | cut -f 2- -d=` ;;

    ( --with-cxxflags=* )
       CXXFLAGS=`echo "$option" | cut -f 2- -d=` ;;

    ( --mode=* )
       MODE=`echo "$option" | cut -f 2- -d=` ;;

    ( --threadsafe-hack )
       THREADSAFE_HACK=yes ;;

    ( --prefix=* )
       INSTALL_DIR=`echo "$option" | cut -f 2- -d=`
       if [ \! -d "$INSTALL_DIR" ]
       then
         echo "$0: specified installation directory does not exist: \`$INSTALL_DIR\'"
         exit 1
       fi;;

    ( --help* )
       echo -e "\nThis script configures CoCoALib.  For details please consult the"
       echo -e "files INSTALL and README located in the same directory as this script."
       echo -e "\nUsage: ./configure [OPTIONS]"
       echo -e "\nConfiguration:"
       echo -e "  --help\t\tdisplay this help and exit"
       echo -e "  --again\t\trerun configure with the same options as last time"
       echo -e "  --recall\t\tprint out the configure command given last time"
       echo -e "  --with-cxx=ARG\tspecify name of compiler [default: g++]"
       echo -e "\t\t\t(or specify via environment variable CXX)"
       echo -e "  --with-cxxflags=ARG\tspecify compilation flags [default: \"-Wall -pedantic\"]"
       echo -e "\t\t\t(or specify via environment variable CXXFLAGS)"
       echo -e "  --with-libgmp=ARG\tspecify location of the file libgmp.a (or libgmp.so)"
       echo -e "\t\t\t[default is to search]"
       echo -e "  --with-boost=ARG\tspecify location of the BOOST header files (without the trailing /boost)"
       echo -e "\t\t\t[default is to search]"
       echo -e "  --threadsafe-hack\tcompile using a hack for threadsafety"
       echo -e "  --mode=ARG\t\tspecify OPT/DEBUG1/DEBUG2/PROFILE [default: OPT]"
       echo -e "\nOptional_libraries:"
       echo -e "  --no-readline\tdo not look for libreadline.a"
       echo -e "  --with-libreadline=ARG\tspecify location of the file libreadline.a"
       echo -e "\t\t\t[default is to search for libreadline.a]"
       echo -e "  --with-libcddgmp=ARG\tspecify location of the file libcddgmp.a"
       echo -e "\t\t\t[default is no libcddgmp.a]"
       echo -e "  --with-libfrobby=ARG\tspecify location of the file libfrobby.a"
       echo -e "\t\t\t[default is no libfrobby]"
       echo -e "  --with-libgfan=ARG\tspecify location of the file libgfan.a"
       echo -e "\t\t\t[default is no libgfan]"
       echo -e "  --with-libgsl=ARG\tspecify location of the file libgsl.a"
       echo -e "\t\t\t[default is no libgsl]"
       echo -e "  --with-libnormaliz=ARG\tspecify location of the file libnormaliz.a"
       echo -e "\t\t\t[default is no libnormaliz]"
       echo -e "\nInstallation:"
       echo -e "  --prefix=PREFIX\tinstall in PREFIX/include & PREFIX/lib"
       echo -e "\t\t\t[default: /usr/local]"
       exit;;

    ( * )
       echobox "ERROR: unrecognized option/parameter \"$option\""
       echo
       echo "HINT: try \"$0 --help\" for guidance"
       exit 1;;
  esac
done

if [ "$AVOID_READLINE" = yes -a \! -z "$READLINE_LIB" ]
then
    echo "$0: incompatible inputs for readline"
    exit 1
fi


##################################################################
echo "Starting configuration process for CoCoALib..."
echo


##################################################################
# Rename the old configuration file (if it exists).
# Do this early so that after a failed configure it is not
# not possible to compile

if [ -f "$CONFIG_FILE" ]
then
  /bin/rm -rf "$CONFIG_FILE.old"
  /bin/mv "$CONFIG_FILE" "$CONFIG_FILE.old"
  echo "-------------------------------------------------------"
  echo "**NOTE**  moved previous config file into $CONFIG_FILE.old"
  echo "-------------------------------------------------------"
  echo
fi


##################################################################
# Check the compiler.

# If user didn't specify a compiler, we assume g++.
if [ -z "$CXX" ]
then
  CXX=g++
fi
# Check compiler and flags are sane.
# If there's a problem, result is error message & return code is non-zero.
# If all is well, result is either "gnu" or "not gnu" & return code is 0.
CXX_TYPE=`$SCRIPT_DIR/verify-compiler.sh "$CXX" "$CXXFLAGS"`
if [ $? -ne 0 ]
then
  echo "ERROR: $CXX_TYPE"                                                 > /dev/stderr
  echo "ERROR: Check the options --with-cxx=... and --with-cxxflags=..."  > /dev/stderr
  echo "ERROR: (or the environment variables CXX and CXXFLAGS)."          > /dev/stderr
  exit 1
fi
# Next line required by the scripts which check GMP (see below).
export CXX
export CXXFLAGS


##################################################################
# -- EXTERNAL LIBRARIES --            ############################
##################################################################
# Prepare subtree for symlinks to external libs
EXTLIBS=configuration/ExternalLibs
/bin/rm -rf $EXTLIBS
mkdir $EXTLIBS
mkdir $EXTLIBS/lib
mkdir $EXTLIBS/include

##################################################################
# external library: GMP
##################################################################
# Now obtain and check the GMP installation.

# Establish the location of the GMP library.
# Check whether user supplied the location of the GMP library; if not, we search for it.
if [ -n "$GMP_LIB" ]
then
  # User supplied a path to the GMP library.
  # Check it is at least a readable file.
  # Convert the file name into an absolute path (in case it was not).
  if [ \! -f "$GMP_LIB" -o \! -r "$GMP_LIB" ]
  then
    echo "ERROR: Specified GMP library is not a readable file \"$GMP_LIB\""
    exit 1
  fi
  # These two lines should make sure GMP_LIB is an absolute path.
  GMP_DIR=`dirname "$GMP_LIB"`
  GMP_LIB=`cd "$GMP_DIR"; pwd`/`basename "$GMP_LIB"`
  GMP_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$GMP_LIB"`
  /bin/ln -s "$GMP_LIB" $EXTLIBS/lib/libgmp-symlink.$GMP_LIB_EXTN
  HAVE_DEFAULT_GMP=no
else
  # User did not supply path to GMP library, so we try to use defaults.
  # That means we do NOT specify any paths just add -lgmp and maybe -lgmpxx.
  GMP_TRY_DEFAULT=`$SCRIPT_DIR/gmp-try-default.sh`  # uses CXX & CXXFLAGS
  if [ $? -eq 0 ]
  then
    HAVE_DEFAULT_GMP=yes
    # System defaults for GMP work fine.
    GMP_LIB=
    if [ "$GMP_TRY_DEFAULT" = "GMPXX" ]
    then
      GMPXX_LIB=
      HAVE_GMPXX=yes
    else
      HAVE_GMPXX=no
    fi
  else
    # Default GMP library not found, so we search for it.
    HAVE_DEFAULT_GMP=no
    GMP_MESG=`$SCRIPT_DIR/gmp-find.sh`
    if [ $? -ne 0 ]
    then
      # Something went wrong; so GMP_MESG contains an error message.
      echo "ERROR: Problem with GMP -- abandoning configuration of CoCoALib." > /dev/stderr
      echo "ERROR: You can provide the path to libgmp.a (or libgmp.so) using" > /dev/stderr
      echo "ERROR: the \"--with-libgmp=\" command line option to $0."         > /dev/stderr
      echo "ERROR: $GMP_MESG"                                                 > /dev/stderr
      exit 1
    else
      # gmp-find.sh script worked, so message is full path of GMP library
      /bin/ln -s "$GMP_MESG" $EXTLIBS/lib/libgmp-symlink.a
      GMP_LIB="$GMP_MESG"
    fi
  fi
fi


# If GMP is not a default library, search for the header file
# (and put symlink in $EXTLIBS/include).
if [ $HAVE_DEFAULT_GMP = "no" ]
then
  GMP_INC_DIR=`$SCRIPT_DIR/gmp-find-hdr.sh "$GMP_LIB"`
  if [ $? -ne 0 ]
  then
    # Something went wrong; details are in GMP_INC_DIR
    echo "ERROR: Unable to locate header for GMP library $GMP_LIB"  > /dev/stderr
    echo "ERROR: $GMP_INC_DIR"                                      > /dev/stderr
    exit 1
  fi
  GMP_H="$GMP_INC_DIR/gmp.h"
  /bin/ln -s "$GMP_H" $EXTLIBS/include/gmp.h
fi

GMP_VERSION=`$SCRIPT_DIR/gmp-version.sh`
if [ $? -ne 0 ]
then
  # GMP version script had trouble and printed out an error mesg.
  exit 1
fi

if [ "$GMP_VERSION" \< "4.2.3" ]
then
  echo "ERROR: Your GMP installation is too old!"                             > /dev/stderr
  echo "ERROR: Please update to a newer version, then reconfigure CoCoALib."  > /dev/stderr
  exit 1
fi

# 20140901 warn about old GMP; soon we shall REQUIRE a more recent version.
if [ "$GMP_VERSION" \< "5.1.0" ]
then
  WARNINGS="old GMP (incompat with C++11); $WARNINGS"
  echo ""; sleep 1; echo ""
  echo "WARNING: Your GMP installation is rather old (version: $GMP_VERSION)" > /dev/stderr
  echo "WARNING: We recommend you update to a newer version."                 > /dev/stderr
  echo ""; sleep 1; echo ""
  sleep 1
fi


# See which (-m***) compilation flags GMP used; we shall use the same ones.
CXXFLAGS_FOR_GMP=`$SCRIPT_DIR/gmp-cxx-flags.sh`
if [ $? -ne 0 ]
then
  echo "$CXXFLAGS_FOR_GMP"
  exit 1
fi

# If user supplied CXXFLAGS, check they are compatible with GMP
if [ -n "$CXXFLAGS" ]
then
  ( cd $SCRIPT_DIR; ./gmp-check-cxxflags.sh "$CXXFLAGS_FOR_GMP" )
  if [ $? -ne 0 ]
  then
    echo "ERROR: Supplied value of CXXFLAGS, namely \"$CXXFLAGS\""    > /dev/stderr
    echo "ERROR: is not compatible with GMP library"                  > /dev/stderr
    exit 1
  fi
fi

if [ $HAVE_DEFAULT_GMP = "no" ]
then
  # Now make "intelligent guess" for the full path of libgmpxx.
  # For the moment we do NOT CHECK if our guess was good
  # (we will check later if we are using Frobby).
  GMP_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$GMP_LIB"`
  GMPXX_LIB=`dirname "$GMP_LIB"`/libgmpxx.$GMP_LIB_EXTN
  GMPXX_H="$GMP_INC_DIR/gmpxx.h"
  if [ -f "$GMPXX_LIB" -a -r "$GMPXX_LIB" -a -f "$GMPXX_H" -a -r "$GMPXX_H" ]
  then
    HAVE_GMPXX=yes
  else
    HAVE_GMPXX=no
  fi

  if [ $HAVE_GMPXX = "yes" ]
  then
    /bin/ln -s "$GMPXX_LIB" $EXTLIBS/lib/libgmpxx-symlink.$GMP_LIB_EXTN
  fi
  # Tell user which version of GMP we are using.
  echo "Using GMP version $GMP_VERSION:"
#  echo "  library is in \"$GMP_LIB\""
  echo "  library is in " `readlink $EXTLIBS/lib/libgmp-symlink.a`
  echo "  header  is in " `readlink $EXTLIBS/include/gmp.h`
#  echo "  header  is in \"$GMP_INC_DIR/gmp.h\""
  echo

  else # $HAVE_DEFAULT_GMP = "yes"
    if [ $HAVE_GMPXX = "yes" ]
    then
      echo "Using system default GMP+GMPXX, version $GMP_VERSION"
    else
      echo "Using system default GMP, version $GMP_VERSION"
    fi
  echo
fi


##################################################################
# external library: READLINE
##################################################################
# Check whether libreadline is available; if so, we will use it
# for compiling CoCoAInterpreter

HAVE_READLINE=no
if [ -z "$AVOID_READLINE" ]
then
    if [ -z "$READLINE_LIB" ]
    then
	#  No libreadline specified, so look for an installed version
	$SCRIPT_DIR/readline-try-default.sh > /dev/null 2>& 1
	if [ $? = 0 ]
	then
	    HAVE_READLINE=yes
	    # Note that -lreadline includes also -lncurses or -ltermcap
	    READLINE_LIB="-lreadline"
	else
	    WARNINGS="libreadline not installed; $WARNINGS"
	    echo "WARNING:  readline is not installed"
	fi
    else
	# user specified a libreadline
	if [ \! "/$READLINE_LIB" -ef "$READLINE_LIB" ]
	then
	    echo "ERROR: supplied libreadline path is not absolute: $READLINE_LIB"
	    exit 1
	fi
	if [ \! -f "$READLINE_LIB" -o \! -r "$READLINE_LIB" ]
	then
	    echo "ERROR: cannot read supplied libreadline: $READLINE_LIB"
	    exit 1
	fi
	# User supplied abs path to a readable file.
	READLINE_HDR=`$SCRIPT_DIR/readline-find-hdr.sh "$READLINE_LIB"`
	if [ $? -ne 0 ]
	then
	    echo "ERROR: cannot find header file for the specified libreadline." > /dev/stderr
	    exit 1
	fi
	
	LIBTERMCAP=`$SCRIPT_DIR/readline-check-cxxflags.sh "$READLINE_HDR" "$READLINE_LIB"`
	if [ $? = 0 ]
	then
	    HAVE_READLINE=yes
	    READLINE_LIB="$READLINE_LIB  $LIBTERMCAP"
	fi
    fi
fi

if [ $HAVE_READLINE = yes ]
then
  READLINE_FLAG="-DCoCoA_WITH_READLINE"
fi


##################################################################
# Check whether "qmake" is available -- needed for building the GUI
# Assume that if "qmake" is available then all of QT is too.
# Also check that "qmake" actually runs (Ubuntu 14.04 can have a broken symlink)
HAVE_QMAKE=no
QMAKE=`which qmake 2>/dev/null`
if [ $? = 0 ]
then
  QMAKE=`[ -x "$QMAKE" ] && qmake -help 2>&1 >/dev/null`
  if [ $? = 0 ]
  then
    HAVE_QMAKE=yes
  fi
fi

if [ $HAVE_QMAKE = no ]
then
  WARNINGS="no qmake (so no CoCoA5 GUI); $WARNINGS"
fi

##################################################################
# external library: BOOST
##################################################################
# BOOST installation
# Since BOOST is not (currently) essential for CoCoALib, we do
# not give an error if the boost-find-hdrs.sh script cannot find it.
# If a unique BOOST installation was not found, we set
# BOOST_HDRS_NOT_FOUND to the error mesg (o/w it is left empty).
# Otherwise the full path of the unique dir containing header files
# is put in BOOST_INC_DIR.

if [ -z "$BOOST_INC_DIR" ]
then
  BOOST_MESG=`$SCRIPT_DIR/boost-find-hdrs.sh`
  BOOST_FIND_HDRS_EXIT_CODE=$?
  if [ $BOOST_FIND_HDRS_EXIT_CODE = 2 ] # 2 means terminating error
  then
    echo "$BOOST_MESG"  > /dev/stderr
    exit 1
  fi
  if [ $BOOST_FIND_HDRS_EXIT_CODE = 0 ]
  then
    BOOST_INC_DIR="$BOOST_MESG"
    HAVE_BOOST_HDRS=yes
  else
    HAVE_BOOST_HDRS=no
    echo "CONFIGURATION COMMENT"
    echo "$BOOST_MESG"
    echo "Proceeding without BOOST"
  fi
else
  # User supplied a path to the BOOST directory.
  # Check it is at least a readable file.
  # Convert the file name into an absolute path (in case it was not).
  HAVE_BOOST_HDRS=yes
  if [ \! -d "$BOOST_INC_DIR" -o \! -r "$BOOST_INC_DIR" ]
  then
    echo "ERROR: the specified BOOST header directory does not exist or is unreadable" > /dev/stderr
    echo "ERROR: path specified was $BOOST_INC_DIR"                                    > /dev/stderr
    exit 1
  fi
  if [ \! -d "$BOOST_INC_DIR/boost" ]
  then
    echo "ERROR: the specified BOOST header directory does not contain subdirectory \"boost\"" > /dev/stderr
    if [ "`basename \"$BOOST_INC_DIR\"`" = "boost" ]
    then
      echo "ERROR: Perhaps you should simply remove the final path component?"         > /dev/stderr
    fi
    exit 1
  fi
fi

# At this point either HAVE_BOOST_HDRS = "yes" (in which case BOOST_INC_DIR contains a plausible path)
# or HAVE_BOOST_HDRS = "no" (and BOOST_MESG might explain why)
if [ "$HAVE_BOOST_HDRS" = "yes" ]
then
  BOOST_INC_DIR=`cd "$BOOST_INC_DIR"; pwd`
  if [ "$BOOST_INC_DIR" != /usr/local/include -a "$BOOST_INC_DIR" != /usr/include ]
  then
    /bin/ln -s "$BOOST_INC_DIR/boost" $EXTLIBS/include/
  fi
  BOOST_MESG=`$SCRIPT_DIR/boost-find-lib.sh "$BOOST_INC_DIR"`
  if [ $? -ne 0 ]
  then
    BOOST_LIB_NOT_FOUND="$BOOST_MESG"
  else
    eval "$BOOST_MESG"  # sets BOOST_LIB_DIR and BOOST_LDLIBS
    BOOST_LIB_NOT_FOUND=
    # Check that the BOOST libs are compatible with CXX and CXXFLAGS
    BOOST_MESG=`$SCRIPT_DIR/boost-check-arch.sh "$CXXFLAGS_FOR_GMP" "$BOOST_LDLIBS"`
    if [ $? -ne 0 ]
    then
      BOOST_LIB_NOT_FOUND="BOOST libs incompatible with GMP lib!"
      WARNINGS="BOOST not found; $WARNINGS"
      echo "***WARNING*** $BOOST_LIB_NOT_FOUND"
      echo "***WARNING*** Proceeding without BOOST"
      BOOST_LDLIBS=
    fi
  fi
fi

# Set HAVE_BOOST  "english boolean"
if [ "$HAVE_BOOST_HDRS" = "yes" -a -z "$BOOST_LIB_NOT_FOUND" ]
then
  HAVE_BOOST=yes
else
  HAVE_BOOST=no
fi


# Tell user which BOOST we are using.
if [ "$HAVE_BOOST" = "yes" ]
then
  BOOST_FLAG="-DCoCoA_WITH_BOOST"
  echo "Using BOOST:"
  echo "  header files  are in $BOOST_INC_DIR"
  echo "  library files are in $BOOST_LIB_DIR"
else
  echo
  echo "Not using BOOST ==> compilation of CoCoA5+GUI disabled"
  if [ "$HAVE_BOOST_HDRS" = "yes" ]
  then
    echo "$BOOST_LIB_NOT_FOUND" | fold -s
  fi
fi
echo

if [ $HAVE_BOOST = "yes" -a $HAVE_QMAKE = "no" ]
then
  echo "Note: we will build CoCoA5 ***without*** GUI because \"qmake\" is absent"
  echo
fi


##################################################################
# external library: CDD (GMP)
##################################################################
# Check the file passed as CDD library is at least a readable file.
# Convert the file name into an absolute path (in case it was not).
if [ -n "$CDD_LIB" ]
then 
  if [ \! -f "$CDD_LIB" -o \! -r "$CDD_LIB" ]
  then
    echo "ERROR: Specified CDD library is not a readable file \"$CDD_LIB\""
    exit 1
  fi
  HAVE_CDD=yes
  # These three lines should make sure CDD_LIB is an absolute path.
  CDD_LIB_NAME=`basename "$CDD_LIB"`
  CDD_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$CDD_LIB_NAME"`
  CDD_LIB_DIR=`dirname "$CDD_LIB"`
  CDD_LIB_DIR=`cd "$CDD_LIB_DIR"; pwd`
  CDD_LIB="$CDD_LIB_DIR/$CDD_LIB_NAME"
  CDD_INC_DIR="$CDD_LIB_DIR"
  CDD_FLAG="-DCoCoA_WITH_CDD"
  if [ \! -d "$CDD_INC_DIR" ]
  then
    echo "ERROR: Did not find CDD header file where expected, in dir: \"$CDD_INC_DIR\""
    exit 1
  fi
/bin/ln -s "$CDD_LIB"     $EXTLIBS/lib/libcddgmp-symlink.$CDD_LIB_EXTN
/bin/ln -s "$CDD_INC_DIR" $EXTLIBS/include
else
  HAVE_CDD=no
fi


##################################################################
# external library: FROBBY
##################################################################
# Check the file passed as libfrobby.a is at least a readable file.
# Convert the file name into an absolute path (in case it was not).
if [ -n "$FROBBY_LIB" ]
then 
  if [ \! -f "$FROBBY_LIB" -o \! -r "$FROBBY_LIB" ]
  then
    echo "ERROR: Specified FROBBY library is not a readable file \"$FROBBY_LIB\""
    exit 1
  fi
  if [ $HAVE_GMPXX = "no" ]
  then
    echo "ERROR: Frobby needs libgmpxx but your GMP installation does not have it."   > /dev/stderr
    echo "ERROR: Please specify a GMP installation with libgmpxx, or rebuild GMP"     > /dev/stderr
    echo "specifying that you want the C++ library too (see GMP doc for details)."    > /dev/stderr
    exit 1
  fi

  # These lines should put absolute paths in FROBBY_LIB and FROBBY_DIR.
  HAVE_FROBBY=yes
  FROBBY_LIB_DIR=`dirname "$FROBBY_LIB"`
  FROBBY_LIB_DIR=`cd "$FROBBY_LIB_DIR"; pwd`
  FROBBY_LIB="$FROBBY_LIB_DIR"/`basename "$FROBBY_LIB"`
  FROBBY_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$FROBBY_LIB"`
/bin/ln -s "$FROBBY_LIB" $EXTLIBS/lib/libfrobby-symlink.$FROBBY_LIB_EXTN
/bin/ln -s `dirname "$FROBBY_LIB_DIR"`/src/frobby.h $EXTLIBS/include
if [ $? -ne 0 ]
then
  echo "ERROR: frobby.h header file not found in `dirname \"$FROBBY_LIB_DIR\"`"
  exit 1
fi
###  FROBBY_DIR=`cd "$FROBBY_LIB_DIR"/..; pwd`
  FROBBY_FLAG="-DCoCoA_WITH_FROBBY"
else
  HAVE_FROBBY=no
fi


##################################################################
# external library: GFAN
##################################################################
# Check the file passed as GFAN library is at least a readable file.
# Convert the file name into an absolute path (in case it was not).
if [ -n "$GFAN_LIB" ]
then 
  if [ \! -f "$GFAN_LIB" -o \! -r "$GFAN_LIB" ]
  then
    echo "ERROR: Specified GFAN library is not a readable file \"$GFAN_LIB\""
    exit 1
  fi
  HAVE_GFAN=yes
  # These three lines should make sure GFAN_LIB is an absolute path.
  GFAN_LIB_NAME=`basename "$GFAN_LIB"`
  GFAN_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$GFAN_LIB_NAME"`
  GFAN_LIB_DIR=`dirname "$GFAN_LIB"`
  GFAN_LIB_DIR=`cd "$GFAN_LIB_DIR"; pwd`
  GFAN_LIB="$GFAN_LIB_DIR/$GFAN_LIB_NAME"
  GFAN_INC_DIR="$GFAN_LIB_DIR"
  GFAN_FLAG="-DCoCoA_WITH_GFAN"
  if [ \! -d "$GFAN_INC_DIR" ]
  then
    echo "ERROR: Did not find GFAN header file where expected, in dir: \"$GFAN_INC_DIR\""
    exit 1
  fi
/bin/ln -s "$GFAN_LIB"     $EXTLIBS/lib/libgfan-symlink.$GFAN_LIB_EXTN
/bin/ln -s "$GFAN_INC_DIR" $EXTLIBS/include
else
  HAVE_GFAN=no
fi


##################################################################
# external library: GSL
##################################################################
# Check the file passed as GSL library is at least a readable file.
# Convert the file name into an absolute path (in case it was not).
if [ -n "$GSL_LIB" ]
then 
  if [ \! -f "$GSL_LIB" -o \! -r "$GSL_LIB" ]
  then
    echo "ERROR: Specified GSL library is not a readable file \"$GSL_LIB\""
    exit 1
  fi
  HAVE_GSL=yes
  # These three lines should make sure GSL_LIB is an absolute path.
  GSL_LIB_NAME=`basename "$GSL_LIB"`
  GSL_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$GSL_LIB_NAME"`
  GSL_LIB_DIR=`dirname "$GSL_LIB"`
  GSL_LIB_DIR=`cd "$GSL_LIB_DIR"; pwd`
  GSL_LIB="$GSL_LIB_DIR/$GSL_LIB_NAME"
  if [ "`basename \"$GSL_LIB_DIR\"`" = ".libs" ]
  then
    GSL_INC_DIR="`dirname \"$GSL_LIB_DIR\"`/gsl"
  else
    GSL_INC_DIR="`dirname \"$GSL_LIB_DIR\"`/include/gsl"
  fi
  GSL_FLAG="-DCoCoA_WITH_GSL"
  if [ \! -d "$GSL_INC_DIR" ]
  then
    echo "ERROR: Did not find GSL header file where expected, in dir: \"$GSL_INC_DIR\""
    exit 1
  fi
/bin/ln -s "$GSL_LIB" $EXTLIBS/lib/libgsl-symlink.$GSL_LIB_EXTN
/bin/ln -s "$GSL_INC_DIR" $EXTLIBS/include
else
  HAVE_GSL=no
fi


##################################################################
# external library: NORMALIZ
##################################################################
# Check the path passed as libnormaliz is at least a readable file.
# Make the path absolute (in case it was not).
if [ -n "$NORMALIZ_LIB" ]
then 
  if [ \! -f "$NORMALIZ_LIB" -o \! -r "$NORMALIZ_LIB" ]
  then
    echo "ERROR: Specified NORMALIZ library is not a readable file \"$NORMALIZ_LIB\""
    echo
    exit 1
  fi
  HAVE_NORMALIZ=yes
  # These lines should put absolute paths in NORMALIZ_LIB and NORMALIZ_DIR.
  NORMALIZ_LIB_NAME=`basename "$NORMALIZ_LIB"`
  NORMALIZ_LIB_EXTN=`$SCRIPT_DIR/extn.sh "$NORMALIZ_LIB_NAME"`
  NORMALIZ_LIB_DIR=`dirname "$NORMALIZ_LIB"`
  NORMALIZ_LIB_DIR=`cd "$NORMALIZ_LIB_DIR"; pwd`
  NORMALIZ_LIB="$NORMALIZ_LIB_DIR"/"$NORMALIZ_LIB_NAME"
  NORMALIZ_INC_DIR="$NORMALIZ_LIB_DIR/../../source/libnormaliz"
  # Hack to handle case of installed libnormaliz...
  if [ \! -f "$NORMALIZ_INC_DIR/libnormaliz.h" ]
  then
    NORMALIZ_INC_DIR="$NORMALIZ_LIB_DIR/../include/libnormaliz"
  fi
  # End of hack
  NORMALIZ_FLAG="-DCoCoA_WITH_NORMALIZ"
  if [ $HAVE_BOOST_HDRS = "no" ]
  then
    echo "ERROR: Normaliz requires BOOST."                           > /dev/stderr
    echo "ERROR: You can provide the path to boost header dir using" > /dev/stderr
    echo "ERROR: the \"--with-boost=\" command line option to $0."   > /dev/stderr
    echo "ERROR: $BOOST_HDRS_NOT_FOUND"                              > /dev/stderr
    exit 1
  fi
  if [ $HAVE_GMPXX = "no" ]
  then
    echo "ERROR: Normaliz requires libgmpxx."                                      > /dev/stderr
    echo "ERROR: Please use the command line option \"--with-libgmp=\" to specify" > /dev/stderr
    echo "ERROR: a GMP installation with libgmpxx, or rebuild GMP specifying that" > /dev/stderr
    echo "ERROR: you want the C++ library too (see GMP doc for details)."          > /dev/stderr
    exit 1
  fi
/bin/ln -s "$NORMALIZ_LIB" $EXTLIBS/lib/libnormaliz-symlink.$NORMALIZ_LIB_EXTN
/bin/ln -s "$NORMALIZ_INC_DIR" $EXTLIBS/include/
else
  HAVE_NORMALIZ=no
fi



##################################################################
# Decide which "optimization" flags to use:
#  if the compiler is type "gnu" then we use -O2
#  otherwise we just use -O

if [ "$CXX_TYPE" = "gnu" ]
then
  OPT_FLAG=-O2
  if [ -z "$CXXFLAGS" ]
  then
    FPIC=`$SCRIPT_DIR/fpic-flag.sh "$CXX"`
    CXXFLAGS="-Wall -pedantic $FPIC"
    LDFPIC=`$SCRIPT_DIR/fpic-ldflag.sh "$CXX"`
  fi
else
  OPT_FLAG=-O
fi

# Some special handling for Normaliz..
if [ $HAVE_NORMALIZ = "yes" ]
then
  # Hack to allow Normaliz to use longlong
  # later versions of g++ allow the flag -std=c++11, but I'm
  # stuck on a horrible Mac with version 4.2 :-(
  if [ "$CXX_TYPE" = "gnu" -a -n "$NORMALIZ_FLAG" ]
  then
    NORMALIZ_FLAG="$NORMALIZ_FLAG -Wno-long-long"
  fi

  # Check whether Normaliz was compiled with OpenMP
  NORMALIZ_WITH_OPENMP=`$SCRIPT_DIR/normaliz-with-openmp.sh "$NORMALIZ_LIB"`
  if [ -n "$NORMALIZ_WITH_OPENMP" ]
  then
    CXXFLAGS_DEFINES_COMMON="$CXXFLAGS_DEFINES_COMMON -fopenmp"
  fi
fi

# Ugly hack to produce a threadsafe compilation:
if [ "$THREADSAFE_HACK" = "yes" ]
then
  CXXFLAGS_DEFINES_COMMON="$CXXFLAGS_DEFINES_COMMON -DCoCoA_THREADSAFE_HACK"
fi


CXXFLAGS_COMMON="$CXXFLAGS $CXXFLAGS_FOR_GMP"
CXXFLAGS_DEFINES_COMMON="$CXXFLAGS_DEFINES_COMMON $BOOST_FLAG $CDD_FLAG $FROBBY_FLAG $GFAN_FLAG $GSL_FLAG $NORMALIZ_FLAG $READLINE_FLAG"
CXXFLAGS_OPT="$OPT_FLAG"
CXXFLAGS_DEBUG1="-Wextra -g"; CXXFLAGS_DEFINES_DEBUG1="-DCoCoA_DEBUG"
CXXFLAGS_DEBUG2="-Wextra -g"; CXXFLAGS_DEFINES_DEBUG2="-DCoCoA_DEBUG -DCoCoA_MEMPOOL_DEBUG"
CXXFLAGS_PROFILE="$OPT_FLAG -pg"

##################################################################
# Determine custom CPP flags (& add them to CXXFLAGS_COMMON)

CXX="$CXX"  CXXFLAGS="$CXXFLAGS_COMMON $CXXFLAGS_OPT"  UL2L="`$SCRIPT_DIR/cpp-flags.sh`"
if [ $? -ne 0 ]
then
  echo "***ERROR*** Customization failed"
  echo "***ERROR*** $UL2L"
  echo
  exit 1
fi
CXXFLAGS_DEFINES_COMMON="$UL2L $CXXFLAGS_DEFINES_COMMON"


# Tell user the compiler and default compilation flags put in the Makefile.
echo "The C++ compiler is $CXX"
echo "The C++ compilation flags are \"$CXXFLAGS_COMMON $CXXFLAGS_DEFAULT\""
echo

##################################################################
##################################################################
# We have checked the compiler, GMP library etc., so now create the CONFIG_FILE

##################################################################
# Place initial message and fixed_part1 in $CONFIG_FILE

echo "# Makefile configuration for CoCoALib."                  > "$CONFIG_TMP"
echo "# Created automatically by the configure script."       >> "$CONFIG_TMP"
echo "# Created on  `date \"+%Y-%m-%d  at time  %H:%M:%S\"`"  >> "$CONFIG_TMP"
echo "# Command was: "                                        >> "$CONFIG_TMP"
echo "# $ORIG_CMD"                                            >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"

/bin/cat "$CONFIG_DIR/fixed_part1"                            >> "$CONFIG_TMP"

##################################################################
# Append the CoCoALib version number.

source "$CONFIG_DIR/version"
VERSION=$VER_MAJ.$VER_MIN$VER_PATCH
echo "# Version number of CoCoALib we shall build."           >> "$CONFIG_TMP"
echo "VERSION=$VERSION"                                       >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"

# Installation command and directory (just placeholders, 20140323)
echo "Installation options are:"
echo "  directory INSTALL_DIR=$INSTALL_DIR"
echo "  command   INSTALL=$INSTALL"
echo

echo "INSTALL_DIR=$INSTALL_DIR"                               >> "$CONFIG_TMP"
echo "INSTALL=$INSTALL"                                       >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"
echo "EXTLIBS=\$(COCOA_ROOT)/configuration/ExternalLibs"      >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"

echo "# Compilation flags common to all cases:"               >> "$CONFIG_TMP"
echo "CXXFLAGS_COMMON=$CXXFLAGS_COMMON"                       >> "$CONFIG_TMP"
echo "CXXFLAGS_DEFINES_COMMON=$CXXFLAGS_DEFINES_COMMON"       >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"

echo "####################################################"   >> "$CONFIG_TMP"
echo "###   SPECIFIC compilation flags -- CHOOSE ONE   ###"   >> "$CONFIG_TMP"
echo "####################################################"   >> "$CONFIG_TMP"

#############################################################################
# Output the various compilation cases.

if [ "$MODE" = OPT ]
then
  CXXFLAGS_DEFAULT="$CXXFLAGS_OPT"
else
  HASH_OPT="#"
fi

if [ "$MODE" = DEBUG1 ]
then
  CXXFLAGS_DEFAULT="$CXXFLAGS_DEBUG1 $CXXFLAGS_DEFINES_DEBUG1"
else
  HASH_DEBUG1="#"
fi

if [ "$MODE" = DEBUG2 ]
then
  CXXFLAGS_DEFAULT="$CXXFLAGS_DEBUG2 $CXXFLAGS_DEFINES_DEBUG2"
else
  HASH_DEBUG2="#"
fi

if [ "$MODE" = PROFILE ]
then
  CXXFLAGS_DEFAULT="$CXXFLAGS_PROFILE"
else
  HASH_PROFILE="#"
fi


echo "### Case (1): compile optimized and without debugging"  >> "$CONFIG_TMP"
echo "$HASH_OPT CXXFLAGS_SPECIFIC=$CXXFLAGS_OPT"              >> "$CONFIG_TMP"

echo "### Case (2): compile with debugging activated"         >> "$CONFIG_TMP"
echo "$HASH_DEBUG1 CXXFLAGS_SPECIFIC=$CXXFLAGS_DEBUG1"        >> "$CONFIG_TMP"
echo "$HASH_DEBUG1 CXXFLAGS_DEFINES_SPECIFIC=$CXXFLAGS_DEFINES_DEBUG1" >> "$CONFIG_TMP"

echo "### Case (3): compile with full debugging activated"    >> "$CONFIG_TMP"
echo "$HASH_DEBUG2 CXXFLAGS_SPECIFIC=$CXXFLAGS_DEBUG2"        >> "$CONFIG_TMP"
echo "$HASH_DEBUG2 CXXFLAGS_DEFINES_SPECIFIC=$CXXFLAGS_DEFINES_DEBUG2" >> "$CONFIG_TMP"


echo "### Case (4): compile with profiling activated"         >> "$CONFIG_TMP"
echo "$HASH_PROFILE CXXFLAGS_SPECIFIC=$CXXFLAGS_PROFILE"      >> "$CONFIG_TMP"


echo                                                          >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"
echo "# Actual compilation flags:"                            >> "$CONFIG_TMP"
echo "CXXFLAGS=\$(CXXFLAGS_COMMON) \$(CXXFLAGS_SPECIFIC)"     >> "$CONFIG_TMP"
echo "CXXFLAGS_DEFINES=\$(CXXFLAGS_DEFINES_COMMON) \$(CXXFLAGS_DEFINES_SPECIFIC)" >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"
echo "######################################################" >> "$CONFIG_TMP"
echo "# These variables were set by the configure script."    >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"
echo "CXX=$CXX"                                               >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"


# Add appropriate definitions to the config file, and version number in a comment.
echo "# We use the following GMP installation:"               >> "$CONFIG_TMP"
echo "GMP_VERSION=$GMP_VERSION"                               >> "$CONFIG_TMP"

if [ $HAVE_DEFAULT_GMP = "no" ]
then
  echo "GMP_LIB=\$(EXTLIBS)/lib/libgmp-symlink.a"             >> "$CONFIG_TMP"
  echo "GMP_LDLIB=-lgmp-symlink"                              >> "$CONFIG_TMP"
  if [ $HAVE_GMPXX = "yes" ]
  then
    echo "GMPXX_LIB=\$(EXTLIBS)/lib/libgmpxx-symlink.a"       >> "$CONFIG_TMP"
    echo "GMPXX_LDLIB=-lgmpxx-symlink"                        >> "$CONFIG_TMP"
  fi
else
  echo "GMP_LDLIB=-lgmp"                                      >> "$CONFIG_TMP"
  if [ $HAVE_GMPXX = "yes" ]
  then
    echo "GMPXX_LDLIB=-lgmpxx"                                >> "$CONFIG_TMP"
  fi
fi
####  echo "GMP_INC_DIR=\"$GMP_INC_DIR\""                         >> "$CONFIG_TMP"
  ## Anna-20090515:  !!WARNING!! -isystem option did not work on my G4
####  echo "GMP_INCLUDE=-isystem \$(GMP_INC_DIR)"                 >> "$CONFIG_TMP"

echo                                                          >> "$CONFIG_TMP"


echo "HAVE_QMAKE=$HAVE_QMAKE"                                 >> "$CONFIG_TMP"
echo                                                          >> "$CONFIG_TMP"

# If BOOST libraries are present, put them in CONFIG file; otherwise
# unset BOOST_INC_DIR & BOOST_LIBS
echo "# BOOST settings:"                                      >> "$CONFIG_TMP"
echo "HAVE_BOOST=$HAVE_BOOST"                                 >> "$CONFIG_TMP"
####if [ $HAVE_BOOST = "no" ]
####then
####else
####  echo "BOOST_INC_DIR=\"$BOOST_INC_DIR\""                 >> "$CONFIG_TMP"
####  echo "BOOST_INCLUDE=-isystem \$(BOOST_INC_DIR)"         >> "$CONFIG_TMP"
####  echo "BOOST_LIBS=-L\"$BOOST_LIB_DIR\" $BOOST_LIBS"      >> "$CONFIG_TMP"
  echo "BOOST_LDLIBS=$BOOST_LDLIBS"                           >> "$CONFIG_TMP"
####fi
echo                                                          >> "$CONFIG_TMP"


##################################################################
# settings for optional external libs

echo "OPTIONAL external libraries:"

# READLINE
echo "# READLINE settings:"                                   >> "$CONFIG_TMP"
echo "HAVE_READLINE=$HAVE_READLINE"                           >> "$CONFIG_TMP"
echo "HAVE_READLINE = $HAVE_READLINE   $READLINE_LIB"
if [ $HAVE_READLINE = "yes" ]
then
  echo "READLINE_LDLIBS=$READLINE_LIB"                        >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# CDD
echo "# CDD settings:"                                        >> "$CONFIG_TMP"
echo "HAVE_CDD=$HAVE_CDD"                                     >> "$CONFIG_TMP"
echo "HAVE_CDD      = $HAVE_CDD   $CDD_LIB"
if [ $HAVE_CDD = "yes" ]
then
  echo "CDD_LIBS=\$(EXTLIBS)/lib/libcddgmp-symlink.a"         >> "$CONFIG_TMP"
  echo "CDD_LDLIBS=-lcddgmp-symlink"                          >> "$CONFIG_TMP"
  echo "CDD_VERSION="                                         >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# FROBBY
echo "# FROBBY settings:"                                     >> "$CONFIG_TMP"
echo "HAVE_FROBBY=$HAVE_FROBBY"                               >> "$CONFIG_TMP"
echo "HAVE_FROBBY   = $HAVE_FROBBY   $FROBBY_LIB"
if [ $HAVE_FROBBY = "yes" ]
then
  # Recall that GMPXX_LIB is the libgmpxx.a library
  echo "FROBBY_LIBS=\$(EXTLIBS)/lib/libfrobby-symlink.a \$(GMPXX_LIB)" >> "$CONFIG_TMP"
  echo "FROBBY_LDLIBS=-lfrobby-symlink"                       >> "$CONFIG_TMP"
  echo "FROBBY_VERSION="                                      >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# GFAN
echo "# GFAN settings:"                                       >> "$CONFIG_TMP"
echo "HAVE_GFAN=$HAVE_GFAN"                                   >> "$CONFIG_TMP"
echo "HAVE_GFAN     = $HAVE_GFAN   $GFAN_LIB"
if [ $HAVE_GFAN = "yes" ]
then
  echo "GFAN_LIBS=\$(EXTLIBS)/lib/libgfan-symlink.a"          >> "$CONFIG_TMP"
  echo "GFAN_LDLIBS=-lgfan-symlink"                           >> "$CONFIG_TMP"
  echo "GFAN_VERSION="                                        >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# GSL
echo "# GSL settings:"                                        >> "$CONFIG_TMP"
echo "HAVE_GSL=$HAVE_GSL"                                     >> "$CONFIG_TMP"
echo "HAVE_GSL      = $HAVE_GSL   $GSL_LIB"
if [ $HAVE_GSL = "yes" ]
then
####  echo "GSL_INC_DIR=\"$GSL_INC_DIR\""                     >> "$CONFIG_TMP"
####  echo "GSL_INCLUDE=-I \$(GSL_INC_DIR)"                   >> "$CONFIG_TMP"
  echo "GSL_LIBS=\$(EXTLIBS)/lib/libgsl-symlink.a"            >> "$CONFIG_TMP"
  echo "GSL_LDLIBS=-lgsl-symlink -lblas -llapack"             >> "$CONFIG_TMP"
  echo "GSL_VERSION="                                         >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# NORMALIZ
echo "# Normaliz settings:"                                   >> "$CONFIG_TMP"
echo "HAVE_NORMALIZ=$HAVE_NORMALIZ"                           >> "$CONFIG_TMP"
echo "HAVE_NORMALIZ = $HAVE_NORMALIZ   $NORMALIZ_LIB"
if [ $HAVE_NORMALIZ = "yes" ]
then
  # Recall that GMPXX_LIB is the libgmpxx.a library
  echo "NORMALIZ_LIBS=\$(EXTLIBS)/lib/libnormaliz-symlink.a \$(GMPXX_LIB)" >> "$CONFIG_TMP"
  echo "NORMALIZ_LDLIBS=-lnormaliz-symlink"                   >> "$CONFIG_TMP"
  echo "NORMALIZ_VERSION="                                    >> "$CONFIG_TMP"
fi
echo                                                          >> "$CONFIG_TMP"

# ### list of libraries
# ### moved here from "fixed_part2"  AMB 01/09/2015
echo "LDLIBS=$LDFPIC \$(COCOA_LIB) -L\$(EXTLIBS)/lib  \$(FROBBY_LDLIBS)  \$(GFAN_LDLIBS)  \$(CDD_LDLIBS)  \$(GSL_LDLIBS)  \$(NORMALIZ_LDLIBS)  \$(GMPXX_LDLIB)  \$(GMP_LDLIB)  \$(SOCKET_LIB)  \$(BOOST_LDLIBS)  \$(READLINE_LDLIBS)" >> "$CONFIG_TMP"

##################################################################
# Some platforms need a special library to use sockets.

OSNAME=`uname`
if [ "$OSNAME" = "SunOS" ]
then
  SOCKET_LIB="-lsocket -lnsl"
fi

if [ -n "$SOCKET_LIB" ]
then
  echo "SOCKET_LIB=$SOCKET_LIB"                               >> "$CONFIG_TMP"
fi


##################################################################
# Append the second fixed part to $CONFIG_TMP, then rename to $CONFIG_FILE

/bin/cat "$CONFIG_DIR/fixed_part2"                            >> "$CONFIG_TMP"
/bin/mv "$CONFIG_TMP" "$CONFIG_FILE"

##################################################################
# Configuration completed successfully.  Print final message.

# Removing .last-makedepend forces a rebuild of the dependencies files
# (necessary e.g. if we change versions of external libs)
/bin/rm -f .last-makedepend

echo
echo "-------------------------------------------------------"
echo "CoCoALib configuration process complete."
echo "Configuration info saved in file $CONFIG_FILE"
if [ -n "$WARNINGS" ]
then
  echo "======================================================="
  echo "**WARNINGS**  $WARNINGS"
fi
echo "-------------------------------------------------------"
