## Process this file with autoconf to produce a configure script.
##
## Configure.in for RcppOctave
##
## Borrowed and adapted from the file configure.in for RcppGSL
## Copyright (C) 2010 Romain Francois and Dirk Eddelbuettel
## Licensed under GNU GPL 2 or later
##
## Copyright (C) 2011 Renaud Gaujoux

# You will very likely need to ensure that the same C compiler and compiler 
# flags are used in the configure tests as when compiling R or your package. 
# Under a Unix-alike, you can achieve this by including the following fragment 
# early in configure.ac
: ${R_HOME=`R RHOME`}
if test -z "${R_HOME}"; then
  echo "RcppOctave - configure: could not determine R_HOME"
  exit 1
fi

# load m4 Windows macros
. exec/m4.win
AC_INIT RcppOctave 0.18.1

AC_MSG_CHECKING "R architecture"
AC_MSG_RESULT ${R_ARCH}
if test "${R_ARCH}" = "/x64"; then
echo "configure: Octave does not provide 64bit libraries [RcppOctave library will be a ????]"
fi


## Path to R binaries
AC_PATH_PROG R_BIN R "" "${R_HOME}/bin${R_ARCH_BIN}"
AC_PATH_PROG RSCRIPT Rscript "" "${R_HOME}/bin${R_ARCH_BIN}"
#

RSCRIPT_ARG=''
if test -n "${RSCRIPT_ARG}"; then
	RSCRIPT="${RSCRIPT} ${RSCRIPT_ARG}"
	RSCRIPT_ARG=" ${RSCRIPT_ARG}"
	AC_MSG_NOTICE "Using Rscript command: ${RSCRIPT}"
fi
AC_SUBST RSCRIPT_ARG 
#

AC_MSG_CHECKING "R version"
echo `${RSCRIPT} -e "cat(R.version[['version.string']])"`
AC_MSG_CHECKING "R platform"
R_OS=`${RSCRIPT} -e "cat(R.version[['platform']])"`
AC_MSG_RESULT ${R_OS}

# get compiler and compilation flags from R
if test -n "${R_HOME}"; then
  AC_MSG_CHECKING "R CC"
  CC=`"${R_BIN}" CMD config CC`
  AC_MSG_RESULT "${CC}"
  AC_SUBST CC

  CC_NAME=${CC% *}
  AC_PATH_PROG R_CC_COMPILER "${CC}" "" "" "${CC_NAME}"
  AC_MSG_CHECKING "${CC_NAME} version"
  CC_VERSION=`${CC} -dumpversion`
  CC_MACHINE=`${CC} -dumpmachine`
  CC_SPECS="${CC_MACHINE}-${CC_VERSION}"
  AC_MSG_RESULT "${CC_SPECS}"

  AC_MSG_CHECKING "R CFLAGS"
  CFLAGS=`"${R_BIN}" CMD config CFLAGS`
  AC_MSG_RESULT ${CFLAGS}

  AC_MSG_CHECKING "R CPPFLAGS"
  CPPFLAGS=`"${R_BIN}" CMD config CPPFLAGS`
  AC_MSG_RESULT ${CPPLAGS}

  AC_MSG_CHECKING "R CXXLAGS"
  CXXFLAGS=`"${R_BIN}" CMD config CXXFLAGS`
  AC_MSG_RESULT ${CXXLAGS}
fi


echo "Checking system PATH..."
AC_SYS_PATH

# check R is a shared library
R_LDFLAGS="\`\${R_BIN} CMD config --ldflags 2>/dev/null\`"
R_CPPFLAGS="\`\${R_BIN} CMD config --cppflags 2>/dev/null\`"

# pass R flags down to makefile in modules/
AC_SUBST R_CPPFLAGS
AC_SUBST R_LDFLAGS

## Use octave-config to find arguments for compiler and linker flags
##
## Check for non-standard programs: 
## octave-config

OCTAVE_CUSTOMED=FALSE
# pass custom flag to scripts
AC_SUBST OCTAVE_CUSTOMED

# looking up octave-config and mkoctfile
AC_PATH_PROG OCTAVE_CONFIG octave-config
## If octave-config was found, let's use it
if test "${OCTAVE_CONFIG}" != ""; then
	
	AC_PROG_VARPATH OCTAVE_BINDIR "${OCTAVE_CONFIG}" BINDIR "Octave binary directory"
	## mkoctfile
	AC_PATH_PROG OCTAVE_MKOCTFILE mkoctfile "" "${OCTAVE_BINDIR}"

	# define AC functions to load Octave config variables
	. exec/octave.m4
	##

	## octave
	AC_PATH_PROG OCTAVE_BIN octave "" "${OCTAVE_BINDIR}"
	
	## select the right R compiler
	AC_CC_COMPATIBLE_OCTAVE CC_BIN_BASE "${OCTAVE_BINDIR}" "${CC}" "${RTOOLS_COMPILER_NO_LOOKUP}"
	if test -z "${CC_BIN_BASE}"; then
		echo "RcppOctave configuration error:
  Rtools gcc version is not compatible with Octave's version 
  -> If using Octave >= 4.0.0, please check that Rtools Windows native toolchain gcc-4.9.2 
  is located in a sub-directory of Rtools.
  See https://rawgit.com/kevinushey/RToolsToolchainUpdate/master/mingwnotes.html"
		exit 1
	fi
	
	AC_MSG_CHECKING "RcppOctave system-specific cpp flags"
	PKG_CPPFLAGS_XTRA=-fmax-errors=10
	if test "${PKG_CPPFLAGS_XTRA}" = "" ; then AC_MSG_RESULT "none"; else AC_MSG_RESULT "${PKG_CPPFLAGS_XTRA}"; fi
	SYSTEM_SPEC="${SYSTEM_SPEC}\nPKG_CPPFLAGS_XTRA=${PKG_CPPFLAGS_XTRA}"
	
	# Include directory
	# AC_OCTAVE_MKCONFIG OCT_INCLUDEDIR OCTINCLUDEDIR "include directory"
	
	# Library directory
	#	AC_OCTAVE_MKCONFIG OCT_LIBDIR OCTLIBDIR "lib directory"
	
	## pass on PATH modifications
	AC_MSG_CHECKING "PATH changes"
	if test "${PATH_EXPORT}" = ""; then
		PATH_EXPORT="# none needed"
		AC_MSG_RESULT "none"
	else
		AC_MSG_RESULT "${PATH_EXPORT}"
		PATH_EXPORT="${PATH_EXPORT}\$(PATH_SEPARATOR)\$(PATH)"
		PATH_EXPORT="export PATH:=${PATH_EXPORT}"
	fi
	AC_SUBST PATH_EXPORT
	##
	
	## Octave module target
	AC_MSG_CHECKING "Octave module make target"
	OCT_TARGET="win\${R_ARCH}"
	AC_SUBST OCT_TARGET
	AC_MSG_RESULT "${OCT_TARGET}"
	##
	
	## System-specific statements (flags,target)
	if test "${SYSTEM_SPEC}" = ""; then SYSTEM_SPEC="# none needed"; fi
	AC_SUBST SYSTEM_SPEC
	##

	## mkoctfile command used to compile the modules
	AC_MSG_CHECKING "mkoctfile compile command"
	MKOCT_CMD=mkoctfile
	AC_SUBST MKOCT_CMD
	AC_MSG_RESULT "${MKOCT_CMD}"

	# Use mkoctfile for compiler and linker arguments
#	AC_OCTAVE_MKCONFIG OCT_BASELIBDIR LIBDIR "libdir"
#	AC_OCTAVE_MKCONFIG OCT_BASEINCLUDEDIR INCLUDEDIR "includedir"
	AC_OCTAVE_MKCONFIG OCT_CPPFLAGS CPPFLAGS "cpp flags"
    AC_OCTAVE_MKCONFIG OCT_INCFLAGS INCFLAGS "include flags"
    OCT_CPPFLAGS="${OCT_CPPFLAGS} ${OCT_INCFLAGS}"
    
    AC_OCTAVE_MKCONFIG OCTAVE_LFLAGS LFLAGS "-L flags"
    AC_OCTAVE_MKCONFIG OCTAVE_LIBS OCTAVE_LIBS "libraries"
    # add to LDFLAGS
    OCT_LDFLAGS=" ${OCTAVE_LFLAGS} ${OCTAVE_LIBS}"
    
    # check Octave compilers
    AC_OCTAVE_MKCONFIG_RAW OCTAVE_CC CC "C compiler"
    AC_OCTAVE_MKCONFIG_RAW OCTAVE_F77 F77 "Fortran compiler"
    
    # Octave config info
    AC_OCTAVE_CONFIG OCTAVE_PLATFORM CANONICAL_HOST_TYPE "platform"
    AC_OCTAVE_CONFIG OCTAVE_API_VERSION API_VERSION "API version"
    
    # check Octave version to know whether one should infer output names:
    # this only works for Octave >= 3.4.3
    AC_OCTAVE_CONFIG OCTAVE_VERSION VERSION "version"
    
    CMPV="3.4.3"
    AC_MSG_CHECKING "whether to infer output names for Octave function (Octave >= ${CMPV}?)"
    AS_VERSION_COMPARE OCT_POST_3_4_0 ${OCTAVE_VERSION} ${CMPV}
    if test "${OCT_POST_3_4_0}" != "-1"; then AC_MSG_RESULT yes; else AC_MSG_RESULT no; fi
	# add to CPPFLAGS
    OCT_CPPFLAGS="${OCT_CPPFLAGS} -DOCT_POST_3_4_0=${OCT_POST_3_4_0}"
    
else
    echo "octave-config not found, is Octave installed?
    
    -> Check that Octave is installed and that its bin/ sub-directory
       is in Windows PATH.
    "
    exit 2
fi

# Now substitute these variables in src/Makevars.in to create src/Makevars
AC_MSG_NOTICE "Using Octave LDFLAGS: ${OCT_LDFLAGS}"
AC_SUBST OCT_LDFLAGS
AC_MSG_NOTICE "Using Octave CPPFLAGS: ${OCT_CPPFLAGS}"
AC_SUBST OCT_CPPFLAGS

# Module path
AC_MSG_CHECKING "RcppOctave Octave module directory"
OCT_MODULES_PATH=${R_PACKAGE_DIR}/modules
AC_MSG_RESULT "${OCT_MODULES_PATH}"
AC_SUBST OCT_MODULES_PATH

# Configure libRrng
#AC_CONFIG_SUBDIRS([src/modules])

R_ARCH0=`echo "${R_ARCH}" | sed 's/\///'`
OCT_SOURCES_DIR=`${RSCRIPT} -e "cat(getwd())"`/src-${R_ARCH0}/modules
AC_SUBST OCT_SOURCES_DIR

AC_OUTPUT R/config-vars.R
AC_OUTPUT src-${R_ARCH0}/Makevars
AC_OUTPUT src-${R_ARCH0}/modules/Makefile
