#
# Copyright (c) 2021 Genexis Netherlands B.V. All rights reserved.
# This Software and its content are protected by the Dutch Copyright Act
# ('Auteurswet'). All and any copying and distribution of the software
# and its content without authorization by Genexis Netherlands B.V. is
# prohibited. The prohibition includes every form of reproduction and
# distribution.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=dmcli
PKG_LICENSE:=PROPRIETARY GENEXIS
PKG_VERSION:=1.9.4

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/gnx/dmcli.git
PKG_SOURCE_VERSION:=2139e81d0faca09fe1832c503297ef75ea76d6eb
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.xz
PKG_MIRROR_HASH:=skip

PKG_BUILD_PARALLEL:=1

include $(INCLUDE_DIR)/package.mk

define Package/dmcli
  SECTION:=tools
  CATEGORY:=Genexis
  TITLE:=DMCLI (datamodel-based CLI)
  URL:=http://genexis.eu
  DEPENDS:=+usp-js +DMCLI_REMOTE_CONNECTION:mosquitto-auth-plugin +shadow-utils +@BUSYBOX_CONFIG_ADDUSER
endef

define Package/dmcli/description
  CLI to view and configure datamodels of CPE
endef

define Package/dmcli/conffiles
/etc/dmcli/dmcli.conf
endef

define Package/dmcli/config
	source "$(SOURCE)/Config.in"
endef

define Package/dmcli/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/dmcli $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/usr/lib/dmcli
	$(CP) $(PKG_BUILD_DIR)/common $(1)/usr/lib/dmcli/
	mv $(1)/usr/lib/dmcli/common/os_qjs.js $(1)/usr/lib/dmcli/common/os.js
	rm $(1)/usr/lib/dmcli/common/os_node.js
	$(CP) $(PKG_BUILD_DIR)/core $(1)/usr/lib/dmcli/
	$(CP) $(PKG_BUILD_DIR)/cli $(1)/usr/lib/dmcli/
	$(CP) $(PKG_BUILD_DIR)/data $(1)/usr/lib/dmcli/
	$(CP) $(PKG_BUILD_DIR)/plugins $(1)/usr/lib/dmcli/

	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DATA) ./files/etc/uci-defaults/36-dmcli $(1)/etc/uci-defaults/
ifeq ($(CONFIG_DMCLI_REMOTE_CONNECTION),y)
	$(INSTALL_DATA) ./files/etc/uci-defaults/36-dmcli-remote $(1)/etc/uci-defaults/
else
	$(INSTALL_DATA) ./files/etc/uci-defaults/36-dmcli-remote-remove $(1)/etc/uci-defaults/
endif

	$(INSTALL_DIR) $(1)/etc/dmcli
	$(CP) ./files/etc/dmcli/dmcli.acl $(1)/etc/dmcli/
	$(CP) ./files/etc/dmcli/dmcli.conf $(1)/etc/dmcli/

	$(INSTALL_DIR) $(1)/etc/users/roles/
	$(INSTALL_DATA) ./files/etc/users/roles/operator.json $(1)/etc/users/roles/
endef

$(eval $(call BuildPackage,dmcli))
