#!/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:
CONFIG_DIR=configuration
CONFIG_FILE="$CONFIG_DIR/autoconf.mk"

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

##################################################################
# Special handling for the arg "--again" (recalls args from previous invocation)
# This section puts the script args into ARGS.
if [ "XXX$*" \!= "XXX--again" ]
then
  ARGS="$@"
else
  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
fi


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

ORIG_CMD="$0 $ARGS"

MODE=OPT  # default optimization/debugging flag

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

    ( --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=` ;;

    ( --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-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 ;;

    ( --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 "  --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 "  --mode=ARG\t\tspecify OPT/DEBUG1/DEBUG2/PROFILE [default: OPT]"
       echo -e "\nOptional libraries:"
       echo -e "  --with-libfrobby=ARG\tspecify location of the file libfrobby.a"
       echo -e "\t\t\t[default is no libfrobby]"
       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 directories:"
       echo -e "  --prefix=PREFIX\tinstall in PREFIX/include & PREFIX/lib"
       echo -e "\t\t\t[default: /usr/local]"
       echo -e "  --threadsafe-hack\t\tcompile using a hack for threadsafety"
       exit;;

    ( * )
       echobox "Ignoring parameter \"$option\"" ;;
  esac
done

##################################################################
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


#############################################################################
# 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"`
  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_OUT=(`$SCRIPT_DIR/gmp-try-default.sh`)  # uses CXX & CXXFLAGS
  if [ $? -eq 0 ]
  then
    # System defaults for GMP work fine.
    GMP_VERSION=${GMP_TRY_OUT[0]}
    HAVE_DEFAULT_GMP=yes
    GMP_LIB="-lgmp"
    if [ ${#GMP_TRY_OUT[*]} -eq 1 ]
    then
      GMPXX_LIB="-lgmpxx"
      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
      GMP_LIB="$GMP_MESG"
    fi
  fi
fi

# For default GMP library we have the version already
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
  fi
  GMP_H="$GMP_INC_DIR/gmp.h"
  GMP_VERSION=`$SCRIPT_DIR/gmp-version-from-hdr.sh "$GMP_H"`
  if [ $? -ne 0 ]
  then
    # GMP version check had trouble and printed out an error mesg.
    exit 1
  fi
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

## The following could also be useful for HAVE_DEFAULT_GMP but it then needs update to be more flexible.

if [ $HAVE_DEFAULT_GMP = "no" ]
then
CXXFLAGS_FOR_GMP=`$SCRIPT_DIR/gmp-cxx-flags.sh "$GMP_H"`
# If user supplied CXXFLAGS, check they are compatible with GMP
if [ -n "$CXXFLAGS" ]
then
  $SCRIPT_DIR/gmp-check-cxxflags.sh "$CXX" "$CXXFLAGS $CXXFLAGS_FOR_GMP" "$GMP_H" "$GMP_LIB" &&
  $SCRIPT_DIR/gmp-check-cxxflags.sh "$CXX" "$CXXFLAGS_FOR_GMP $CXXFLAGS" "$GMP_H" "$GMP_LIB"
  if [ $? -ne 0 ]
  then
    echo "ERROR: Supplied value of CXXFLAGS, namely \"$CXXFLAGS\""     > /dev/stderr
    echo "ERROR: is not compatible with GMP library in \"$GMP_LIB\""   > /dev/stderr
    exit 1
  fi
fi

# 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_BASE=`basename "$GMP_LIB"`
GMP_EXTN=`$SCRIPT_DIR/extn.sh "$GMP_BASE"`
GMPXX_LIB=`dirname "$GMP_LIB"`/libgmpxx.$GMP_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

# Tell user which version of GMP we are using.
echo "Using GMP version $GMP_VERSION:"
echo "  library is in \"$GMP_LIB\""
echo "  header  is in \"$GMP_INC_DIR/gmp.h\""
echo

else # $HAVE_DEFAULT_GMP = "yes"
  if [ $HAVE_GMPXX = yes ]
  then
    echo "Using GMP version $GMP_VERSION with gmpxx at system default location"
  else
    echo "Using GMP version $GMP_VERSION without gmpxx at system default location"
  fi
  echo
fi


##################################################################
# Check whether "qmake" is available -- needed for building the GUI
QMAKE=`which qmake 2>&1 >/dev/null`
if [ $? = 0 ]
then
  HAVE_QMAKE=yes
else
  HAVE_QMAKE=no
fi


##################################################################
# BOOST installation
# Since BOOST is not (currently) essential for CoCoALib, we do
# not give an error if the boost-find-hdrs 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`
  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_LIBS
    BOOST_LIB_NOT_FOUND=
    # Check that the BOOST libs are compatible with CXX and CXXFLAGS
    BOOST_MESG=`$SCRIPT_DIR/boost-check-arch.sh "$CXX" "$CXXFLAGS $CXXFLAGS_FOR_GMP -I$BOOST_INC_DIR" "-L$BOOST_LIB_DIR $BOOST_LIBS"`
    if [ $? -ne 0 ]
    then
      BOOST_LIB_NOT_FOUND="$BOOST_MESG"
      BOOST_LIB_DIR=
      BOOST_LIBS=
    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"
  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

#############################################################################
# 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=`cd "$FROBBY_LIB_DIR"; pwd`/`basename "$FROBBY_LIB"`
  FROBBY_DIR=`cd "$FROBBY_LIB_DIR"/..; pwd`
  FROBBY_FLAG="-DCoCoA_WITH_FROBBY"
else
  HAVE_FROBBY=no
fi


##################################################################
# 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_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\"`"
  else
    GSL_INC_DIR="`dirname \"$GSL_LIB_DIR\"`/include"
  fi
  GSL_FLAG="-DCoCoA_WITH_GSL"
  if [ \! -d "$GSL_INC_DIR" -o \! -d "$GSL_INC_DIR/gsl" ]
  then
    echo "ERROR: Did not find GSL header file dir where expected: \"$GSL_LIB_DIR/gsl\""
    exit 1
  fi
else
  HAVE_GSL=no
fi


##################################################################
# 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_DIR=`dirname "$NORMALIZ_LIB"`
  NORMALIZ_LIB=`cd "$NORMALIZ_LIB_DIR"; pwd`/`basename "$NORMALIZ_LIB"`
  NORMALIZ_DIR=`cd "$NORMALIZ_LIB_DIR/.."; pwd`
  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
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"
  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 $FROBBY_FLAG $GSL_FLAG $NORMALIZ_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"


echo "The C++ compiler is $CXX"


##################################################################
##################################################################
# 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_FILE"
echo "# Created automatically by the configure script."       >> "$CONFIG_FILE"
echo "# Created on  `date \"+%Y-%m-%d  at time  %H:%M:%S\"`"  >> "$CONFIG_FILE"
echo "# Command was: "                                        >> "$CONFIG_FILE"
echo "# $ORIG_CMD"                                            >> "$CONFIG_FILE"
echo                                                          >> "$CONFIG_FILE"

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

##################################################################
# 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_FILE"
echo "VERSION=$VERSION"                                       >> "$CONFIG_FILE"
echo                                                          >> "$CONFIG_FILE"

# Installation command and directory (just placeholders, 20140323)
echo "INSTALL=install"                                        >> "$CONFIG_FILE"
echo "INSTALL_DIR=/usr/local"                                 >> "$CONFIG_FILE"
echo                                                          >> "$CONFIG_FILE"

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

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

#############################################################################
# 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_FILE"
echo "$HASH_OPT CXXFLAGS_SPECIFIC=$CXXFLAGS_OPT"              >> "$CONFIG_FILE"

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

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


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

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


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


# Add appropriate definitions to the config file, and version number in a comment.
echo "# We use the following GMP installation:"               >> "$CONFIG_FILE"
echo "GMP_VERSION=$GMP_VERSION"                               >> "$CONFIG_FILE"
echo "GMP_LIB=\"$GMP_LIB\""                                   >> "$CONFIG_FILE"
if [ $HAVE_GMPXX = "yes" ]
then
  echo "GMPXX_LIB=\"$GMPXX_LIB\""                             >> "$CONFIG_FILE"
fi
if [ $HAVE_DEFAULT_GMP = "yes" ]
then
  echo "GMP_INC_DIR="                                         >> "$CONFIG_FILE"
  echo "GMP_INCLUDE="                                         >> "$CONFIG_FILE"
else
  echo "GMP_INC_DIR=\"$GMP_INC_DIR\""                         >> "$CONFIG_FILE"
  ## Anna-20090515:  !!WARNING!! -isystem option did not work on my G4
  echo "GMP_INCLUDE=-isystem \$(GMP_INC_DIR)"                 >> "$CONFIG_FILE"
fi
echo                                                          >> "$CONFIG_FILE"


echo "HAVE_QMAKE=$HAVE_QMAKE"                                 >> "$CONFIG_FILE"
echo                                                          >> "$CONFIG_FILE"

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


##################################################################
# settings for optional external libs
echo
echo "OPTIONAL external libraries:"
echo "# Frobby settings:"                                     >> "$CONFIG_FILE"
if [ $HAVE_FROBBY = "yes" ]
then
  # Recall that GMPXX_LIB is the libgmpxx.a library
  echo "Using Frobby:   $FROBBY_LIB" #-- with Frobby ------------
  echo "HAVE_FROBBY=yes"                                      >> "$CONFIG_FILE"
  echo "FROBBY_LIBS=\"$FROBBY_LIB\"  \$(GMPXX_LIB)"           >> "$CONFIG_FILE"
  echo "FROBBY_INC_DIR=\"$FROBBY_DIR/src\""                   >> "$CONFIG_FILE"
  echo "FROBBY_INCLUDE=-I \$(FROBBY_INC_DIR)"                 >> "$CONFIG_FILE"
  echo "FROBBY_VERSION="                                      >> "$CONFIG_FILE"
else
  echo "Not using Frobby" #-- without Frobby ------------
  echo "HAVE_FROBBY=no"                                       >> "$CONFIG_FILE"
fi
echo                                                          >> "$CONFIG_FILE"

echo "# GSL settings:"                                        >> "$CONFIG_FILE"
if [ $HAVE_GSL = "yes" ]
then
  echo "Using GSL:      $GSL_LIB" #-- with GSL ------------
  echo "HAVE_GSL=yes"                                         >> "$CONFIG_FILE"
  echo "GSL_LIBS=  \"$GSL_LIB\" -lblas -llapack"              >> "$CONFIG_FILE"
  echo "GSL_INC_DIR=\"$GSL_INC_DIR\""                         >> "$CONFIG_FILE"
  echo "GSL_INCLUDE=-I \$(GSL_INC_DIR)"                       >> "$CONFIG_FILE"
  echo "GSL_VERSION="                                         >> "$CONFIG_FILE"
else
  echo "Not using GSL" #-- without GSL ------------
  echo "HAVE_GSL=no"                                          >> "$CONFIG_FILE"
fi
echo                                                          >> "$CONFIG_FILE"


echo "# Normaliz settings:"                                   >> "$CONFIG_FILE"
if [ $HAVE_NORMALIZ = "yes" ]
then
  # Recall that GMPXX_LIB is the libgmpxx.a library
  echo "Using Normaliz: $NORMALIZ_LIB" #-- with Normaliz ------------
  echo "HAVE_NORMALIZ=yes"                                    >> "$CONFIG_FILE"
  echo "NORMALIZ_LIBS=\"$NORMALIZ_LIB\" \"$GMPXX_LIB\""       >> "$CONFIG_FILE"
  echo "NORMALIZ_INC_DIR=\"$NORMALIZ_DIR\""                   >> "$CONFIG_FILE"
  echo "NORMALIZ_INCLUDE=-I \$(NORMALIZ_INC_DIR)"             >> "$CONFIG_FILE"
  echo "NORMALIZ_VERSION="                                    >> "$CONFIG_FILE"
else
  echo "Not using Normaliz" #-- without Normaliz ------------
  echo "HAVE_NORMALIZ=no"                                     >> "$CONFIG_FILE"
fi
echo                                                          >> "$CONFIG_FILE"


##################################################################
# 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_FILE"
fi


##################################################################
# Append the second fixed part to $CONFIG_FILE

/bin/cat "$CONFIG_DIR/fixed_part2"                            >> "$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"
echo "-------------------------------------------------------"
