# Platform-specific environment
AC_CANONICAL_HOST
AC_CANONICAL_BUILD
case $host in
 i686-pc-linux-gnu)
CPPFLAGS="-I/usr/local/include/modbus -I/usr/include"
LDFLAGS="-L/usr/lib/i386-linux-gnu -L/usr/local/lib"
LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus -lcrypto"
FLAGS="-O0 -g"
CXXFLAGS="-O0 -g"
;;
 armv7l-unknown-linux-gnueabi)
LDFLAGS="-L/usr/lib -L/usr/local/lib"
LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus -lcrypto"
CPP_FLAGS="-I/usr/local/include/modbus -I/usr/local/include -I/usr/include"
CFLAGS="-I/usr/include/modbus -I/usr/include -I/usr/local/include"
LD_LIBRARY_PATH="/usr/local/lib"
FLAGS="-O0 -g"
CXXFLAGS="-O0 -g"
;;
 arm-angstrom-linux-gnueabi)
# Leave this empty so oe/bitbake will configure
# it's own environment
;;
 x86_64-apple-darwin*)
CPPFLAGS="-I/usr/local/include/modbus -I/usr/include"
LDFLAGS="-L/usr/lib/i386-linux-gnu -L/usr/local/lib"
LIBS="-lpthread -lrt -lzmq -lczmq -luuid -lm -lmodbus -lcrypto"
FLAGS="-O0 -g"
CXXFLAGS="-O0 -g"
;;
*)
AC_MSG_ERROR([Platform $host not supported])
;;
esac
/////////////////////////
# Checks for libraries.
AC_CHECK_LIB(rt, nanosleep, ,[AC_MSG_ERROR([cannot link with -lrt, install librt.])])
AC_CHECK_LIB(m, pow, ,[AC_MSG_ERROR([cannot link with -lm, install libm.])])

# libuuid
AC_ARG_WITH([libuuid],
            [AS_HELP_STRING([--with-libuuid],
                            [Specify libuuid prefix])],
            [fmq_search_libuuid="yes"],
            [])

if test "x$fmq_search_libuuid" = "xyes"; then
    if test -r "${with_libuuid}/include/uuid/uuid.h"; then
        CFLAGS="-I${with_libuuid}/include/uuid ${CFLAGS}"
        LDFLAGS="-L${with_libuuid}/lib ${LDFLAGS}"
    else
        AC_MSG_ERROR([${with_libuuid}/include/uuid.h not found. Please check libuuid prefix])
    fi
fi
AC_CHECK_LIB(uuid, uuid_generate, ,[AC_MSG_ERROR([cannot link with -luuid, install libuuid.])])

# libzmq
AC_ARG_WITH([libzmq],
            [AS_HELP_STRING([--with-libzmq],
                            [Specify libzmq prefix])],
            [fmq_search_libzmq="yes"],
            [])

if test "x$fmq_search_libzmq" = "xyes"; then
    if test -r "${with_libzmq}/include/zmq.h"; then
        CFLAGS="-I${with_libzmq}/include ${CFLAGS}"
        LDFLAGS="-L${with_libzmq}/lib ${LDFLAGS}"
    else
        AC_MSG_ERROR([${with_libzmq}/include/zmq.h not found. Please check libzmq prefix])
    fi
fi
AC_CHECK_LIB(zmq, zmq_init, ,[AC_MSG_ERROR([cannot link with -lzmq, install libzmq.])])

# libczmq
AC_ARG_WITH([libczmq],
            [AS_HELP_STRING([--with-libczmq],
                            [Specify libczmq prefix])],
            [fmq_search_libczmq="yes"],
            [])

if test "x$fmq_search_libczmq" = "xyes"; then
    if test -r "${with_libczmq}/include/czmq.h"; then
        CFLAGS="-I${with_libczmq}/include ${CFLAGS}"
        LDFLAGS="-L${with_libczmq}/lib ${LDFLAGS}"
    else
        AC_MSG_ERROR([${with_libczmq}/include/czmq.h not found. Please check libczmq prefix])
    fi
fi
AC_CHECK_LIB(czmq, zctx_new, ,[AC_MSG_ERROR([cannot link with -lczmq, install libczmq.])])

# libpthread
AC_ARG_WITH([libpthread],
            [AS_HELP_STRING([--with-libpthread],
                            [Specify libpthread prefix])],
            [fmq_search_libpthread="yes"],
            [])

if test "x$fmq_search_libpthread" = "xyes"; then
    if test -r "${with_libpthread}/include/pthread.h"; then
        CFLAGS="-I${with_libpthread}/include ${CFLAGS}"
        LDFLAGS="-L${with_libpthread}/lib ${LDFLAGS}"
    else
        AC_MSG_ERROR([${with_libpthread}/include/pthread.h not found. Please check libpthread prefix])
    fi
fi
AC_CHECK_LIB(pthread, pthread_kill, ,[AC_MSG_ERROR([cannot link with -lpthread, install libpthread.])])

# libmodbus
AC_ARG_WITH([libmodbus],
            [AS_HELP_STRING([--with-libmodbus],
                            [Specify libmodbus prefix])],
            [fmq_search_libmodbus="yes"],
            [])

if test "x$fmq_search_libmodbus" = "xyes"; then
    if test -r "${with_libmodbus}/include/modbus/modbus.h"; then
        CFLAGS="-I${with_libmodbus}/include/modbus ${CFLAGS}"
        LDFLAGS="-L${with_libmodbus}/lib ${LDFLAGS}"
    else
        AC_MSG_ERROR([${with_libmodbus}/include/modbus/modbus.h not found. Please check modbus prefix])
    fi
else
	AC_MSG_ERROR([${with_libmodbus}/include/modbus/modbus.h not found. Please check modbus prefix])
fi
AC_CHECK_LIB(modbus, modbus_new_tcp, ,[AC_MSG_ERROR([cannot link with -lmodbus, install libmodbus.])])

/////////////////////////
#AC_CHECK_LIB(pthread, pthread_kill)
#AC_CHECK_LIB(modbus, modbus_free)
#AC_LIB_LINKFLAGS(name, [dependencies])
AC_CHECK_LIB(modbus, modbus_new_tcp, ,[AC_MSG_ERROR([cannot link with -lmodbus, install libmodbus.])])
AC_CHECK_LIB(pthread, pthread_kill, ,[AC_MSG_ERROR([cannot link with -lpthread, install libpthread.])])

AC_CHECK_LIB(modbus, , ,[AC_MSG_ERROR([cannot link with -lmodbus, install libmodbus.])])
AC_CHECK_LIB(pthread, , ,[AC_MSG_ERROR([cannot link with -lpthread, install libpthread.])])
AC_CHECK_LIB(rt, , ,[AC_MSG_ERROR([cannot link with -lrt, install librt.])])
AC_CHECK_LIB(zmq, , ,[AC_MSG_ERROR([cannot link with -lzmq, install libzmq.])])
AC_CHECK_LIB(czmq, , ,[AC_MSG_ERROR([cannot link with -lczmq, install libczmq.])])
AC_CHECK_LIB(uuid, , ,[AC_MSG_ERROR([cannot link with -luuid, install libuuid.])])
AC_CHECK_LIB(m, , ,[AC_MSG_ERROR([cannot link with -lm, install libm.])])
AC_CHECK_LIB(crypto, , ,[AC_MSG_ERROR([cannot link with -lcrypto, install libcrypto.])])

AC_LIB_HAVE_LINKFLAGS(pthread)
AC_LIB_HAVE_LINKFLAGS(rt)
AC_LIB_HAVE_LINKFLAGS(zmq)
AC_LIB_HAVE_LINKFLAGS(czmq)
AC_LIB_HAVE_LINKFLAGS(uuid)
AC_LIB_HAVE_LINKFLAGS(m)
AC_LIB_HAVE_LINKFLAGS(modbus)
AC_LIB_HAVE_LINKFLAGS(crypto)

AC_LIB_LINKFLAGS(pthread)
AC_LIB_LINKFLAGS(rt)
AC_LIB_LINKFLAGS(zmq)
AC_LIB_LINKFLAGS(czmq)
AC_LIB_LINKFLAGS(uuid)
AC_LIB_LINKFLAGS(m)
AC_LIB_LINKFLAGS(modbus)
AC_LIB_LINKFLAGS(crypto)

AC_CONFIG_AUX_DIR([build-aux])
AC_LIB_LINKFLAGS(pthread)
AC_LIB_LINKFLAGS(rt)
AC_LIB_LINKFLAGS(zmq)
AC_LIB_LINKFLAGS(czmq)
AC_LIB_LINKFLAGS(uuid)
AC_LIB_LINKFLAGS(m)
AC_LIB_LINKFLAGS(modbus)
AC_LIB_LINKFLAGS(crypto)