include $(TOPDIR)/rules.mk

PKG_NAME:=cronosagent
PKG_VERSION:=$(shell grep -o -E '[0-9]+' source/Version.h)
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)
PKG_BUILD_DEPENDS:=+libstdcpp +libopenssl
X_SOURCE_DIR:=$(CURDIR)/source

include $(INCLUDE_DIR)/package.mk

define Package/cronosagent
SECTION:=net
CATEGORY:=Network
TITLE:=CronosAgent
URL:=https://brandmeister.network/
DEPENDS:=
endef

define Package/cronosagent/description
	CronosAgent for BrandMeister DMR Master Server
	Allows to connect Radio Activity KAIROS Repetaters to BrandMeister DMR Master Server
	Radio Activity KAIROS should have built-in feature of External Server Access
	which is available on firmware customization 008 (Radio Amateur community)
endef

define Build/Prepare
	echo $(X_SOURCE_DIR)
	mkdir -p $(PKG_BUILD_DIR)
	$(CP) -R $(X_SOURCE_DIR)/* $(PKG_BUILD_DIR)/
endef

#define Build/Compile
#	$(call Build/Compile/Default, -C $(PKG_BUILD_DIR) )
#endef

define Package/cronosagent/conffiles
	/etc/config/cronosagent
endef

define Package/cronosagent/postinst
	#!/bin/sh
	/etc/init.d/cronosagent enable
endef

define Package/cronosagent/prerm
	#!/bin/sh
	/etc/init.d/cronosagent disable
endef

define Package/cronosagent/install
	$(INSTALL_DIR) $(1)/opt/CronosAgent
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/usr/lib/lua/luci/controller
	$(INSTALL_DIR) $(1)/usr/lib/lua/luci/model/cbi/cronosagent
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/cronosagent $(1)/opt/CronosAgent/
	$(INSTALL_BIN) $(X_SOURCE_DIR)/cronosagent.sh $(1)/opt/CronosAgent/
	$(INSTALL_BIN) $(X_SOURCE_DIR)/OpenWRT/init.d/cronosagent $(1)/etc/init.d/
	$(INSTALL_DATA) $(X_SOURCE_DIR)/OpenWRT/config/cronosagent $(1)/etc/config/
	$(INSTALL_DATA) $(X_SOURCE_DIR)/OpenWRT/controller/cronosagent.lua $(1)/usr/lib/lua/luci/controller/
	$(INSTALL_DATA) $(X_SOURCE_DIR)/OpenWRT/model/cbi/cronosagent/configure.lua $(1)/usr/lib/lua/luci/model/cbi/cronosagent/
endef

$(eval $(call BuildPackage,cronosagent))