AC_INIT
AC_CONFIG_SRCDIR([DESCRIPTION])

AC_ARG_ENABLE(debug, [--enable-debug turn on debugging information])

: ${R_HOME=`R RHOME`}
if test -z "${R_HOME}"; then
  echo "could not determine R_HOME"
  exit 1
fi
CC=`"${R_HOME}/bin/R" CMD config CC`
CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`
CPPFLAGS=`"${R_HOME}/bin/R" CMD config CPPFLAGS`

if test -z "${CURL_CONFIG}" ; then
 AC_PATH_PROG(CURL_CONFIG, curl-config)
fi

if test -z "${CURL_CONFIG}" ; then
  echo "Cannot find curl-config"
  exit 1
fi

if test -n "${enable_debug}" && test "${enable_debug}" = "yes" ; then
 DEFINES=-DRCURL_DEBUG_MEMORY
fi


dnl Trying to find out if we have a libidn field.
AC_PROG_CC
AC_PROG_CPP

CURL_CPPFLAGS=`$CURL_CONFIG --cflags`
CPPFLAGS="${CPPFLAGS} $CURL_CPPFLAGS"

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <curl/curl.h>
]], [[
 curl_version_info_data *data;
 char *ptr;
 ptr = data->libidn;
]])],[DEFINES="$DEFINES -DHAVE_LIBIDN_FIELD=1"; echo "Version has a libidn field"],[echo "No libidn field in version structure."])


LDFLAGS=`${CURL_CONFIG} --libs`


AC_DEFUN([RCURL_CHECK_ENUM], [

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
#include <curl/curl.h>
]], [[
  int val;
  val = $1;
]])],[DEFINES="$DEFINES -DHAVE_$1=1"; echo "Version has $1"],[echo "No $1 enumeration value."])
])

RCURL_CHECK_ENUM(CURLOPT_URL)



RCURL_CHECK_ENUM(CURLINFO_EFFECTIVE_URL)
RCURL_CHECK_ENUM(CURLINFO_RESPONSE_CODE)
RCURL_CHECK_ENUM(CURLINFO_TOTAL_TIME)
RCURL_CHECK_ENUM(CURLINFO_NAMELOOKUP_TIME)
RCURL_CHECK_ENUM(CURLINFO_CONNECT_TIME)
RCURL_CHECK_ENUM(CURLINFO_PRETRANSFER_TIME)
RCURL_CHECK_ENUM(CURLINFO_SIZE_UPLOAD)
RCURL_CHECK_ENUM(CURLINFO_SIZE_DOWNLOAD)
RCURL_CHECK_ENUM(CURLINFO_SPEED_DOWNLOAD)
RCURL_CHECK_ENUM(CURLINFO_SPEED_UPLOAD)
RCURL_CHECK_ENUM(CURLINFO_HEADER_SIZE)
RCURL_CHECK_ENUM(CURLINFO_REQUEST_SIZE)
RCURL_CHECK_ENUM(CURLINFO_SSL_VERIFYRESULT)
RCURL_CHECK_ENUM(CURLINFO_FILETIME)
RCURL_CHECK_ENUM(CURLINFO_CONTENT_LENGTH_DOWNLOAD)
RCURL_CHECK_ENUM(CURLINFO_CONTENT_LENGTH_UPLOAD)
RCURL_CHECK_ENUM(CURLINFO_STARTTRANSFER_TIME)
RCURL_CHECK_ENUM(CURLINFO_CONTENT_TYPE)
RCURL_CHECK_ENUM(CURLINFO_REDIRECT_TIME)
RCURL_CHECK_ENUM(CURLINFO_REDIRECT_COUNT)
RCURL_CHECK_ENUM(CURLINFO_PRIVATE)
RCURL_CHECK_ENUM(CURLINFO_HTTP_CONNECTCODE)
RCURL_CHECK_ENUM(CURLINFO_HTTPAUTH_AVAIL)
RCURL_CHECK_ENUM(CURLINFO_PROXYAUTH_AVAIL)
RCURL_CHECK_ENUM(CURLINFO_OS_ERRNO)
RCURL_CHECK_ENUM(CURLINFO_NUM_CONNECTS)
RCURL_CHECK_ENUM(CURLINFO_SSL_ENGINES)
RCURL_CHECK_ENUM(CURLINFO_COOKIELIST)
RCURL_CHECK_ENUM(CURLINFO_LASTSOCKET)
RCURL_CHECK_ENUM(CURLINFO_FTP_ENTRY_PATH)
RCURL_CHECK_ENUM(CURLINFO_REDIRECT_URL)
RCURL_CHECK_ENUM(CURLINFO_PRIMARY_IP)
RCURL_CHECK_ENUM(CURLINFO_APPCONNECT_TIME)
RCURL_CHECK_ENUM(CURLINFO_CERTINFO)
RCURL_CHECK_ENUM(CURLINFO_CONDITION_UNMET)


dnl RCURL_CHECK_ENUM(CURLINFO_CONDITION_UNMET)
dnl RCURL_CHECK_ENUM(CURLINFO_REDIRECT_URL)
dnl RCURL_CHECK_ENUM(CURLINFO_CERTINFO)
dnl RCURL_CHECK_ENUM(CURLINFO_PRIMARY_IP)
dnl RCURL_CHECK_ENUM(CURLINFO_APPCONNECT_TIME)


 RCURL_CHECK_ENUM(CURLOPT_WRITEDATA)
 RCURL_CHECK_ENUM(CURLOPT_KEYPASSWD)
 RCURL_CHECK_ENUM(CURLOPT_DIRLISTONLY)
 RCURL_CHECK_ENUM(CURLOPT_APPEND)
 RCURL_CHECK_ENUM(CURLOPT_KRBLEVEL)
 RCURL_CHECK_ENUM(CURLOPT_USE_SSL)
 RCURL_CHECK_ENUM(CURLOPT_TIMEOUT_MS)
 RCURL_CHECK_ENUM(CURLOPT_CONNECTTIMEOUT_MS)
 RCURL_CHECK_ENUM(CURLOPT_HTTP_TRANSFER_DECODING)
 RCURL_CHECK_ENUM(CURLOPT_HTTP_CONTENT_DECODING)
 RCURL_CHECK_ENUM(CURLOPT_NEW_FILE_PERMS)
 RCURL_CHECK_ENUM(CURLOPT_NEW_DIRECTORY_PERMS)
 RCURL_CHECK_ENUM(CURLOPT_POSTREDIR)
 RCURL_CHECK_ENUM(CURLOPT_OPENSOCKETFUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_OPENSOCKETDATA)
 RCURL_CHECK_ENUM(CURLOPT_COPYPOSTFIELDS)
 RCURL_CHECK_ENUM(CURLOPT_PROXY_TRANSFER_MODE)
 RCURL_CHECK_ENUM(CURLOPT_SEEKFUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_SEEKDATA)
 RCURL_CHECK_ENUM(CURLOPT_CRLFILE)
 RCURL_CHECK_ENUM(CURLOPT_ISSUERCERT)
 RCURL_CHECK_ENUM(CURLOPT_ADDRESS_SCOPE)
 RCURL_CHECK_ENUM(CURLOPT_CERTINFO)
 RCURL_CHECK_ENUM(CURLOPT_USERNAME)
 RCURL_CHECK_ENUM(CURLOPT_PASSWORD)
 RCURL_CHECK_ENUM(CURLOPT_PROXYUSERNAME)
 RCURL_CHECK_ENUM(CURLOPT_PROXYPASSWORD)
 RCURL_CHECK_ENUM(CURLOPT_SSH_HOST_PUBLIC_KEY_MD5)
 RCURL_CHECK_ENUM(CURLOPT_NOPROXY)
 RCURL_CHECK_ENUM(CURLOPT_TFTP_BLKSIZE)
 RCURL_CHECK_ENUM(CURLOPT_SOCKS5_GSSAPI_SERVICE)
 RCURL_CHECK_ENUM(CURLOPT_SOCKS5_GSSAPI_NEC)
 RCURL_CHECK_ENUM(CURLOPT_PROTOCOLS)
 RCURL_CHECK_ENUM(CURLOPT_REDIR_PROTOCOLS)
 RCURL_CHECK_ENUM(CURLOPT_SSH_AUTH_TYPES)
 RCURL_CHECK_ENUM(CURLOPT_SSH_PUBLIC_KEYFILE)
 RCURL_CHECK_ENUM(CURLOPT_SSH_PRIVATE_KEYFILE)
 RCURL_CHECK_ENUM(CURLOPT_FTP_SSL_CCC)
 RCURL_CHECK_ENUM(CURLOPT_COOKIELIST)
 RCURL_CHECK_ENUM(CURLOPT_IGNORE_CONTENT_LENGTH)
 RCURL_CHECK_ENUM(CURLOPT_FTP_SKIP_PASV_IP)
 RCURL_CHECK_ENUM(CURLOPT_FTP_FILEMETHOD)
 RCURL_CHECK_ENUM(CURLOPT_LOCALPORT)
 RCURL_CHECK_ENUM(CURLOPT_LOCALPORTRANGE)
 RCURL_CHECK_ENUM(CURLOPT_CONNECT_ONLY)
 RCURL_CHECK_ENUM(CURLOPT_CONV_FROM_NETWORK_FUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_CONV_TO_NETWORK_FUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_CONV_FROM_UTF8_FUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_MAX_SEND_SPEED_LARGE)
 RCURL_CHECK_ENUM(CURLOPT_MAX_RECV_SPEED_LARGE)
 RCURL_CHECK_ENUM(CURLOPT_FTP_ALTERNATIVE_TO_USER)
 RCURL_CHECK_ENUM(CURLOPT_SOCKOPTFUNCTION)
 RCURL_CHECK_ENUM(CURLOPT_SOCKOPTDATA)
 RCURL_CHECK_ENUM(CURLOPT_SSL_SESSIONID_CACHE)


dnl Next we determine if the Curl_base64_encode routine has an extra parameter
dnl which appeared in curl-7.18.0 (approx?). This routine is not in the header
dnl file, so we can't use compilation warnings, etc. So we use the old style call and
dnl make  a sample program that uses this invocation. If it seg faults, we need the new style.
#AC_RUN_IFELSE([AC_LANG_SOURCE([[
##include <curl/curl.h>
##include <string.h>
#
#int main(int argc, char *argv[])
#{
#  char text[100] = "Some text", ans[200];
#  int len;
#  len = Curl_base64_encode(text, strlen(text), &ans);
#  return(0);
#}
#]])],[#echo "Using older Curl_base64_enccode form with _no_ SessionHandle parameter."],[#echo "Using Curl_base64_encode with SessionHandle parameter."; DEFINES="$DEFINES -DBASE64_NEEDS_SESSION_DATA=1" 
#],[])


CURL_LIBS=`${CURL_CONFIG} --libs`
CURL_CFLAGS=`${CURL_CONFIG} --cflags`


AC_SUBST(DEFINES)

AC_SUBST(CURL_CONFIG)
AC_SUBST(CURL_CFLAGS)
AC_SUBST(CURL_LIBS)


echo "libcurl version: `${CURL_CONFIG} --version`"

AC_CONFIG_FILES([src/Makevars])
AC_OUTPUT


