#! /bin/sh

# ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
# ┃ Configure script for MUESLI.                                               ┃
# ┃                                                                            ┃
# ┃ É. Canot -- IPR/CNRS -- 20 Nov 2024                                        ┃
# ┃                                                                            ┃
# ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

# Set to 0 if you don't like colors in the output.
COLORS_ACTIVATED=1

# Check if the selected shell for running the current script is
# compatible with 'echo -e'
if [ "`echo -e item`" = "item" ]; then
   export ECHO_OPT=-e
else
   export ECHO_OPT=
fi

# The three following colors are ok for both a dark or a light background
if test $COLORS_ACTIVATED = 1; then
  export COLOR_NORMAL=" echo -n $ECHO_OPT \033[0m"
  export COLOR_ERROR="  echo -n $ECHO_OPT \033[1;31m" # Red
  export COLOR_WARNING="echo -n $ECHO_OPT \033[1;35m" # Magenta
  export COLOR_INFO="   echo -n $ECHO_OPT \033[0;37m" # Gray
fi

usage() {
  $COLOR_INFO
  echo
  echo "  configure can take the following options:"
  echo "          --help"
  echo
  echo "          --f90=<f90_compiler>"
  echo "          --cc=<c_compiler>"
  echo "          --c++=<c++_compiler>"
  echo "          --libstdc++=<libstdc++_path>"
  echo "          --ldopt=<linking_options>"
  echo "          --addlib=<additional_library>"
  echo "          --prefix=<dirname>     (installation directory)"
  echo
  echo "          --blas=<path_for_BLAS_library>"
  echo "          --lapack=<path_for_LAPACK_library>"
  echo "          --blas_lapack_flag=<flag for vendor BLAS/LAPACK version>"
  echo
  echo "          --no-X11               (for disabling X11)"
  echo
  echo "          --cache                (save arguments in cache)"
  echo "          --restore              (restore arguments from cache)"
  echo
  echo "  ('=' is optional after option names and may be replaced by one space)"
  $COLOR_NORMAL
  echo
}

abort() {
  $COLOR_ERROR
  echo
  echo "  *** aborting ***"
  echo
  if test $# = 0; then
    echo "  (if you think that this is a bug, mail to: Edouard.Canot@univ-rennes.fr)"
  fi
  $COLOR_NORMAL
  echo
  exit 1
}

force_no_X11=0
save_in_cache=0
read_in_cache=0

#
# Process argument list
#
prev=

for option; do
  if test -n "$prev"; then
    eval "$prev=\$option"
    prev=
    continue
  fi

  case "$option" in
  -*=*) optarg=`echo "$option" | sed 's/[-+_a-zA-Z0-9]*=//'` ;;
  *) optarg= ;;
  esac

  case "$option" in

  -f90 | --f90)
    prev=f90 ;;

  -f90=* | --f90=*)
    f90="$optarg" ;;

  -cc | --cc)
    prev=cc ;;

  -cc=* | --cc=*)
    cc="$optarg" ;;

  -c++ | --c++)
    prev=cxx ;;

  -c++=* | --c++=*)
    cxx="$optarg" ;;

  -libstdc++ | --libstdc++)
    prev=libstdcxx ;;

  -libstdc++=* | --libstdc++=*)
    libstdcxx="$optarg" ;;

  -ldopt | --ldopt)
    prev=ldopt ;;

  -ldopt=* | --ldopt=*)
    ldopt="$optarg" ;;

  -blas | --blas)
    prev=blas ;;

  -blas=* | --blas=*)
    blas="$optarg" ;;

  -lapack | --lapack)
    prev=lapack ;;

  -lapack=* | --lapack=*)
    lapack="$optarg" ;;

  -addlib | --addlib)
    prev=addlib ;;

  -addlib=* | --addlib=*)
    addlib="$optarg" ;;

  -prefix | --prefix)
    prev=prefix ;;

  -prefix=* | --prefix=*)
    prefix="$optarg" ;;

  -blas_lapack_flag | --blas_lapack_flag)
    prev=blas-lapack ;;

  -blas_lapack_flag=* | --blas_lapack_flag=*)
    blas_lapack_flag="$optarg" ;;

  -no-X11 | --no-X11)
    no_X11=1
    force_no_X11=1 ;;

  -cache | --cache)
    save_in_cache=1 ;;

  -restore | --restore)
    read_in_cache=1 ;;

  -help | --help)
    usage
    exit 0
    ;;

  *)
    echo
    echo "  `basename $0`: error: $option: invalid option" >&2
    echo
    exit 1
    ;;

  esac
done

if test -n "$prev"; then
  echo
  echo "  `basename $0`: error: missing argument to '--$prev'" >&2
  echo
  exit 1
fi

if test -n "$blas_lapack_flag" && (test -n "$blas" || test -n "$lapack") ; then
  echo
  echo "  `basename $0`: error: '--blas_lapack_flag=' cannot be used with '--blas=' or '--lapack='!" >&2
  echo
  exit 1
fi

if test $read_in_cache = 1 && (test $save_in_cache = 1     || \
                               test -n "$f90"              || \
                               test -n "$cc"               || \
                               test -n "$cxx"              || \
                               test -n "$libstdcxx"        || \
                               test -n "$ldopt"            || \
                               test -n "$blas"             || \
                               test -n "$lapack"           || \
                               test -n "$addlib"           || \
                               test -n "$blas_lapack_flag" || \
                               test -n "$prefix") ; then
  echo
  echo "  `basename $0`: error: '--restore' cannot be used with any other option!" >&2
  echo
  exit 1
fi

if test $save_in_cache = 1; then
  echo
  echo -n $ECHO_OPT "  Saving args in 'configure.cache'... \t "
  echo -n "$0" > configure.cache
  if test -n "$f90"; then
    echo -n " --f90=\"$f90\"" >> configure.cache
  fi
  if test -n "$cc"; then
    echo -n " --cc=\"$cc\"" >> configure.cache
  fi
  if test -n "$cxx"; then
    echo -n " --c++=\"$cxx\"" >> configure.cache
  fi
  if test -n "$libstdcxx"; then
    echo -n " --libstdc++=\"$libstdcxx\"" >> configure.cache
  fi
  if test -n "$ldopt"; then
    echo -n " --ldopt=\"$ldopt\"" >> configure.cache
  fi
  if test -n "$blas"; then
    echo -n " --blas=\"$blas\"" >> configure.cache
  fi
  if test -n "$lapack"; then
    echo -n " --lapack=\"$lapack\"" >> configure.cache
  fi
  if test -n "$addlib"; then
    echo -n " --addlib=\"$addlib\"" >> configure.cache
  fi
  if test -n "$blas_lapack_flag"; then
    echo -n " --blas_lapack_flag=\"$blas_lapack_flag\"" >> configure.cache
  fi
  if test -n "$prefix"; then
    echo -n " --prefix=\"$prefix\"" >> configure.cache
  fi
  echo >> configure.cache
  echo "done"
fi

#
# Default settings
#
if test "$prefix" = ""; then
  prefix="/usr/local"
elif ( echo "$prefix" | grep -q ' '); then
  # prefix (for installation) must not contain a space character...
  $COLOR_ERROR
  echo
  echo "  ERROR: the installation path (given with '--prefix') cannot contain space!"
  $COLOR_NORMAL
  abort
fi

if test $read_in_cache = 1; then
  echo
  echo "  Restoring args from 'configure.cache'..."
  if test ! -f configure.cache; then
    echo
    echo "  `basename $0`: error: 'configure.cache' not found!" >&2
    echo
    exit 1
  fi
  exec sh configure.cache
fi

export f90_comp="$f90"
export c_comp="$cc"
export cxx_comp="$cxx"
export libstdcxx_path="$libstdcxx"

gcc_lib=`echo $ldopt | awk -F' |,' '{print $3}'`
export gcc_lib

export ld_opt="$ldopt"
export blas_path="$blas"
export lapack_path="$lapack"
export add_lib="$addlib"
export blas_lapack_vendor_flag="$blas_lapack_flag"

LENGTH_VAR=`cat ../VERSION | wc -c`
NB_SPACES=`expr 30 - $LENGTH_VAR`
# generate a string of 'NB_SPACES' spaces (used below)
SPACES_STRING=`head -c $NB_SPACES < /dev/zero | tr '\0' ' '`

$COLOR_INFO
echo
echo    "           ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"
echo    "           ┃   Configure script for the MUESLI Fortran library   ┃"
echo -n "           ┃               version: "; echo -n `cat ../VERSION`; echo -n "$SPACES_STRING"; echo "┃"
echo    "           ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"
$COLOR_NORMAL

# == Check that configure is executed from a COMPILER directory ================

VALID_DIRS="GNU_GFC INTEL_IFC"
CURRENT_DIR=`pwd`
# Hereafter double quotes (") around $CURRENT_DIR is required; else
# the configure will fail if the path contains any blank!
CURRENT_DIR=`basename "$CURRENT_DIR"`
OK=0
for DIR in $VALID_DIRS; do
  if test $CURRENT_DIR = $DIR; then
    OK=1
  fi
done
if test $OK = 0; then
  $COLOR_ERROR
  echo
  echo "  ERROR: the 'configure' script must be called from one of the"
  echo "         following directories:"
  echo "         $VALID_DIRS"
  $COLOR_NORMAL
  abort
fi

# == Check for OS and Hardware =================================================

# The 'which' command must be present
RES=`which which 2>/dev/null`
if test $? != 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: the command 'which' is not available!"
  echo "         -> please install it"
  $COLOR_NORMAL
  abort
fi

export DARWIN='"no"'

# Linux, Darwin, FreeBSD
SYS_PATH="/usr"
SO_EXT="so"
echo -n $ECHO_OPT "\n  Checking for Operating System... \t "
OS=`uname -s`
if test $OS = "Linux"; then
  echo $OS
elif test $OS = "Darwin"; then
  echo $OS
  SO_EXT="dylib"
  DARWIN='"yes"'
elif test $OS = "FreeBSD"; then
  echo $OS
else
  echo
  $COLOR_ERROR
  echo "  ERROR: unknown OS -- valid are currently:"
  echo "         'Linux', 'Darwin' and 'FreeBSD'"
  $COLOR_NORMAL
  abort
fi

if test $OS = "Darwin"; then
  echo -n $ECHO_OPT "  Product Name... \t\t\t "
  set `sw_vers -productName`
  RESULT=$1
  echo $RESULT
  echo -n $ECHO_OPT "  Product Version... \t\t\t "
  set `sw_vers -productVersion`
  RESULT=$1
  echo $RESULT
fi

if test $OS = "Linux"; then
  # Checking for the distribution and the release number (this is required
  # to apply workarounds to some bugs)
  PROG=`which lsb_release 2>/dev/null`
  if test $? = 1; then
    echo
    $COLOR_ERROR
    echo "  ERROR: the command 'lsb_release' (Linux Standard Base) is not available!"
    echo "         -> please install it (see the Muesli Installation Guide)"
    $COLOR_NORMAL
    abort
  fi
  echo -n $ECHO_OPT "  LSB Distribution name... \t\t "
  set `lsb_release -i`
  LSB_DISTRO=$3
  echo $LSB_DISTRO
  echo -n $ECHO_OPT "  LSB Release number... \t\t "
  set `lsb_release -r`
  LSB_RELEASE=$2
  echo $LSB_RELEASE
  # Checking for a Debian-based system (like Ubuntu), because not all
  # libraries go to /usr/lib64 on an x86_64 architecture...
  # (e.g. BLAS, LAPACK, at least)
  echo -n $ECHO_OPT "  Is it a Debian-based Linux? \t\t "
  if test -f /etc/debian_version; then
    echo "yes"
    DEBIAN=1
  else
    echo "no"
    DEBIAN=0
  fi
else
  DEBIAN=0
fi
echo

echo -n $ECHO_OPT "  Checking for Hardware... \t\t "
if test $OS = "Linux"; then
  # i686, x86_64, ia64
  HW=`uname -m`
  if test $HW = "x86_64" || test $HW = "ia64"; then
    ADDRESS_SIZE=64
  else
    ADDRESS_SIZE=32
  fi
elif test $OS = "Darwin"; then
  # Mac OS X
  HW=`uname -m`
  if test $HW = "i386"; then
    ADDRESS_SIZE=32
  else
    if test $HW = "x86_64"; then
      ADDRESS_SIZE=64
    else
      echo
      $COLOR_ERROR
      echo "  ERROR: unknown Hardware -- valid are currently:"
      echo "         'i386', 'x86_64'"
      $COLOR_NORMAL
      abort
    fi
  fi
elif test $OS = "FreeBSD"; then
  # amd64
  HW=`uname -m`
  ADDRESS_SIZE=64
fi
echo $HW

export HW

echo -n $ECHO_OPT "  Checking for Address size... \t\t "
if test "$ADDRESS_SIZE" = ""; then
  $COLOR_ERROR
  echo "  Internal error: ADDRESS_SIZE is not set!"
  echo "    -> please report this bug."
  $COLOR_NORMAL
  abort
fi
echo $ADDRESS_SIZE
if test $ADDRESS_SIZE = "64"; then
  X86_64='"yes"'
else
  X86_64='"no"'
fi

if test $OS = "Linux"; then
  if test $HW = "x86_64"; then
    #--- Under 64bit, check for a directory named '/usr/lib/x86_64-linux-gnu',
    #    and set ADD_LIB_PATH accordingly
    echo -n $ECHO_OPT "\n  Does \"x86_64-linux-gnu\" exist? \t "
    if test -d "/usr/lib/x86_64-linux-gnu"; then
      ADD_LIB_PATH="/usr/lib/x86_64-linux-gnu"
      echo "yes"
    else
      ADD_LIB_PATH=
      echo "no"
    fi
  else
    #--- Under 32bit, check for a directory named '/usr/lib/i386-linux-gnu',
    #    and set ADD_LIB_PATH accordingly
    echo -n $ECHO_OPT "\n  Does \"i386-linux-gnu\" exist? \t\t "
    if test -d "/usr/lib/i386-linux-gnu"; then
      ADD_LIB_PATH="/usr/lib/i386-linux-gnu"
      echo "yes"
    else
      ADD_LIB_PATH=
      echo "no"
    fi
  fi
fi

if test $OS = "Darwin"; then
  echo -n $ECHO_OPT "\n  Does \"/opt/X11/lib\" exist? \t\t "
  if test -d "/opt/X11/lib"; then
    ADD_LIB_PATH="/opt/X11/lib"
    echo "yes"
  else
    ADD_LIB_PATH=
    echo "no"
  fi
fi

# == Check for GNU make ========================================================

echo -n $ECHO_OPT "\n  Checking for GNU-make... \t\t "
GNU_MAKE=0
PROG=`which make 2>/dev/null`
if test $? = 0; then
  if test -n "`$PROG --version 2>/dev/null | head -n 1 | grep GNU`"; then
    echo "make"
    GNU_MAKE=1
  else
    GNU_MAKE=0
  fi
fi
if test "$GNU_MAKE" = "0"; then
  PROG=`which gmake 2>/dev/null`
  if test $? = 0; then
    if test -n "`$PROG --version 2>/dev/null | head -n 1 | grep GNU`"; then
      echo "gmake"
      GNU_MAKE=1
    else
      $COLOR_ERROR
      echo "not found"
      GNU_MAKE=0
    fi
  else
    $COLOR_ERROR
    echo "not found"
      GNU_MAKE=0
  fi
fi
if test "$GNU_MAKE" = "0"; then
  echo
  $COLOR_ERROR
  echo "  ERROR: GNU-make is required to build MUESLI!"
  echo "    -> please consider installing GNU-make."
  $COLOR_NORMAL
  abort
fi

$COLOR_INFO
echo
echo "  ─────────────────────────── checking for FML ───────────────────────────"
$COLOR_NORMAL

# Intel embed Blas/Lapack in the MKL library (simply put --blas_lapack_flag="mkl"
# to use Blas/Lapack).
# Then, the following test about the explicit presence of Blas/Lapack is done
# only if the variable 'blas_lapack_flag' is empty...
if test -z "$blas_lapack_flag"; then
# == check for BLAS ============================================================

# Setting the BLAS path
BLAS_LIB=0
if test "$blas_path" = ""; then
  blas_path_given=0
  if test $HW = "x86_64"; then
    # not all 64-bit OS has a /usr/lib64 directory... we must check
    if test -d $SYS_PATH/lib64; then
      blas_path="$SYS_PATH/lib64"
    else
      blas_path="$SYS_PATH/lib"
    fi
  else
    blas_path="$SYS_PATH/lib"
  fi
  # except for Debian-based OS...
  if test $DEBIAN = "1"; then
    blas_path="$SYS_PATH/lib"
  fi
else
  # BLAS path is given by the user... it should contain the appropriate library
  blas_path_given=1
fi

# Now the test
echo -n $ECHO_OPT "\n  Checking for BLAS... \t\t\t "

BLAS_SO=0
LIB=`ls $blas_path/libblas.$SO_EXT 2>/dev/null`
if test $? = 0; then
  LIB=`ls $blas_path/libblas.$SO_EXT 2>/dev/null | head -n 1`
  echo $LIB
  BLAS_LIB=1
  BLAS_SO=1
else
  if test $blas_path_given = 0; then
    if test ! -z $ADD_LIB_PATH; then
      LIB=`ls $ADD_LIB_PATH/libblas.$SO_EXT 2>/dev/null`
      if test $? = 0; then
        LIB=`ls $ADD_LIB_PATH/libblas.$SO_EXT 2>/dev/null | head -n 1`
        echo $LIB
        BLAS_LIB=1
        blas_path=$ADD_LIB_PATH
        BLAS_SO=1
      fi
    fi
  fi
fi

if test $BLAS_SO = 0; then
  LIB=`ls $blas_path/libblas.a 2>/dev/null`
  if test $? = 0; then
    LIB=`ls $blas_path/libblas.a 2>/dev/null | head -n 1`
    echo $LIB
    BLAS_LIB=1
  else
    if test $blas_path_given = 0; then
      if test ! -z $ADD_LIB_PATH; then
        LIB=`ls $ADD_LIB_PATH/libblas.a 2>/dev/null`
        if test $? = 0; then
          LIB=`ls $ADD_LIB_PATH/libblas.a 2>/dev/null | head -n 1`
          echo $LIB
          BLAS_LIB=1
          blas_path=$ADD_LIB_PATH
        else
          $COLOR_WARNING
          echo "not found"
          $COLOR_NORMAL
        fi
      else
        $COLOR_WARNING
        echo "not found"
        $COLOR_NORMAL
      fi
    fi
  fi
fi

if test $BLAS_LIB = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: you don't have a usable BLAS library!"
  echo "  -> please consider installing the BLAS library."
  echo "     (see also appendix A of the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
  abort
fi

# == Check for LAPACK ==========================================================

# Setting the LAPACK path
LAPACK_LIB=0
if test "$lapack_path" = ""; then
  lapack_path_given=0
  if test $HW = "x86_64"; then
    # Not all 64-bit OS has a /usr/lib64 directory... we must check
    if test -d $SYS_PATH/lib64; then
      lapack_path="$SYS_PATH/lib64"
    else
      lapack_path="$SYS_PATH/lib"
    fi
  else
    lapack_path="$SYS_PATH/lib"
  fi
  # Except for Debian-based OS...
  if test $DEBIAN = "1"; then
    lapack_path="$SYS_PATH/lib"
  fi
else
  # BLAS path is given by the user... it should contain the appropriate library
  lapack_path_given=1
fi

# Now the test
echo -n $ECHO_OPT "  Checking for LAPACK... \t\t "

blas_lapack_both_archived='"no"'
LAPACK_SO=0
LIB=`ls $lapack_path/liblapack.$SO_EXT 2>/dev/null`
if test $? = 0; then
  LIB=`ls $lapack_path/liblapack.$SO_EXT 2>/dev/null | head -n 1`
  echo $LIB
  LAPACK_LIB=1
  LAPACK_SO=1
else
  if test $blas_path_given = 0; then
    if test ! -z $ADD_LIB_PATH; then
      LIB=`ls $ADD_LIB_PATH/liblapack.$SO_EXT 2>/dev/null`
      if test $? = 0; then
        LIB=`ls $ADD_LIB_PATH/liblapack.$SO_EXT 2>/dev/null | head -n 1`
        echo $LIB
        LAPACK_LIB=1
        lapack_path=$ADD_LIB_PATH
        LAPACK_SO=1
      fi
    fi
  fi
fi

if test $LAPACK_SO = 0; then
  LIB=`ls $lapack_path/liblapack.a 2>/dev/null`
  if test $? = 0; then
    LIB=`ls $lapack_path/liblapack.a 2>/dev/null | head -n 1`
    echo $LIB
    LAPACK_LIB=1
    if test ! $BLAS_SO = 1; then
      blas_lapack_both_archived='"yes"'
    fi
  else
    if test $blas_path_given = 0; then
      if test ! -z $ADD_LIB_PATH; then
        LIB=`ls $ADD_LIB_PATH/liblapack.a 2>/dev/null`
        if test $? = 0; then
          LIB=`ls $ADD_LIB_PATH/liblapack.a 2>/dev/null | head -n 1`
          echo $LIB
          LAPACK_LIB=1
          lapack_path=$ADD_LIB_PATH
          if test ! $BLAS_SO = 1; then
            blas_lapack_both_archived='"yes"'
          fi
        else
          $COLOR_WARNING
          echo "not found"
          $COLOR_NORMAL
        fi
      else
        $COLOR_WARNING
        echo "not found"
        $COLOR_NORMAL
      fi
    fi
  fi
fi

if test $LAPACK_LIB = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: you don't have a usable LAPACK library!"
  echo "  -> please consider installing the LAPACK library."
  echo "     (see appendix A of the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
  abort
fi

fi # BLAS and LAPACK test

# == Check for zlib ============================================================

ZLIB_1_2=0
echo $ECHO_OPT "\n  Checking for ZLIB:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # Not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    zlib_path="$SYS_PATH/lib64"
  else
    zlib_path="$SYS_PATH/lib"
  fi
else
  zlib_path="$SYS_PATH/lib"
fi

LIB=`ls $zlib_path/libz.* 2>/dev/null`
if test $? = 0; then
  LIB=`ls $zlib_path/libz.* 2>/dev/null | head -n 1`
  echo $LIB
  ZLIB_DIR=$zlib_path
  ZLIB_1_2=1
else
  if test ! -z $ADD_LIB_PATH; then
    LIB=`ls $ADD_LIB_PATH/libz.* 2>/dev/null`
    if test $? = 0; then
      LIB=`ls $ADD_LIB_PATH/libz.* 2>/dev/null | head -n 1`
      echo $LIB
      ZLIB_DIR=$ADD_LIB_PATH
      ZLIB_1_2=1
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
fi

if test $ZLIB_1_2 = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR you don't have the zlib library!"
  echo "  -> please consider installing 'zlib'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
else
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  INC=`ls $SYS_PATH/include/zlib.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
  else
    $COLOR_WARNING
    echo "zlib.h not found"
    $COLOR_NORMAL
    ZLIB_1_2=0
    ZLIB_DIR=
  fi
  if test $ZLIB_1_2 = 0; then
    echo
    $COLOR_ERROR
    echo "  ERROR you don't have the zlib headers!"
    echo "  -> please consider installing 'zlib'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
  fi
fi

# Additional include file for zlib: zconf.h
# Usually, it is stored at the same location as zlib.h but recent linux
# ubuntu versions (e.g. 13.04 and greater) put it in another place...
echo -n $ECHO_OPT "\t\t                 \t "
INC=`ls $SYS_PATH/include/zconf.h 2>/dev/null`
if test $? = 0; then
  echo $INC
  ZLIB_INCL=$SYS_PATH/include
else
  INC=`ls $SYS_PATH/include/x86_64-linux-gnu/zconf.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
    ZLIB_INCL=$SYS_PATH/include/x86_64-linux-gnu
  else
    $COLOR_WARNING
    echo "zconf.h not found"
    echo
    echo "  Warning: zconf.h can not be found!"
    echo "  -> data compression in file I/O will not be available."
    echo
    echo "  (if you think that this is a bug, because zlib.h is present,"
    echo "   mail to: Edouard.Canot@univ-rennes.fr)"
    $COLOR_NORMAL
    ZLIB_DIR=
    ZLIB='"no"'
  fi
fi

# == Check for readline ========================================================

READLINE='"no"'
READLINE_LIB=0
echo $ECHO_OPT "\n  Checking for READLINE:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # Not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    search_path="$SYS_PATH/lib64"
  else
    search_path="$SYS_PATH/lib"
  fi
else
  search_path="$SYS_PATH/lib"
fi
LIB=`ls $search_path/libreadline.* 2>/dev/null`
if test $? = 0; then
  LIB=`ls $search_path/libreadline.* 2>/dev/null | head -n 1`
  echo $LIB
  READLINE_LIB=1
else
  if test ! -z $ADD_LIB_PATH; then
    LIB=`ls $ADD_LIB_PATH/libreadline.* 2>/dev/null`
    if test $? = 0; then
      LIB=`ls $ADD_LIB_PATH/libreadline.* 2>/dev/null | head -n 1`
      echo $LIB
      READLINE_LIB=1
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
fi

if test $READLINE_LIB = 0; then
  echo
  $COLOR_WARNING
  echo "  Warning: you don't have the readline library!"
  echo "  -> command line editing (including history) will not be available,"
  echo "     therefore, please consider installing 'readline'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
fi

READLINE_INC=0
if test $READLINE_LIB = 1; then
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  INC=`ls $SYS_PATH/include/readline/readline.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
    READLINE_INC=1
    READLINE='"yes"'
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
  if test $READLINE_INC = 0; then
    echo
    $COLOR_WARNING
    echo "  Warning: you don't have the readline headers!"
    echo "  -> data compression in file I/O will not be available,"
    echo "     therefore, please consider installing 'readline'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
  fi
fi
export READLINE_INC

# == Check for convert (part of ImageMagick) ===================================

IM_CONVERT='"no"'
echo -n $ECHO_OPT "\n  Checking for 'convert'... \t\t "
PROG=`which convert 2>/dev/null`
if test $? = 0; then
  echo $PROG
  echo -n $ECHO_OPT "  Does it come from ImageMagick? \t "
  if test -n "`$PROG -version 2>/dev/null | head -n 1 | grep ImageMagick`"; then
    echo "yes"
    IM_CONVERT='"yes"'
  else
    echo "no"
    echo
    $COLOR_WARNING
    echo "  Warning: only XPM images will be supported in MUESLI."
    echo "    -> please consider installing 'ImageMagick'."
    $COLOR_NORMAL
  fi
else
  $COLOR_WARNING
  echo "not found"
  echo
  echo "  Warning: only XPM images will be supported in MUESLI."
  echo "    -> please verify your PATH variable, or consider"
  echo "       installing 'ImageMagick'."
  $COLOR_NORMAL
fi

NO_X11='"yes"'
if test $force_no_X11 = 0; then
$COLOR_INFO
echo
echo "  ─────────────────────────── checking for FGL ───────────────────────────"
$COLOR_NORMAL

# == Check for X11 =============================================================

X11_DIR=
X_11=
echo $ECHO_OPT "\n  Checking for X11:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    x11_path="$SYS_PATH/lib64"
  else
    x11_path="$SYS_PATH/lib"
  fi
else
  x11_path="/usr/lib"
fi
if test $OS = "Linux"; then
  LIB=`ls $x11_path/libX11.* 2>/dev/null`
  if test $? = 0; then
    LIB=`ls $x11_path/libX11.* 2>/dev/null | head -n 1`
    echo $LIB
    X11_DIR=$x11_path
    X_11=1
  else
    if test ! -z $ADD_LIB_PATH; then
      LIB=`ls $ADD_LIB_PATH/libX11.* 2>/dev/null`
      if test $? = 0; then
        LIB=`ls $ADD_LIB_PATH/libX11.* 2>/dev/null | head -n 1`
        echo $LIB
        X11_DIR=$ADD_LIB_PATH
        X_11=1
      else
        $COLOR_WARNING
        echo "not found"
        $COLOR_NORMAL
      fi
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  fi
elif test $OS = "Darwin"; then
  x11_path="/usr/X11/lib"
  LIB=`ls $x11_path/libX11.$SO_EXT 2>/dev/null`
  if test $? = 0; then
    LIB=`ls $x11_path/libX11.$SO_EXT 2>/dev/null | head -n 1`
    echo $LIB
    X11_DIR=$x11_path
    X_11=1
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
elif test $OS = "FreeBSD"; then
  echo
  $COLOR_WARNING
  echo "  Warning: detection of X11 library is not yet implemented for FreeBSD"
  echo "     (mail to: Edouard.Canot@univ-rennes.fr)"
  $COLOR_NORMAL
fi

if test "$X_11" = ""; then
  echo
  $COLOR_WARNING
  echo "  Warning: you must have the X11 library for graphics!"
  echo "  -> FGL will not be built!"
  echo "     therefore, please consider installing 'libX11'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
fi

X11_INCL=
if test "$X_11" = "1"; then
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  if test $OS = "Linux"; then
    x11_inc_path="/usr/include"
    INC=`ls $x11_inc_path/X11/Xlib.h 2>/dev/null`
    if test $? = 0; then
      echo $INC
      X11_INCL=$x11_inc_path
    else
      X11_DIR=
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  elif test $OS = "Darwin"; then
    x11_inc_path="/usr/X11/include"
    INC=`ls $x11_inc_path/X11/Xlib.h 2>/dev/null`
    if test $? = 0; then
      echo $INC
      X11_INCL=$x11_inc_path
    else
      X11_DIR=
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  elif test $OS = "FreeBSD"; then
    echo
    $COLOR_WARNING
    echo "  Warning: detection of X11 headers is not yet implemented for FreeBSD"
    echo "     (mail to: Edouard.Canot@univ-rennes.fr)"
    $COLOR_NORMAL
  fi
  if test "$X11_INCL" = ""; then
    echo
    $COLOR_WARNING
    echo "  Warning: you must have the X11 headers!"
    echo "  -> FGL will not be built!"
    echo "     therefore, please consider installing 'libX11'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
  else
    NO_X11='"no"'
  fi
fi

# == Check for freetype2 =======================================================

FREETYPE2=0
echo $ECHO_OPT "\n  Checking for FREETYPE2:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # Not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    ft_path="$SYS_PATH/lib64"
  else
    ft_path="$SYS_PATH/lib"
  fi
else
  ft_path="$SYS_PATH/lib"
fi

LIB=`ls $ft_path/libfreetype.* 2>/dev/null`
if test $? = 0; then
  LIB=`ls $ft_path/libfreetype.* 2>/dev/null | head -n 1`
  echo $LIB
  FT_DIR=$ft_path
  FREETYPE2=1
else
  if test ! -z $ADD_LIB_PATH; then
    LIB=`ls $ADD_LIB_PATH/libfreetype.* 2>/dev/null`
    if test $? = 0; then
      LIB=`ls $ADD_LIB_PATH/libfreetype.* 2>/dev/null | head -n 1`
      echo $LIB
      FT_DIR=$ADD_LIB_PATH
      FREETYPE2=1
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
fi

if test $FREETYPE2 = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: you don't have the freetype2 library!"
  echo "  -> antialiased display on X11 screen will not be available,"
  echo "     therefore, please consider installing 'freetype2'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
  abort
fi

if test $FREETYPE2 = 1; then
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  INC=`ls $SYS_PATH/include/freetype2/ft2build.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
    LIBFT='"yes"'
    FT_INCL=$SYS_PATH/include/freetype2
  else
    INC=`ls $ADD_LIB_PATH/../include/freetype2/ft2build.h 2>/dev/null`
    if test $? = 0; then
      echo $INC
      LIBFT='"yes"'
      FT_INCL=$ADD_LIB_PATH/../include/freetype2
    else
      $COLOR_WARNING
      echo "ft2build.h not found"
      $COLOR_NORMAL
      FREETYPE2=0
      FT_DIR=
      LIBFT='"no"'
    fi
  fi
  if test $FREETYPE2 = 0; then
    echo
    $COLOR_ERROR
    echo "  ERROR: you don't have the freetype2 headers!"
    echo "  -> antialiased display on X11 screen will not be available,"
    echo "     therefore, please consider installing 'freetype2'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
  abort
  fi
fi

# == Check for fontconfig ======================================================

FONTCONFIG=0
echo $ECHO_OPT "\n  Checking for FONTCONFIG:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # Not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    fc_path="$SYS_PATH/lib64"
  else
    fc_path="$SYS_PATH/lib"
  fi
else
  fc_path="$SYS_PATH/lib"
fi

LIB=`ls $fc_path/libfontconfig.* 2>/dev/null`
if test $? = 0; then
  LIB=`ls $fc_path/libfontconfig.* 2>/dev/null | head -n 1`
  echo $LIB
  FONTCONFIG=1
else
  if test ! -z $ADD_LIB_PATH; then
    LIB=`ls $ADD_LIB_PATH/libfontconfig.* 2>/dev/null`
    if test $? = 0; then
      LIB=`ls $ADD_LIB_PATH/libfontconfig.* 2>/dev/null | head -n 1`
      echo $LIB
      FONTCONFIG=1
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
fi

if test $FONTCONFIG = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: you don't have the fontconfig library!"
  echo "  -> antialiased display on X11 screen will not be available,"
  echo "     therefore, please consider installing 'fontconfig'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
  abort
fi

if test $FONTCONFIG = 1; then
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  INC=`ls $SYS_PATH/include/fontconfig/fontconfig.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
  else
    INC=`ls $ADD_LIB_PATH/../include/fontconfig/fontconfig.h 2>/dev/null`
    if test $? = 0; then
      echo $INC
    else
      $COLOR_WARNING
      echo "fontconfig.h not found"
      $COLOR_NORMAL
      FONTCONFIG=0
    fi
  fi
  if test $FONTCONFIG = 0; then
    echo
    $COLOR_ERROR
    echo "  ERROR: you don't have the fontconfig headers!"
    echo "  -> antialiased display on X11 screen will not be available,"
    echo "     therefore, please consider installing 'fontconfig'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
    abort
  fi
fi

# == Check for Xrender =========================================================

XRENDER=0
echo $ECHO_OPT "\n  Checking for XRENDER:"
echo -n $ECHO_OPT "\t\tlibrary... \t\t "
if test $HW = "x86_64"; then
  # Not all 64-bit OS has a /usr/lib64 directory... we must check
  if test -d $SYS_PATH/lib64; then
    xr_path="$SYS_PATH/lib64"
  else
    xr_path="$SYS_PATH/lib"
  fi
else
  r_path="$SYS_PATH/lib"
fi

LIB=`ls $xr_path/libXrender.* 2>/dev/null`
if test $? = 0; then
  LIB=`ls $xr_path/libXrender.* 2>/dev/null | head -n 1`
  echo $LIB
  XRENDER=1
else
  if test ! -z $ADD_LIB_PATH; then
    LIB=`ls $ADD_LIB_PATH/libXrender.* 2>/dev/null`
    if test $? = 0; then
      LIB=`ls $ADD_LIB_PATH/libXrender.* 2>/dev/null | head -n 1`
      echo $LIB
      XRENDER=1
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  else
    $COLOR_WARNING
    echo "not found"
    $COLOR_NORMAL
  fi
fi

if test $XRENDER = 0; then
  echo
  $COLOR_ERROR
  echo "  ERROR: you don't have the Xrender library!"
  echo "  -> antialiased display on X11 screen will not be available,"
  echo "     therefore, please consider installing 'libXrender'."
  echo "     (see the 'MUESLI Installation Guide')"
  $COLOR_NORMAL
  abort
fi

if test $XRENDER = 1; then
  echo -n $ECHO_OPT "\t\tinclude files... \t "
  INC=`ls $SYS_PATH/include/X11/extensions/Xrender.h 2>/dev/null`
  if test $? = 0; then
    echo $INC
  else
    INC=`ls $ADD_LIB_PATH/../include/X11/extensions/Xrender.h 2>/dev/null`
    if test $? = 0; then
      echo $INC
    else
      $COLOR_WARNING
      echo "Xrender.h not found"
      $COLOR_NORMAL
      XRENDER=0
    fi
  fi
  if test $XRENDER = 0; then
    echo
    $COLOR_ERROR
    echo "  ERROR: you don't have the Xrender headers!"
    echo "  -> antialiased display on X11 screen will not be available,"
    echo "     therefore, please consider installing 'libXrender'."
    echo "     (see the 'MUESLI Installation Guide')"
    $COLOR_NORMAL
    abort
  fi
fi

# == Check for Qt 4 ============================================================

QT4DIR_VAR=0
QT4_LIB=0
QT4_QMAKE='"no"'

# Only if X11 has been detected
if test "$X11_DIR" != ""; then

  echo -n $ECHO_OPT "\n  Checking for QT4DIR variable... \t "
  if test "$QT4DIR" = ""; then
    $COLOR_WARNING
    echo "not found"
    echo
    echo "  Warning: QT4DIR not set -- graphic matrix editor will be not built!"
    echo "    -> if you have a usable Qt4 library, please set QT4DIR variable"
    echo "       to the appropriate directory (without 'lib')."
    $COLOR_NORMAL
  else
    QT4DIR_VAR=1
    echo $QT4DIR
    echo -n $ECHO_OPT "  Checking for Qt4 library... \t\t "
    LIB=`ls /usr/lib/x86_64-linux-gnu/libQtCore.$SO_EXT.4 2>/dev/null`
    if test $? = 0; then
      echo $LIB
      echo -n $ECHO_OPT "  Checking for 'qmake' utility... \t "
      PROG=`ls $QT4DIR/bin/qmake 2>/dev/null`
      if test $? = 0; then
        echo $PROG
        QT4_LIB=1
        QT4_QMAKE='"yes"'
      else
        $COLOR_WARNING
        echo "not found"
        $COLOR_NORMAL
      fi
    else
      $COLOR_WARNING
      echo "not found"
      $COLOR_NORMAL
    fi
  fi

  if test $QT4DIR_VAR = 1; then
    if test $QT4_LIB = 0; then
      echo
      $COLOR_WARNING
      echo "  Warning: you don't have libqt release 4!"
      echo "  -> graphic matrix editor will not be built!"
      $COLOR_NORMAL
    else

      echo -n $ECHO_OPT "  Checking for Qt4 include... \t\t "
      # The following path depends of the linux distribution...
      INC=`ls /usr/include/qt4/Qt3Support/q3table.h 2>/dev/null`
      if test $? = 0; then
        echo "yes"
      else
        # ...therefore, we must add another check
        INC=`ls $QT4DIR/include/qtable.h 2>/dev/null`
        if test $? = 0; then
          echo "yes"
        else
          $COLOR_WARNING
          echo "not found"
          echo
          echo "  Warning: you don't have the Qt4 headers!"
          echo "  -> graphic matrix editor will not be built!"
          $COLOR_NORMAL
          QT4_QMAKE='"no"'
        fi
      fi

    fi
  fi

fi

#===============================================================================
fi

# Running local check
export KDUGPSJRVZ=1
if test -x ./local_check; then
  $COLOR_INFO
  echo
  echo "  ────────────────────── local checking for $CURRENT_DIR ──────────────────────"
  $COLOR_NORMAL
  ./local_check
  if test $? != 0; then
    exit 1
  fi
else
  $COLOR_ERROR
  echo
  echo "  ERROR: no 'local_check' script, or not executable."
  $COLOR_NORMAL
  abort
fi

if test -n "$blas_lapack_flag"; then
  blas_lapack_vendor=\"yes\"
else
  blas_lapack_vendor=\"no\"
fi

#-- Generating 'Makefile.config' ===============================================

# == Making header and copy of all configure arguments
echo "# Makefile (subpart) generated by configure --" \
     > Makefile.config
echo "#" >> Makefile.config
echo "# This file has been generated by the command:" >> Makefile.config

echo -n "# $0" >> Makefile.config
if test -n "$f90"; then
  echo -n " --f90=\"$f90\"" >> Makefile.config
fi
if test -n "$cc"; then
  echo -n " --cc=\"$cc\"" >> Makefile.config
fi
if test -n "$cxx"; then
  echo -n " --c++=\"$cxx\"" >> Makefile.config
fi
if test -n "$libstdcxx"; then
  echo -n " --libstdc++=\"$libstdcxx\"" >> Makefile.config
fi
if test -n "$ldopt"; then
  echo -n " --ldopt=\"$ldopt\"" >> Makefile.config
fi
if test -n "$blas"; then
  echo -n " --blas=\"$blas\"" >> Makefile.config
fi
if test -n "$lapack"; then
  echo -n " --lapack=\"$lapack\"" >> Makefile.config
fi
if test -n "$addlib"; then
  echo -n " --addlib=\"$addlib\"" >> Makefile.config
fi
if test -n "$blas_lapack_flag"; then
  echo -n " --blas_lapack_flag=\"$blas_lapack_flag\"" >> Makefile.config
fi
if test -n "$no_X11"; then
  echo -n " --no-X11" >> Makefile.config
fi
if test -n "$prefix"; then
  echo -n " --prefix=\"$prefix\"" >> Makefile.config
fi
echo >> Makefile.config
echo "#" >> Makefile.config

# == Substitution in Makefile.config.tmp =======================================
cat Makefile.config.tmp \
    | sed -e "s+# Makefile.config.in+# Makefile.config+" \
    | sed -e "s+@NO_X11@+$NO_X11+" \
    | sed -e "s+@ECHO_OPT@+$ECHO_OPT+" \
    | sed -e "s+@DARWIN@+$DARWIN+" \
    | sed -e "s+@X86_64@+$X86_64+" \
    | sed -e "s+@ZLIB_DIR@+$ZLIB_DIR+" \
    | sed -e "s+@ZLIB_INCL@+$ZLIB_INCL+" \
    | sed -e "s+@FREETYPE2@+$LIBFT+" \
    | sed -e "s+@FREETYPE2_DIR@+$FT_DIR+" \
    | sed -e "s+@FREETYPE2_INCL@+$FT_INCL+" \
    | sed -e "s+@IM_CONVERT@+$IM_CONVERT+" \
    | sed -e "s+@READLINE@+$READLINE+" \
    | sed -e "s+@X11_DIR@+$X11_DIR+" \
    | sed -e "s+@X11_INCL@+$X11_INCL+" \
    | sed -e "s+@QT4_QMAKE@+$QT4_QMAKE+" \
    | sed -e "s+@INSTALL_DIR@+$prefix/lib+" \
    | sed -e "s+@DOCS_INSTALL_DIR@+$prefix/share/doc+" \
    | sed -e "s+@MAN_INSTALL_DIR@+$prefix/share/man+" \
    | sed -e "s+@MUESLI_CONFIG_DIR@+$prefix/bin+" \
    | sed -e "s+@BLAS_DIR@+$blas_path+" \
    | sed -e "s+@LAPACK_DIR@+$lapack_path+" \
    | sed -e "s+@ADD_LIB@+$add_lib+" \
    | sed -e "s+@GCC_LIB@+$gcc_lib+" \
    | sed -e "s+@LD_OPT@+$ld_opt+" \
    | sed -e "s+@BLAS_LAPACK_BOTH_ARCHIVED@+$blas_lapack_both_archived+" \
    | sed -e "s+@BLAS_LAPACK_VENDOR@+$blas_lapack_vendor+" \
    >> Makefile.config

rm -f Makefile.config.tmp

echo
exit 0
