#
# Copyright (C) 2015 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=lua-protobuf
PKG_VERSION:=0.3.1
PKG_RELEASE:=1
PKG_MAINTAINER:=Dirk Chang <dirk@kooiot.com>
PKG_LICENSE:=MIT

PKG_INSTALL:=1

include $(INCLUDE_DIR)/package.mk

define Package/lua-protobuf
  SUBMENU:=Lua
  SECTION:=lang
  CATEGORY:=Languages
  TITLE:=lua-protobuf
  URL:=https://github.com/Kong/lua-protobuf
  DEPENDS:=+lua
endef

define Package/lua-protobuf/description
  pack binary parser/builder for Lua
endef

TARGET_CFLAGS += $(FPIC) -DUSE_GLOBALS

# add make variable overrides here
MAKE_FLAGS += -DUSE_GLOBALS

define Build/Prepare
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Configure
endef

define Build/Compile
	$(MAKE) -C $(PKG_BUILD_DIR) \
		PREFIX="$(STAGING_DIR)/usr" \
		LUA_LIBDIR="$(STAGING_DIR)/usr/lib/lua" \
		LUA_INCDIR="$(STAGING_DIR)/usr/include" \
		LUA="$(STAGING_DIR)/host/bin/lua" \
		clean
	$(MAKE) -C $(PKG_BUILD_DIR) \
		PREFIX="$(STAGING_DIR)/usr" \
		LUA_LIBDIR="$(STAGING_DIR)/usr/lib/lua" \
		LUA_INCDIR="$(STAGING_DIR)/usr/include" \
		LIB_OPTION="-shared" \
		CC="$(TARGET_CROSS)gcc" \
		CFLAGS="$(TARGET_CFLAGS) $(TARGET_CPPFLAGS)" \
		all
endef

define Build/Install
endef

define Package/lua-protobuf/install
	$(INSTALL_DIR) $(1)/usr/lib/lua/
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/pb.so $(1)/usr/lib/lua/pb.so
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/protoc.lua $(1)/usr/lib/lua/protoc.lua
endef

$(eval $(call BuildPackage,lua-protobuf))
