AC_INIT(mod_R,1.0,jeff.horner@vanderbilt.edu)
AC_CONFIG_AUX_DIR([tools])
AC_CONFIG_MACRO_DIR([m4])
# Automake initialization
AM_INIT_AUTOMAKE( foreign no-texinfo.tex no-dependencies no-dist no-installinfo no-installman no-exeext )

# This macro turns off maintainer mode by default. use "configure --enable-mainainer-mode"
# to enable
AM_MAINTAINER_MODE

# We'll get compiler and libtool info from apxs2
#AC_PROG_CC
AC_PROG_LIBTOOL

AC_CHECK_HEADERS( sys/types.h unistd.h stdlib.h )

# --with-R
AC_DEFUN([R_PROG],[

    AC_ARG_WITH(
            R,
            [  --with-R=PATH                Path to R (usually /usr/local/bin/R) ],
            ,
            [with_R="no"]
    )


    if test "$with_R" = "no"; then
		AC_PATH_PROG([RPROGRAM],[R],[no])
		if test "$RPROGRAM" = "no"; then
			echo
			echo
			echo "Specify the R program using --with-R (like /usr/local/bin/R)"
			echo
			AC_MSG_ERROR( aborting! )
		else
			if $RPROGRAM CMD config --ldflags 2>/dev/null | grep -v 'R was not built as a shared library' >/dev/null; then
					RPROG=$RPROGRAM
					RHOME=`$RPROGRAM RHOME`
					RINCLUDES=`$RPROG CMD config --cppflags`
					RLINKLD=`$RPROG CMD config --ldflags`
			else
				echo
				echo
				echo "R was not built as a shared library"
				echo
				echo "Either build it with one, or use another install of R"
				echo
				AC_MSG_ERROR( aborting! )
			fi
		fi
    else
		AC_MSG_CHECKING(for R Program)
        # make sure that a well known include file exists
        # and the libR.so
        if $with_R CMD config --ldflags 2>/dev/null | grep -v 'R was not built as a shared library' >/dev/null; then
                RPROG=$with_R
                RHOME=`$with_R RHOME`
				RINCLUDES=`$RPROG CMD config --cppflags`
				RLINKLD=`$RPROG CMD config --ldflags`
                AC_MSG_RESULT([$RPROG])
        else
            echo
            echo
            echo "R was not built as a shared library"
            echo
            echo "Either build it with one, or use another install of R"
            echo
            AC_MSG_ERROR( aborting! )
        fi
    fi

])

# --with-apreq2-config
AC_DEFUN([APREQ_CONFIG],[

    AC_ARG_WITH(
            apreq2-config,
            [  --with-apreq2-config=PATH    Path to apreq2-config ],
            ,
            [with_apreq2_config="no"]
    )


    if test "$with_apreq2_config" = "no"; then
		AC_PATH_PROG([APREQCONFIG],[apreq2-config],[no])
			if test "$APREQCONFIG" = "no"; then
				echo
				echo
				echo "Using libapreq2 that comes bundled with mod_R"
				echo
				APREQ=`pwd`/libapreq2/apreq2-config
				APREQ_COMPILE="(cd libapreq2; make apreq2-config; cd library; make)"
				APREQ_INSTALL="(cd libapreq2/library; make install)"
				APREQ_CLEAN="(cd libapreq2; make clean)"
				(cd libapreq2; ./configure --with-apache2-apxs=$APXS)
				APREQ_LIB_DIR=`$APREQ --link-ld | sed -e 's/-L//;s/-lapreq2//;s/ //g'`
				APREQ_INCLUDES=`$APREQ --includes`
				APREQ_LINKLD=`$APREQ --link-ld`
			else
                APREQ=$APREQCONFIG
                APREQ_COMPILE=
                APREQ_INSTALL=
				APREQ_CLEAN=
				APREQ_LIB_DIR=`$APREQ --link-ld | sed -e 's/-L//;s/-lapreq2//;s/ //g'`
				APREQ_INCLUDES=`$APREQ --includes`
				APREQ_LINKLD=`$APREQ --link-ld`
			fi
    else
		AC_MSG_CHECKING(for apreq2-config)
        # make sure that a well known include file exists
        if test -f `$with_apreq2_config --includedir`/apreq.h  ; then
                APREQ=$with_apreq2_config
                APREQ_COMPILE=
                APREQ_INSTALL=
				APREQ_CLEAN=
				APREQ_LIB_DIR=`$APREQ --link-ld | sed -e 's/-L//;s/-lapreq2//;s/ //g'`
				APREQ_INCLUDES=`$APREQ --includes`
				APREQ_LINKLD=`$APREQ --link-ld`
        else
            echo
            echo
            echo "Cannot find libapreq2 header files"
            echo
            echo "Check to make sure libapreq2 is already installed in your Apache install."
            echo "If not, then don't specifiy the --with-apreq2-config option as mod_R comes bundled"
            echo "with it."
            echo
            AC_MSG_ERROR( aborting! )
        fi
    fi
])

AC_DEFUN([SET_DOCROOT],[
        DOCROOT=`pwd`/test
])

AC_PATH_PROG(UNAME,uname)

AC_DEFUN([WITH_RPATH],[
	if test "${UNAME}" != ""; then
		if test "`${UNAME}`" = "Linux"; then
			RPATH=-Wl,-rpath,${RHOME}/lib
		fi
	fi
])

AX_WITH_APXS
AC_SUBST(APXS)
HTTPD="`$APXS -q sbindir`/`$APXS -q progname`"
AC_SUBST(HTTPD)
R_PROG
AC_SUBST(RPROG)
AC_SUBST(RHOME)
AC_SUBST(RINCLUDES)
AC_SUBST(RLINKLD)
APREQ_CONFIG
AC_SUBST(APREQ)
AC_SUBST(APREQ_COMPILE)
AC_SUBST(APREQ_INSTALL)
AC_SUBST(APREQ_CLEAN)
AC_SUBST(APREQ_LIB_DIR)
AC_SUBST(APREQ_INCLUDES)
AC_SUBST(APREQ_LINKLD)
SET_DOCROOT
AC_SUBST(DOCROOT)
WITH_RPATH
AC_SUBST(RPATH)

SHLIBPATH_VAR=$shlibpath_var
AC_SUBST(SHLIBPATH_VAR)

AC_OUTPUT(Makefile mod_R.h)
