include $(TOPDIR)/rules.mk

PKG_NAME:=lua-eco
PKG_VERSION:=4.0.0
PKG_RELEASE:=1

PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL=https://github.com/zhaojh329/lua-eco/releases/download/v$(PKG_VERSION)
PKG_HASH:=285427f29016afa59799f1caa12d35fdeb0a041908c0e8d6c6994783b1f39717

PKG_MAINTAINER:=Jianhui Zhao <zhaojh329@gmail.com>
PKG_LICENSE:=MIT
PKG_LICENSE_FILES:=LICENSE

PKG_CONFIG_DEPENDS:= \
	LUA_ECO_OPENSSL \
	LUA_ECO_WOLFSSL \
	LUA_ECO_MBEDTLS

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/lua-eco
  TITLE:=A Lua interpreter with a built-in epoll based event loop
  SECTION:=lang
  CATEGORY:=Languages
  SUBMENU:=Lua
  URL:=https://github.com/zhaojh329/lua-eco
  DEPENDS:=+liblua5.4
endef

define Package/lua-eco/description
  Lua-eco is a coroutine-first runtime environment for Lua 5.4.
  At its internal is an **I/O-event-driven coroutine scheduler** (Linux `epoll` + timers): all I/O APIs
  use **non-blocking file descriptors** under the hood while exposing a **synchronous** programming style
  in Lua.
endef

define Package/lua-eco/Module
  TITLE:=$1 support for lua-eco
  SECTION:=lang
  CATEGORY:=Languages
  SUBMENU:=Lua
  URL:=https://github.com/zhaojh329/lua-eco
  DEPENDS:=+lua-eco $2
endef

Package/lua-eco-log=$(call Package/lua-eco/Module,log utils)
Package/lua-eco-base64=$(call Package/lua-eco/Module,base64)
Package/lua-eco-sha1=$(call Package/lua-eco/Module,sha1)
Package/lua-eco-sha256=$(call Package/lua-eco/Module,sha256)
Package/lua-eco-md5=$(call Package/lua-eco/Module,md5)
Package/lua-eco-hmac=$(call Package/lua-eco/Module,hmac)
Package/lua-eco-socket=$(call Package/lua-eco/Module,socket)
Package/lua-eco-dns=$(call Package/lua-eco/Module,dns,+lua-eco-socket)
Package/lua-eco-ssl=$(call Package/lua-eco/Module,ssl,\
  +LUA_ECO_OPENSSL:libopenssl +LUA_ECO_WOLFSSL:libwolfssl \
  +LUA_ECO_MBEDTLS:libmbedtls +LUA_ECO_MBEDTLS:zlib +lua-eco-socket)
Package/lua-eco-ubus=$(call Package/lua-eco/Module,ubus,+libubus)
Package/lua-eco-http=$(call Package/lua-eco/Module,http/https,+lua-eco-dns +lua-eco-ssl +lua-eco-base64 +lua-eco-log)
Package/lua-eco-mqtt=$(call Package/lua-eco/Module,mqtt,+lua-eco-socket +lua-eco-ssl)
Package/lua-eco-websocket=$(call Package/lua-eco/Module,websocket,+lua-eco-http +lua-eco-base64 +lua-eco-sha1)
Package/lua-eco-termios=$(call Package/lua-eco/Module,termios)
Package/lua-eco-netlink=$(call Package/lua-eco/Module,netlink,+lua-eco-socket)
Package/lua-eco-ip=$(call Package/lua-eco/Module,ip utils,+lua-eco-netlink)
Package/lua-eco-nl80211=$(call Package/lua-eco/Module,nl80211,+lua-eco-netlink)
Package/lua-eco-ssh=$(call Package/lua-eco/Module,ssh,+lua-eco-socket +libssh2)
Package/lua-eco-packet=$(call Package/lua-eco/Module,packet,+lua-eco-nl80211)
Package/lua-eco-uci=$(call Package/lua-eco/Module,uci,+libuci)
Package/lua-eco-shared=$(call Package/lua-eco/Module,shared-memory dictionary)
Package/lua-eco-net=$(call Package/lua-eco/Module,net,+lua-eco-packet +lua-eco-dns)

define Package/lua-eco-ssl/config
	choice
		prompt "SSL Library"
		default LUA_ECO_MBEDTLS

		config LUA_ECO_OPENSSL
			bool "OpenSSL"

		config LUA_ECO_WOLFSSL
			bool "wolfSSL"

		config LUA_ECO_MBEDTLS
			bool "mbedTLS"
	endchoice
endef

CMAKE_OPTIONS += \
  -DECO_UBUS_SUPPORT=O$(if $(CONFIG_PACKAGE_lua-eco-ubus),N,FF) \
  -DECO_SSL_SUPPORT=O$(if $(CONFIG_PACKAGE_lua-eco-ssl),N,FF)

ifneq ($(CONFIG_PACKAGE_lua-eco-ssl),)
  ifneq ($(CONFIG_LUA_ECO_OPENSSL),)
    CMAKE_OPTIONS += -DUSE_OPENSSL=ON
  else ifneq ($(CONFIG_LUA_ECO_WOLFSSL),)
    CMAKE_OPTIONS += -DUSE_WOLFSSL=ON
  else ifneq ($(CONFIG_LUA_ECO_MBEDTLS),)
    CMAKE_OPTIONS += -DUSE_MBEDTLS=ON
  endif
endif

define Package/lua-eco/install
	$(INSTALL_DIR) $(1)/usr/bin $(1)/usr/local/lib/lua/5.4/eco/internal \
		$(1)/usr/local/lib/lua/5.4/eco/encoding
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/eco $(1)/usr/bin
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/hex.lua $(1)/usr/local/lib/lua/5.4/eco/encoding
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/{time,sys,file,sync}.so $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/{time,sys,file,sync,channel}.lua $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-log/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/log.so $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-base64/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/encoding
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/base64.so $(1)/usr/local/lib/lua/5.4/eco/encoding
endef

define Package/lua-eco-sha1/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/hash
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hash/sha1.so $(1)/usr/local/lib/lua/5.4/eco/hash
endef

define Package/lua-eco-sha256/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/hash
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hash/sha256.so $(1)/usr/local/lib/lua/5.4/eco/hash
endef

define Package/lua-eco-md5/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/hash
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hash/md5.so $(1)/usr/local/lib/lua/5.4/eco/hash
endef

define Package/lua-eco-hmac/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/hash
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/hash/hmac.lua $(1)/usr/local/lib/lua/5.4/eco/hash
endef

define Package/lua-eco-socket/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/socket.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/socket.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-dns/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/dns.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/dns.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-ssl/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/ssl.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ssl.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-ubus/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/ubus.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ubus.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-http/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/http
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/http/*.lua $(1)/usr/local/lib/lua/5.4/eco/http
endef

define Package/lua-eco-mqtt/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/mqtt.lua $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-websocket/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/websocket.lua $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-termios/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/termios.so $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-netlink/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/nl/{nl,genl}.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/nl/{nl,genl}.so $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/nl/rtnl.so $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-ip/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/nl/ip.lua $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-nl80211/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/nl/nl80211.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/nl/nl80211.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-ssh/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco/internal
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/ssh.lua $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/ssh.so $(1)/usr/local/lib/lua/5.4/eco/internal
endef

define Package/lua-eco-packet/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/packet.lua $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-uci/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/uci.so $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-shared/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/shared.so $(1)/usr/local/lib/lua/5.4/eco
endef

define Package/lua-eco-net/install
	$(INSTALL_DIR) $(1)/usr/local/lib/lua/5.4/eco
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/net.lua $(1)/usr/local/lib/lua/5.4/eco
endef

$(eval $(call BuildPackage,lua-eco))
$(eval $(call BuildPackage,lua-eco-log))
$(eval $(call BuildPackage,lua-eco-base64))
$(eval $(call BuildPackage,lua-eco-sha1))
$(eval $(call BuildPackage,lua-eco-sha256))
$(eval $(call BuildPackage,lua-eco-md5))
$(eval $(call BuildPackage,lua-eco-hmac))
$(eval $(call BuildPackage,lua-eco-socket))
$(eval $(call BuildPackage,lua-eco-dns))
$(eval $(call BuildPackage,lua-eco-ssl))
$(eval $(call BuildPackage,lua-eco-ubus))
$(eval $(call BuildPackage,lua-eco-http))
$(eval $(call BuildPackage,lua-eco-mqtt))
$(eval $(call BuildPackage,lua-eco-websocket))
$(eval $(call BuildPackage,lua-eco-termios))
$(eval $(call BuildPackage,lua-eco-netlink))
$(eval $(call BuildPackage,lua-eco-ip))
$(eval $(call BuildPackage,lua-eco-nl80211))
$(eval $(call BuildPackage,lua-eco-ssh))
$(eval $(call BuildPackage,lua-eco-packet))
$(eval $(call BuildPackage,lua-eco-uci))
$(eval $(call BuildPackage,lua-eco-shared))
$(eval $(call BuildPackage,lua-eco-net))
