#!/bin/sh

# (C) 2007, Robert Bradshaw, William Hart, William Stein, Michael Abshoff
# (C) 2011, William Hart
# (C) 2012, William Hart, Jean-Pierre Flori, Thomas DuBuisson
# (C) 2012, Jan Engelhardt

# ./configure --disable-static --with-flint=/home/fredrik/src/flint2 --with-arb=/home/fredrik/src/arb --with-antic=/home/fredrik/src/antic

# soname version
#
# calcium     => soname
# 0.0.1   => 0.0.1
CALCIUM_MAJOR=0
CALCIUM_MINOR=4
CALCIUM_PATCH=1

PREFIX="/usr/local"
GMP_DIR="/usr/local"
MPFR_DIR="/usr/local"
FLINT_DIR="/usr/local"
ARB_DIR="/usr/local"
ANTIC_DIR="/usr/local"
NTL_DIR="/usr/local"
GC_DIR="/usr/local"
BLAS_DIR="/usr/local"
SHARED=1
STATIC=1
TLS=1
PTHREAD=1
WANT_TLS=0
ASSERT=0
BUILD=
EXTENSIONS=
EXT_MODS=
EXTRA_BUILD=

usage()
{
   echo "Usage: ./configure <options> <args>"
   echo "   where <options> may be"
   echo "     -h display usage information"
   echo "   where <args> may be:"
   echo "     --prefix=<path>      Specify path to installation location (default: /usr/local)"
   echo "     --with-mpir=<path>   Specify location of MPIR (default: /usr/local)"
   echo "     --with-gmp=<path>    Specify location of GMP (default: /usr/local)"
   echo "     --with-mpfr=<path>   Specify location of MPFR (default: /usr/local)"
   echo "     --with-flint=<path>  Specify location of FLINT (default: /usr/local)"
   echo "     --with-arb=<path>    Specify location of ARB (default: /usr/local)"
   echo "     --with-antic=<path>  Specify location of ANTIC (default: /usr/local)"
   echo "     --build=arch-os      Specify architecture/OS combination rather than use values from uname -m/-s"
   echo "     --enable-shared      Build a shared library (default)"
   echo "     --disable-shared     Do not build a shared library"
   echo "     --enable-static      Build a static library (default)"
   echo "     --disable-static     Do not build a static library"
   echo "     --single             Faster [non-reentrant if tls or pthread not used] version of library (default)"
   echo "     --reentrant          Build fully reentrant [with or without tls, with pthread] version of library"
   echo "     --with-gc=<path>     GC safe build with path to gc"
   echo "     --enable-pthread     Use pthread (default)"
   echo "     --disable-pthread    Do not use pthread"
   echo "     --enable-tls         Use thread-local storage (default)"
   echo "     --disable-tls        Do not use thread-local storage"
   echo "     --enable-assert      Enable use of asserts (use for debug builds only)"
   echo "     --disable-assert     Disable use of asserts (default)"
   echo "     --enable-cxx         Enable C++ wrapper tests"
   echo "     --disable-cxx        Disable C++ wrapper tests (default)"
   echo "     CC=<name>            Use the C compiler with the given name (default: gcc)"
   echo "     CXX=<name>           Use the C++ compiler with the given name (default: g++)"
   echo "     AR=<name>            Use the AR library builder with the given name (default: ar)"
   echo "     CFLAGS=<flags>       Pass the given flags to the compiler"
   echo "     ABI=[32|64]          Tell the compiler to use given ABI (default: empty)"
}


absolute_path(){
   dirlist="$1"
   retval=""
   for dir in $dirlist; do
      case $dir in
        /*) dir=$dir;;
        *) dir=$PWD/$dir;;
      esac
      retval=$retval" "$dir
   done
   echo $retval
}

while [ "$1" != "" ]; do
   PARAM=`echo $1 | sed 's/=.*//'`
   VALUE=`echo $1 | sed 's/[^=]*//; s/=//'`
   case "$PARAM" in
      -h|--help)
         usage
         exit 0
         ;;
      --with-mpir|--with-gmp)
         GMP_DIR=$(absolute_path "$VALUE")
         ;;
      --with-mpfr)
         MPFR_DIR=$(absolute_path "$VALUE")
         ;;
      --with-flint)
         FLINT_DIR=$(absolute_path "$VALUE")
         ;;
      --with-arb)
         ARB_DIR=$(absolute_path "$VALUE")
         ;;
      --with-antic)
         ANTIC_DIR=$(absolute_path "$VALUE")
         ;;
      --build)
         BUILD="$VALUE"
         ;;
      --prefix)
         PREFIX=$VALUE
         ;;
      --enable-shared)
         SHARED=1
         ;;
      --disable-shared)
         SHARED=0
         ;;
      --enable-static)
         STATIC=1
         ;;
      --disable-static)
         STATIC=0
         ;;
      --single)
         REENTRANT=0
         ;;
      --reentrant)
         REENTRANT=1
         ;;
      --enable-pthread)
         PTHREAD=1
         ;;
      --disable-pthread)
         PTHREAD=0
         ;;
      --enable-tls)
         TLS=1
         WANT_TLS=1;;
      --disable-tls)
         TLS=0
         ;;
      --enable-assert)
         ASSERT=1
         ;;
      --disable-assert)
         ASSERT=0
         ;;
      AR)
         AR="$VALUE"
         ;;
      CC)
         CC="$VALUE"
         ;;
      CXX)
         CXX="$VALUE"
         ;;
      CFLAGS)
         CFLAGS="$VALUE"
         ;;
      ABI)
         ABI="$VALUE"
         ;;
      *)
         usage
         exit 1
         ;;
   esac
   shift
done

#find dependencies

LIBS="m"

if [ -d "${GMP_DIR}/lib" ]; then
   GMP_LIB_DIR="${GMP_DIR}/lib"
   GMP_INC_DIR="${GMP_DIR}/include"
elif [ -d "${GMP_DIR}/lib64" ]; then
   GMP_LIB_DIR="${GMP_DIR}/lib64"
   GMP_INC_DIR="${GMP_DIR}/include"
elif [ -d "${GMP_DIR}/.libs" ]; then
   GMP_LIB_DIR="${GMP_DIR}/.libs"
   GMP_INC_DIR="${GMP_DIR}"
else
   echo "Invalid GMP directory"
   exit 1
fi
LIB_DIRS="${LIB_DIRS} ${GMP_LIB_DIR}"
INC_DIRS="${INC_DIRS} ${GMP_INC_DIR}"
LIBS="${LIBS} gmp"

if [ -d "${MPFR_DIR}/lib" ]; then
   MPFR_LIB_DIR="${MPFR_DIR}/lib"
   MPFR_INC_DIR="${MPFR_DIR}/include"
elif [ -d "${MPFR_DIR}/lib64" ]; then
   MPFR_LIB_DIR="${MPFR_DIR}/lib64"
   MPFR_INC_DIR="${MPFR_DIR}/include"
elif [ -d "${MPFR_DIR}/.libs" ]; then
   MPFR_LIB_DIR="${MPFR_DIR}/.libs"
   MPFR_INC_DIR="${MPFR_DIR}"
elif [ -d "${MPFR_DIR}/src/.libs" ]; then
   MPFR_LIB_DIR="${MPFR_DIR}/src/.libs"
   MPFR_INC_DIR="${MPFR_DIR}/src"
else
   echo "Invalid MPFR directory"
   exit 1
fi
LIB_DIRS="${LIB_DIRS} ${MPFR_LIB_DIR}"
INC_DIRS="${INC_DIRS} ${MPFR_INC_DIR}"
LIBS="${LIBS} mpfr"

if [ -d "${FLINT_DIR}/lib" ]; then
   FLINT_LIB_DIR="${FLINT_DIR}/lib"
   FLINT_INC_DIR="${FLINT_DIR}/include"
elif [ -d "${FLINT_DIR}/lib64" ]; then
   FLINT_LIB_DIR="${FLINT_DIR}/lib64"
   FLINT_INC_DIR="${FLINT_DIR}/include"
elif [ -d "${FLINT_DIR}/.libs" ]; then
   FLINT_LIB_DIR="${FLINT_DIR}/.libs"
   FLINT_INC_DIR="${FLINT_DIR}"
elif [ -d "${FLINT_DIR}" ]; then
   FLINT_LIB_DIR="${FLINT_DIR}"
   FLINT_INC_DIR="${FLINT_DIR}"
else
   echo "Invalid FLINT directory"
   exit 1
fi

if [ -d "${FLINT_INC_DIR}/flint" ]; then
   FLINT_INC_DIR="${FLINT_INC_DIR}"
elif [ -f "${FLINT_INC_DIR}/flint.h" ]; then
   mkdir -p build/include
   ln -sf ${FLINT_INC_DIR} build/include/flint
   FLINT_INC_DIR="${PWD}/build/include"
fi

echo "FLINT_LIB_DIR set to ${FLINT_LIB_DIR}"
echo "FLINT_INC_DIR set to ${FLINT_INC_DIR}"

LIB_DIRS="${LIB_DIRS} ${FLINT_LIB_DIR}"
INC_DIRS="${INC_DIRS} ${FLINT_INC_DIR}"
LIBS="${LIBS} flint"



if [ -d "${ARB_DIR}/lib" ]; then
   ARB_LIB_DIR="${ARB_DIR}/lib"
   ARB_INC_DIR="${ARB_DIR}/include"
elif [ -d "${ARB_DIR}/lib64" ]; then
   ARB_LIB_DIR="${ARB_DIR}/lib64"
   ARB_INC_DIR="${ARB_DIR}/include"
elif [ -d "${ARB_DIR}/.libs" ]; then
   ARB_LIB_DIR="${ARB_DIR}/.libs"
   ARB_INC_DIR="${ARB_DIR}"
elif [ -d "${ARB_DIR}" ]; then
   ARB_LIB_DIR="${ARB_DIR}"
   ARB_INC_DIR="${ARB_DIR}"
else
   echo "Invalid ARB directory"
   exit 1
fi

echo "ARB_LIB_DIR set to ${ARB_LIB_DIR}"
echo "ARB_INC_DIR set to ${ARB_INC_DIR}"

LIB_DIRS="${LIB_DIRS} ${ARB_LIB_DIR}"
INC_DIRS="${INC_DIRS} ${ARB_INC_DIR}"
LIBS="${LIBS} arb"


if [ -d "${ANTIC_DIR}/lib" ]; then
   ANTIC_LIB_DIR="${ANTIC_DIR}/lib"
   ANTIC_INC_DIR="${ANTIC_DIR}/include"
elif [ -d "${ANTIC_DIR}/lib64" ]; then
   ANTIC_LIB_DIR="${ANTIC_DIR}/lib64"
   ANTIC_INC_DIR="${ANTIC_DIR}/include"
elif [ -d "${ANTIC_DIR}/.libs" ]; then
   ANTIC_LIB_DIR="${ANTIC_DIR}/.libs"
   ANTIC_INC_DIR="${ANTIC_DIR}"
elif [ -d "${ANTIC_DIR}" ]; then
   ANTIC_LIB_DIR="${ANTIC_DIR}"
   ANTIC_INC_DIR="${ANTIC_DIR}"
else
   echo "Invalid ANTIC directory"
   exit 1
fi

if [ -d "${ANTIC_INC_DIR}/antic" ]; then
   ANTIC_INC_DIR="${ANTIC_INC_DIR}"
elif [ -f "${ANTIC_INC_DIR}/nf.h" ]; then
   mkdir -p build/include
   ln -sf ${ANTIC_INC_DIR} build/include/antic
   ANTIC_INC_DIR="${PWD}/build/include"
fi

echo "ANTIC_LIB_DIR set to ${ANTIC_LIB_DIR}"
echo "ANTIC_INC_DIR set to ${ANTIC_INC_DIR}"

LIB_DIRS="${LIB_DIRS} ${ANTIC_LIB_DIR}"
INC_DIRS="${INC_DIRS} ${ANTIC_INC_DIR}"
LIBS="${LIBS} antic"


# defaults for CC, CXX and AR

if [ -z "$CC" ]; then
   CC=gcc
fi

if [ -z "$CXX" ]; then
   CXX=g++
fi

if [ -z "$AR" ]; then
   AR=ar
fi

# Architecture handler

KERNEL=`uname`

if [ -z "$BUILD" ]; then
   ARCH=`uname -m`

   if [ "$(uname | cut -d_ -f1)" = "MINGW32" ]; then
      if [ "$ABI" = "64" ]; then
         OS="MINGW64"
      else
         OS="MINGW32"
      fi
   elif [ "$(uname | cut -d_ -f1)" = "CYGWIN" ]; then
      if [ "$ARCH" = "x86_64" ]; then
         if [ "$ABI" = "32" ]; then
            OS="CYGWIN32"
         else
            OS="CYGWIN64"
            ABI="64"
         fi
      else
         OS="CYGWIN32"
      fi
   else
      OS=`uname -s`
   fi
else
   ARCH=`echo "$BUILD" | cut -d- -f1`
   OS=`echo "$BUILD" | cut -d- -f2`
fi

case "$ARCH" in
   x86_64 | amd64)
      MACHINE="x86_64";;
   x86 | i*86 | pc)
      MACHINE="x86";;
   ia64)
      MACHINE="ia64";;
   sparc | sun4*)
      MACHINE="sparc";;
   sparc64)
      MACHINE="sparc64";;
   ppc64 | powerpc64)
      MACHINE="ppc64";;
   ppc | powerpc | [P|p]ower*)
      MACHINE="ppc";;
   *)
      MACHINE="unknown";;
esac

#ABI flag
if [ "$ABI" = "32" ]; then
   ABI_FLAG="-m32"
   case "$MACHINE" in
      x86_64)
         MACHINE="x86";;
      sparc64)
         MACHINE="sparc";;
      ppc64)
         MACHINE="ppc";;
      *)
         ;;
   esac
elif [ "$ABI" = "64" ]; then
   ABI_FLAG="-m64"
   if [ "$MACHINE" = "sparc" ]; then
      MACHINE="sparc64"
   fi
   if [ "$MACHINE" = "x86" ]; then
      MACHINE="x86_64"
   fi
fi

if [ "$MACHINE" = "sparc" ] || [ "$MACHINE" = "sparc64" ]; then
   if [ "$CC" = "gcc" ]; then
      CC="gcc -mno-relax"
   fi
fi

echo "Configuring...${MACHINE}-${OS}"

#name for CALCIUM shared library

CALCIUM_SOLIB=0
if [ -z "$CALCIUM_LIB" ]; then
   case "$OS" in
      Darwin)
         CALCIUM_LIBNAME="libcalcium.dylib"
	 CALCIUM_LIB="libcalcium-$CALCIUM_MAJOR.$CALCIUM_MINOR.$CALCIUM_PATCH.dylib"
         EXTRA_SHARED_FLAGS="-install_name $PREFIX/lib/$CALCIUM_LIB -compatibility_version $CALCIUM_MAJOR.$CALCIUM_MINOR -current_version $CALCIUM_MAJOR.$CALCIUM_MINOR.$CALCIUM_PATCH";;
      CYGWIN* | MINGW*)
         CALCIUM_LIBNAME="libcalcium.dll"
	 CALCIUM_LIB="libcalcium-$CALCIUM_MAJOR.dll"
	 EXTRA_SHARED_FLAGS="-static-libgcc -shared -Wl,--export-all-symbols -Wl,-soname,libcalcium-$CALCIUM_MAJOR.dll.$CALCIUM_MINOR.$CALCIUM_PATCH";;
      *)
         CALCIUM_LIBNAME="libcalcium.so"
	 CALCIUM_LIB="libcalcium.so.$CALCIUM_MAJOR.$CALCIUM_MINOR.$CALCIUM_PATCH"
	 EXTRA_SHARED_FLAGS="-Wl,-soname,libcalcium.so.$CALCIUM_MAJOR"
	 CALCIUM_SOLIB=1;;
   esac
fi

# sometimes LDCONFIG is not to be found in the path. Look at some common places.
case "$OS" in
    MINGW*|CYGWIN*|Darwin)
	LDCONFIG="true";;
    *)
	if [ -z "$LDCONFIG" ]; then
	    LDCONFIG="true"
	    if [ "$CALCIUM_SOLIB" = "1" ]; then
		if command -v ldconfig > /dev/null; then
		    LDCONFIG="ldconfig"
		elif [ -x /sbin/ldconfig ]; then
		    LDCONFIG="/sbin/ldconfig"
		fi
	    fi
	fi;;
esac

#extension for executables

if [ -z "$EXEEXT" ]; then
   case "$OS" in
      CYGWIN* | MINGW*)
         EXEEXT=".exe";;
      *)
         EXEEXT="";;
   esac
fi

#don't build both shared and static lib on MinGW and Cygwin

case "$OS" in
   CYGWIN* | MINGW*)
      if [ "$STATIC" = "1" ] && [ "$SHARED" = "1" ]; then
         echo "Building both static and shared versions of MPIR/GMP on $OS is currently"
         echo "unsupported, and so is it for MPFR, FLINT and ARB."
         echo "You should pass --disable-shared or --disable-static to configure"
         echo "depending on the versions of MPIR/GMP, MPFR and FLINT you built."
         exit 1
      fi
      ;;
   *)
      ;;
esac 

#test for popcnt flag and set needed CFLAGS

#defaults for CFLAGS

if [ -z "$CFLAGS" ]; then
   if [ "$OS" = "MINGW64" ]; then
      CFLAGS="-O2 -funroll-loops -g $POPCNT_FLAG $ABI_FLAG"
   elif [ "$OS" = "CYGWIN64" ]; then
      CFLAGS="-O2 -funroll-loops -g -D _WIN64 $POPCNT_FLAG $ABI_FLAG"
   else
      CFLAGS="-ansi -pedantic -Wall -O2 -funroll-loops -g $POPCNT_FLAG $ABI_FLAG"
   fi
fi

#this is needed on PPC G5 and does not hurt on other OS Xes

if [ "$KERNEL" = Darwin ]; then
   CFLAGS="-fno-common $CFLAGS"
fi

#PIC flag

if [ -z "$PIC_FLAG" ]; then
   case "$OS" in
      CYGWIN* | MINGW*)
         ;;
      *)
         PIC_FLAG="-fPIC";;
   esac
fi

#test support for thread-local storage

CONFIG_TLS="#define HAVE_TLS 0"

if [ "$TLS" = "1" ]; then
   mkdir -p build
   rm -f build/test-tls > /dev/null 2>&1
   MSG="Testing __thread..."
   printf "%s" "$MSG"
   echo "__thread int x = 42; int main(int argc, char ** argv) { return x != 42; }" > build/test-tls.c
   $CC build/test-tls.c -o ./build/test-tls > /dev/null 2>&1
   if [ $? -eq 0 ]; then
      build/test-tls > /dev/null 2>&1
      if [ $? -eq 0 ]; then
         printf "%s\n" "yes"
         CONFIG_TLS="#define HAVE_TLS 1"
      else
         printf "%s\n" "no"
      fi
      rm -f build/test-tls{,.c}
   else
      rm -f build/test-tls.c
      printf "%s\n" "no"
   #build-tls can segfault on systems where tls is not available
   fi 2> /dev/null
fi

#pthread configuration

CONFIG_PTHREAD="#define HAVE_PTHREAD ${PTHREAD}"


#pocess external modules

EXTRA_INC_DIRS="${EXTRA_INC_DIRS} ${EXTENSIONS}"


if [ -d "${EXTENSIONS}/examples" ]; then
   cp ${EXTENSIONS}/examples/*.c ./examples/
fi

#include paths

INCS="-I\$(CURDIR)"
for INC_DIR in ${INC_DIRS} ${EXTRA_INC_DIRS}; do
   INCS="${INCS} -I${INC_DIR}"
done

#library paths

LLIBS="-L\$(CURDIR)"
for LIB_DIR in ${LIB_DIRS} ${EXTRA_LIB_DIRS}; do
   LLIBS="${LLIBS} -L${LIB_DIR}"
done

#linker params

if [ "$PTHREAD" = "1" ]; then
   lLIBS2="-lpthread ${lLIBS2}"
fi


for LIB in ${EXTRA_LIBS} ${LIBS}; do
   lLIBS2="-l${LIB} ${lLIBS2}"
done
lLIBS="-lcalcium $lLIBS2"
LIBS2="$LLIBS $lLIBS2"
LIBS="$LLIBS $lLIBS"

#paths for dynamic linker

case "$OS" in
   CYGWIN* | MINGW*)
      DLPATH="PATH";;
   Darwin)
      DLPATH="DYLD_LIBRARY_PATH";;
   sparc)
      DLPATH="LD_LIBRARY_PATH32";;
   sparc64)
      DLPATH="LD_LIBRARY_PATH64";;
   *)
      DLPATH="LD_LIBRARY_PATH";;
esac

DLPATH_ADD="\$(CURDIR)"
for LIB_DIR in ${LIB_DIRS} ${EXTRA_LIB_DIRS}; do
   DLPATH_ADD="${DLPATH_ADD}:${LIB_DIR}"
done

#write out Makefile

echo "# This file is autogenerated by ./configure -- do not edit!" > Makefile
echo "" >> Makefile
echo "SHELL=/bin/sh" >> Makefile
echo "" >> Makefile
echo "CALCIUM_STATIC=$STATIC" >> Makefile
echo "CALCIUM_SHARED=$SHARED" >> Makefile
echo "CALCIUM_LIB=$CALCIUM_LIB" >> Makefile
echo "CALCIUM_LIBNAME=$CALCIUM_LIBNAME" >> Makefile
echo "CALCIUM_MAJOR=$CALCIUM_MAJOR" >> Makefile
echo "CALCIUM_SOLIB=$CALCIUM_SOLIB" >> Makefile
echo "EXEEXT=$EXEEXT" >> Makefile
echo "PREFIX=$PREFIX" >> Makefile
echo "" >> Makefile
echo "INCS=$INCS" >> Makefile
echo "LIBS=$LIBS" >> Makefile
echo "LIBS2=$LIBS2" >> Makefile
echo "" >> Makefile
echo "CC=$CC" >> Makefile
echo "CXX=$CXX" >> Makefile
echo "AR=$AR" >> Makefile
echo "LDCONFIG=$LDCONFIG" >> Makefile
echo "" >> Makefile
echo "CFLAGS=$CFLAGS" >> Makefile
echo "ABI_FLAG=$ABI_FLAG" >> Makefile
echo "PIC_FLAG=$PIC_FLAG" >> Makefile
echo "EXTRA_SHARED_FLAGS=$EXTRA_SHARED_FLAGS" >> Makefile
echo "" >> Makefile
echo "DLPATH=$DLPATH" >> Makefile
echo "DLPATH_ADD=$DLPATH_ADD" >> Makefile
echo "EXTENSIONS=$EXTENSIONS" >> Makefile
echo "EXTRA_BUILD_DIRS=$EXTRA_BUILD" >> Makefile
echo "" >> Makefile

cat Makefile.in >> Makefile

echo "Calcium was successfully configured."
