ngx_addon_name=ngx_nchan_module

nchan_libs=""
#do we have hiredis on the platform?
# it's currently no longer possible to link the platform's hiredis lib, 
# because we now use a hacked connect function
# maybe it can be brought back at some later time...
ngx_feature="hiredis with stored sockaddr"
ngx_feature_name="NCHAN_HAVE_HIREDIS_WITH_SOCKADDR"
ngx_feature_run=yes
ngx_feature_path=
ngx_feature_incs=" \
  #include <hiredis/hiredis.h>
  #include <sys/socket.h>
"
ngx_feature_libs="-lhiredis"
ngx_feature_test=" \
  redisContext c; \
  if(HIREDIS_SONAME < 0.13) { return 1; } \
  if(sizeof(c.sockaddr) != sizeof(struct sockaddr)) { return 1;} \
"
. auto/feature
if [ $ngx_found = no ]; then
  _NCHAN_HIREDIS_SRCS="\
    ${ngx_addon_dir}/src/store/redis/hiredis/hiredis.c \
    ${ngx_addon_dir}/src/store/redis/hiredis/read.c \
    ${ngx_addon_dir}/src/store/redis/hiredis/async.c \
    ${ngx_addon_dir}/src/store/redis/hiredis/sds.c \
    ${ngx_addon_dir}/src/store/redis/hiredis/net.c \
  "
  ngx_feature_libs=""
else
  nchan_libs="$nchan_libs $ngx_feature_libs"
  _NCHAN_HIREDIS_SRCS=""
fi

ngx_feature="math lib"
ngx_feature_name="NCHAN_HAVE_MATH"
ngx_feature_run=yes
ngx_feature_path=
ngx_feature_incs="#include <math.h>"
ngx_feature_libs="-lm"
ngx_feature_test="sqrt(20);"
. auto/feature
if [ $ngx_found = yes ]; then
  nchan_libs="$nchan_libs $ngx_feature_libs"
fi

#do we have memrchr() on the platform?
ngx_feature="memrchr()"
ngx_feature_name="NCHAN_HAVE_MEMRCHR"
ngx_feature_run=yes
ngx_feature_path=
ngx_feature_libs=
ngx_feature_incs=" \
  #include <string.h> 
  #include <stddef.h> 
"
ngx_feature_test=" \
  const char *str = \"aboobar\"; \
  const void *place = &str[4]; \
  const void *found = memrchr(str, 'b', strlen(str)); \
  if(place != found) { return 1; } \
"
. auto/feature

_NCHAN_SUBSCRIBERS_SRCS="\
  ${ngx_addon_dir}/src/subscribers/common.c \
  ${ngx_addon_dir}/src/subscribers/longpoll.c \
  ${ngx_addon_dir}/src/subscribers/intervalpoll.c \
  ${ngx_addon_dir}/src/subscribers/eventsource.c \
  ${ngx_addon_dir}/src/subscribers/http-chunked.c \
  ${ngx_addon_dir}/src/subscribers/http-multipart-mixed.c \
  ${ngx_addon_dir}/src/subscribers/http-raw-stream.c \
  ${ngx_addon_dir}/src/subscribers/websocket.c \
  ${ngx_addon_dir}/src/nchan_websocket_publisher.c \
  ${ngx_addon_dir}/src/subscribers/internal.c \
  ${ngx_addon_dir}/src/subscribers/memstore_ipc.c \
  ${ngx_addon_dir}/src/subscribers/memstore_multi.c \
  ${ngx_addon_dir}/src/subscribers/memstore_redis.c \
  ${ngx_addon_dir}/src/subscribers/getmsg_proxy.c \
  ${ngx_addon_dir}/src/subscribers/benchmark.c \
"

_NCHAN_REDIS_STORE_SRCS="\
  ${_NCHAN_HIREDIS_SRCS} \
  ${ngx_addon_dir}/src/store/redis/cmp.c \
  ${ngx_addon_dir}/src/store/redis/redis_lua_commands.c \
  ${ngx_addon_dir}/src/store/redis/redis_nodeset_parser.c \
  ${ngx_addon_dir}/src/store/redis/redis_nodeset.c \
  ${ngx_addon_dir}/src/store/redis/rdsstore.c \
  ${ngx_addon_dir}/src/store/redis/redis_nginx_adapter.c \
"
_NCHAN_MEMORY_STORE_SRCS="\
  ${ngx_addon_dir}/src/store/memory/ipc.c \
  ${ngx_addon_dir}/src/store/memory/ipc-handlers.c \
  ${ngx_addon_dir}/src/store/memory/groups.c \
  ${ngx_addon_dir}/src/store/memory/memstore.c \
"

_nchan_util_dir="${ngx_addon_dir}/src/util"
_NCHAN_UTIL_SRCS=" \
  $_nchan_util_dir/nchan_debug.c \
  $_nchan_util_dir/nchan_list.c \
  $_nchan_util_dir/nchan_slist.c \
  $_nchan_util_dir/ngx_nchan_hacked_slab.c \
  $_nchan_util_dir/shmem.c \
  $_nchan_util_dir/nchan_rbtree.c \
  $_nchan_util_dir/nchan_reuse_queue.c \
  $_nchan_util_dir/nchan_output.c \
  $_nchan_util_dir/nchan_util.c \
  $_nchan_util_dir/nchan_fake_request.c \
  $_nchan_util_dir/nchan_bufchainpool.c \
  $_nchan_util_dir/nchan_channel_id.c \
  $_nchan_util_dir/nchan_output_info.c \
  $_nchan_util_dir/nchan_msg.c \
  $_nchan_util_dir/nchan_thingcache.c \
  $_nchan_util_dir/nchan_reaper.c \
  $_nchan_util_dir/nchan_subrequest.c \
  $_nchan_util_dir/nchan_benchmark.c \
  $_nchan_util_dir/hdr_histogram.c \
"

_NCHAN_STORE_SRCS="\
  ${ngx_addon_dir}/src/store/spool.c \
  ${ngx_addon_dir}/src/store/ngx_rwlock.c \
  ${ngx_addon_dir}/src/store/store_common.c \
  $_NCHAN_MEMORY_STORE_SRCS \
  $_NCHAN_REDIS_STORE_SRCS \
"

_NCHAN_SRCS="\
  ${ngx_addon_dir}/src/nchan_defs.c \
  ${ngx_addon_dir}/src/nchan_variables.c \
  ${ngx_addon_dir}/src/nchan_module.c \
  $_NCHAN_UTIL_SRCS \
  $_NCHAN_SUBSCRIBERS_SRCS \
  $_NCHAN_STORE_SRCS \
"

ngx_module_incs=$ngx_addon_dir/src

have=NGX_HTTP_HEADERS . auto/have

if test -n "$ngx_module_link"; then
  ngx_module_type=HTTP
  ngx_module_name=$ngx_addon_name
  ngx_module_srcs="$_NCHAN_SRCS"
  ngx_module_libs=$nchan_libs
  . auto/module
else
  NGX_ADDON_SRCS="$NGX_ADDON_SRCS $_NCHAN_SRCS"
  CORE_LIBS="$CORE_LIBS $nchan_libs"
  CORE_INCS="$CORE_INCS $ngx_module_incs"
  HTTP_MODULES="$HTTP_MODULES $ngx_addon_name"
fi
