MODULES+=	lua

MODULE_SUMMARY_lua=	3rd-party Lua dynamic modules

include $(CONTRIB)/src/luajit2/version
include $(CONTRIB)/src/ngx_devel_kit/version
include $(CONTRIB)/src/lua-nginx-module/version
include $(CONTRIB)/src/stream-lua-nginx-module/version
include $(CONTRIB)/src/lua-resty-core/version
include $(CONTRIB)/src/lua-resty-lrucache/version

MODULE_VERSION_lua=	$(LUA_NGINX_MODULE_VERSION)
MODULE_RELEASE_lua=	1

MODULE_VERSION_PREFIX_lua=$(MODULE_TARGET_PREFIX)

MODULE_SOURCES_lua=	ngx_devel_kit-$(NGX_DEVEL_KIT_VERSION).tar.gz \
			lua-nginx-module-$(LUA_NGINX_MODULE_VERSION).tar.gz \
			stream-lua-nginx-module-$(STREAM_LUA_NGINX_MODULE_VERSION).tar.gz \
			luajit2-$(LUAJIT2_VERSION).tar.gz \
			lua-resty-core-$(LUA_RESTY_CORE_VERSION).tar.gz \
			lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION).tar.gz

MODULE_PATCHES_lua=	$(CONTRIB)/src/luajit2/Makefile.patch \
			$(CONTRIB)/src/luajit2/src-luaconf.h.patch \
			$(CONTRIB)/src/lua-nginx-module/config.patch \
			$(CONTRIB)/src/stream-lua-nginx-module/config.patch

MODULE_CONFARGS_lua=	--with-http_dav_module \
			--with-http_realip_module \
			--with-http_ssl_module \
			--with-http_v2_module \
			--with-stream \
			--with-stream_ssl_module \
			--add-dynamic-module=$(MODSRC_PREFIX)ngx_devel_kit-$(NGX_DEVEL_KIT_VERSION) \
			--add-dynamic-module=$(MODSRC_PREFIX)lua-nginx-module-$(LUA_NGINX_MODULE_VERSION) \
			--add-dynamic-module=$(MODSRC_PREFIX)stream-lua-nginx-module-$(STREAM_LUA_NGINX_MODULE_VERSION)

MODULE_BUILD_DEPENDS_lua=pcre-dev

.deps-module-lua:
	cd $(CONTRIB) && make \
		.sum-luajit2 \
		.sum-ngx_devel_kit \
		.sum-lua-nginx-module \
		.sum-stream-lua-nginx-module \
		.sum-lua-resty-core \
		.sum-lua-resty-lrucache
	touch $@

prerequisites-for-module-lua: module-ndk

MODULE_DEPENDS_lua=$(NDK_DEPENDENCY_TAG)

define MODULE_PREBUILD_lua
	cd ../luajit2-$(LUAJIT2_VERSION) \&\& \
	DESTDIR=`pwd` CFLAGS="-fPIC" make $$_make_opts install
endef
export MODULE_PREBUILD_lua

MODULE_LD_OPT_lua=-lpcre
MODULE_LD_OPT_DEBUG_lua=$(MODULE_LD_OPT_lua)

define MODULE_ENV_lua
LUAJIT_INC="../luajit2-$(LUAJIT2_VERSION)/usr/include/nginx-luajit-2.1" LUAJIT_LIB="../luajit2-$(LUAJIT2_VERSION)/usr/lib"
endef
export MODULE_ENV_lua

define MODULE_PREINSTALL_lua
	rm -f objs/ndk_http_module-debug.so
	rm -f objs/ndk_http_module.so
	mkdir -p "$$pkgdir"/usr/bin
	mkdir -p "$$pkgdir"/usr/share/man/man1
        install -m755 ../luajit2-$(LUAJIT2_VERSION)/usr/bin/luajit "$$pkgdir"/usr/bin/nginx-luajit
        install -m644 ../luajit2-$(LUAJIT2_VERSION)/usr/share/man/man1/luajit.1 "$$pkgdir"/usr/share/man/man1/nginx-luajit.1
	if [ -z "`echo $subpackages | grep -- -doc`" ]; then
		gzip "$$pkgdir"/usr/share/man/man1/nginx-luajit.1
	fi
	mkdir -p "$$pkgdir"/usr/share/nginx-luajit-2.1/jit
        for f in `find ../luajit2-$(LUAJIT2_VERSION)/usr/share/nginx-luajit-2.1/jit/ -type f`; do \
                install -m644 $${f} "$$pkgdir"/usr/share/nginx-luajit-2.1/jit/ ; \
        done
	mkdir -p "$$pkgdir"/usr/include/nginx-luajit-2.1
        for f in `find ../luajit2-$(LUAJIT2_VERSION)/usr/include/nginx-luajit-2.1/ -type f`; do \
                install -m644 $${f} "$$pkgdir"/usr/include/nginx-luajit-2.1/ ; \
        done
	mkdir -p "$$pkgdir"/usr/share/nginx-luajit-2.1/resty
	( cd ../lua-resty-core-$(LUA_RESTY_CORE_VERSION) \&\& \
		LUA_LIB_DIR="$$pkgdir"/usr/share/nginx-luajit-2.1 make install )
	( cd ../lua-resty-lrucache-$(LUA_RESTY_LRUCACHE_VERSION) \&\& \
		LUA_LIB_DIR="$$pkgdir"/usr/share/nginx-luajit-2.1 make install )

endef
export MODULE_PREINSTALL_lua

define MODULE_POST_lua
cat <<BANNER
----------------------------------------------------------------------

The $(MODULE_SUMMARY_lua) for $(MODULE_SUMMARY_PREFIX) have been installed.
To enable these modules, add the following to /etc/nginx/nginx.conf
and reload nginx:

    load_module modules/ndk_http_module.so;
    load_module modules/ngx_http_lua_module.so;
    load_module modules/ngx_stream_lua_module.so;

Note that ndk_http_module.so must be placed first.

These modules are compiled with LuaJIT 2.1 library.
The following binary should be used for testing and bytecode generation:

    /usr/bin/nginx-luajit

Please refer to the modules documentation for further details:
https://github.com/openresty/lua-nginx-module
https://github.com/openresty/stream-lua-nginx-module

----------------------------------------------------------------------
BANNER
endef
export MODULE_POST_lua
