#
# Copyright (C) 2023 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk

PKG_NAME:=obuspc
PKG_VERSION:=1.0.1.8

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/obuspa-test-controller.git
PKG_SOURCE_VERSION:=1cf32fa4cb5c07906b1e061a394cf0413a6ad750
PKG_MAINTAINER:=Vivek Dutta <vivek.dutta@iopsys.eu>
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

PKG_FIXUP:=autoreconf

PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE

include $(INCLUDE_DIR)/package.mk

define Package/obuspc
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  TITLE:=USP Controller
  DEPENDS:=+libopenssl +libuci +libblobmsg-json +libcurl +libsqlite3 +libubox +libubus +libmosquitto-ssl +libwebsockets-openssl
endef

define Package/obuspc/description
 OB-USP-Controller is a local usp controller based on User Services Platform (USP).
endef

TARGET_CFLAGS += \
	-D_GNU_SOURCE \
	-Wall \
	-Werror

CONFIGURE_ARGS += \
        --localstatedir="/tmp/" \
	--enable-stomp \
	--enable-coap \
	--enable-mqtt \
	--enable-websockets

ifeq ($(LOCAL_DEV),1)
define Build/Prepare
        $(CP) -rf ~/git/obuspa-test-controller/* $(PKG_BUILD_DIR)/
        $(Build/Patch)
endef
endif

define Package/obuspc/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/obuspc $(1)/usr/sbin/uspc
	$(INSTALL_BIN) ./files/etc/init.d/uspc $(1)/etc/init.d/
	$(INSTALL_BIN) ./files/etc/uci-defaults/99-fix-agent-endpoint $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/100-add-mosquitto-listener $(1)/etc/uci-defaults/
	$(INSTALL_DATA) ./files/etc/config/uspc $(1)/etc/config/
endef

$(eval $(call BuildPackage,obuspc))
