ngx_feature="Lua library"
ngx_feature_libs="-llua -lm"
ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <lauxlib.h>"
ngx_feature_path=
ngx_feature_test="(void) luaL_newstate();"

if [ -n "$LUAJIT_INC" -o -n "$LUAJIT_LIB" ]; then
    # explicitly set Lua lib path
    ngx_feature="LuaJIT library in $LUAJIT_LIB and $LUAJIT_INC (specified by the LUAJIT_LIB and LUAJIT_INC env)"
    ngx_feature_path="$LUAJIT_INC"
    if [ $NGX_RPATH = YES ]; then
        ngx_feature_libs="-R$LUAJIT_LIB -L$LUAJIT_LIB -lluajit-5.1 -lm"
    else
        ngx_feature_libs="-L$LUAJIT_LIB -lluajit-5.1 -lm"
    fi

    . auto/feature

    if [ $ngx_found = no ]; then
        cat << END
        $0: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as $LUAJIT_LIB and LUAJIT_INC (path for lua.h) $LUAJIT_INC, but we cannot find LuaJIT there.
END
        exit 1
    fi

    case "$NGX_PLATFORM" in
        Darwin:*)
            case "$NGX_MACHINE" in
                amd64 | x86_64 | i386)
                    echo "adding extra linking options needed by LuaJIT"
                    if [ $HTTP_LUA_SHARED = YES ]; then
                        NGX_EXTEND_LD_OPT="$NGX_EXTEND_LD_OPT -pagezero_size 10000 -image_base 100000000"
                    else
                        ngx_feature_libs="$ngx_feature_libs -pagezero_size 10000 -image_base 100000000"
                    fi
                ;;

                *)
                ;;
            esac
        ;;

        *)
        ;;
    esac
else
    if [ -n "$LUA_INC" -o -n "$LUA_LIB" ]; then
        # explicitly set Lua lib path
        ngx_feature="Lua library in $LUA_LIB and $LUA_INC (specified by the LUA_LIB and LUA_INC env)"
        ngx_feature_path="$LUA_INC"
        if [ $NGX_RPATH = YES ]; then
            ngx_feature_libs="-R$LUA_LIB -L$LUA_LIB -llua -lm"
        else
            ngx_feature_libs="-L$LUA_LIB -llua -lm"
        fi

        . auto/feature

        if [ $ngx_found = no ]; then
            cat << END
            $0: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUA_LIB is defined as $LUA_LIB and LUA_INC (path for lua.h) is $LUA_INC, but we cannot find standard Lua there.
END
            exit 1
        fi
    else
        # auto-discovery
        ngx_feature="Lua library"
        ngx_feature_libs="-llua -lm"
        . auto/feature

        if [ $ngx_found = no ]; then
            # OpenBSD-5.2
            ngx_feature="Lua library in /usr/local/"
            ngx_feature_path="/usr/local/include/lua-5.1"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -llua -lm"
            else
                ngx_feature_libs="-L/usr/local/lib -llua5.1 -lm"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # OpenBSD < 5.2
            ngx_feature="Lua library in /usr/local/"
            ngx_feature_path="/usr/local/include"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -llua -lm"
            else
                ngx_feature_libs="-L/usr/local/lib -llua -lm"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # NetBSD
            ngx_feature="Lua library in /usr/pkg/"
            ngx_feature_path="/usr/pkg/include/"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/pkg/lib -L/usr/pkg/lib -lm -llua"
            else
                ngx_feature_libs="-L/usr/pkg/lib -lm -llua"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # MacPorts
            ngx_feature="Lua library in /opt/local/"
            ngx_feature_path="/opt/local/include"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/opt/local/lib -L/opt/local/lib -lm -llua"
            else
                ngx_feature_libs="-L/opt/local/lib -lm -llua"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # FreeBSD
            ngx_feature="Lua library in /usr/local/*/lua51/"
            ngx_feature_path="/usr/local/include/lua51"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/local/lib/lua51 -L/usr/local/lib/lua51 -llua -lm"
            else
                ngx_feature_libs="-L/usr/local/lib/lua51 -llua -lm"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # Debian
            ngx_feature="Lua library in /usr/"
            ngx_feature_path="/usr/include/lua5.1"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/lib -L/usr/lib -lm -llua5.1"
            else
                ngx_feature_libs="-L/usr/lib -lm -llua5.1"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # FreeBSD with luajit-2.0 from ports collection
            ngx_feature="LuaJIT library in /usr/local/"
            ngx_feature_path="/usr/local/include/luajit-2.0"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/local/lib -L/usr/local/lib -lluajit-5.1 -lm"
            else
                ngx_feature_libs="-L/usr/local/lib -lluajit-5.1 -lm"
            fi
            . auto/feature
        fi

        if [ $ngx_found = no ]; then
            # Gentoo with LuaJIT-2.0
            ngx_feature="LuaJIT library in /usr/"
            ngx_feature_path="/usr/include/luajit-2.0"
            if [ $NGX_RPATH = YES ]; then
                ngx_feature_libs="-R/usr/lib -L/usr/lib -lm -lluajit-5.1"
            else
                ngx_feature_libs="-L/usr/lib -lm -lluajit-5.1"
            fi
            . auto/feature
        fi
    fi
fi

if [ $ngx_found = yes ]; then
    if [ $HTTP_LUA_SHARED = YES ]; then
        DSO_INCS="$DSO_INCS|$NGX_HTTP_LUA_MODULE:$ngx_feature_path"
        DSO_LIBS="$DSO_LIBS|$NGX_HTTP_LUA_MODULE:$ngx_feature_libs"
    else
        CORE_INCS="$CORE_INCS $ngx_feature_path"
        CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
    fi
else
 cat << END
 $0: error: ngx_http_lua_module requires the Lua library.
END
 exit 1
fi

CFLAGS="$CFLAGS -DNDK_SET_VAR"

ngx_feature="export symbols by default"
ngx_feature_libs="-Wl,-E"
ngx_feature_name=
ngx_feature_run=no
ngx_feature_incs="#include <stdio.h>"
ngx_feature_path=
ngx_feature_test='printf("hello");'

. auto/feature

if [ $ngx_found = yes ]; then
   if [ $HTTP_LUA_SHARED = YES ]; then
       DSO_LIBS="$DSO_LIBS $ngx_feature_libs"
   else
       CORE_LIBS="-Wl,-E $CORE_LIBS"
   fi
fi

#NGX_DTRACE_PROVIDERS="$NGX_DTRACE_PROVIDERS $ngx_addon_dir/dtrace/ngx_lua_provider.d"
#NGX_TAPSET_SRCS="$NGX_TAPSET_SRCS $ngx_addon_dir/tapset/ngx_lua.stp"

ngx_feature="SO_PASSCRED"
ngx_feature_libs=
ngx_feature_name="NGX_HTTP_LUA_HAVE_SO_PASSCRED"
ngx_feature_run=no
ngx_feature_incs="#include <sys/types.h>
#include <sys/socket.h>"
ngx_feature_path=
ngx_feature_test='setsockopt(1, SOL_SOCKET, SO_PASSCRED, NULL, 0);'

. auto/feature

