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

# Warning: NETCONF server authenticates system users, make sure a root password is set.

include $(TOPDIR)/rules.mk

PKG_NAME:=netopeer2
PKG_VERSION:=2.1.71
PKG_RELEASE:=6

PKG_SOURCE_VERSION:=2.1.71
PKG_SOURCE:=$(PKG_NAME)-$(PKG_SOURCE_VERSION).tar.gz
PKG_SOURCE_URL:=https://codeload.github.com/CESNET/netopeer2/tar.gz/v$(PKG_SOURCE_VERSION)?

PKG_HASH:=eb822f48e461caedf86a92bbc20b456f844d6f04ac40caf236874d78b14ae15c

PKG_MAINTAINER:=Jakov Smolic <jakov.smolic@sartura.hr>
PKG_LICENSE:=BSD-3-Clause
PKG_LICENSE_FILES:=LICENSE
PKG_CPE_ID:=cpe:/a:cesnet:netopeer2

CMAKE_INSTALL:=1
PKG_BUILD_PARALLEL:=1

# host sysrepoctl pre-generates /etc/sysrepo at packaging time
PKG_BUILD_DEPENDS:=sysrepo/host

NETOPEER2_ENABLE_URL:=OFF

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

define Package/netopeer2-server
  SECTION:=net
  CATEGORY:=Network
  TITLE:=NETCONF server
  URL:=https://github.com/CESNET/netopeer2
  DEPENDS:=+libpthread +libyang +libnetconf2 +libssh +libsysrepo +sysrepo +sysrepoctl +sysrepocfg +openssl-util $(if $(filter ON,$(NETOPEER2_ENABLE_URL)),+libcurl)
  # Own uid, not sysrepo-plugind's "netconf" (declared by the sysrepo package):
  # this daemon is network-facing, so it must not share plugind's ubus access to
  # the root-privileged "api" object. Shared access to the sysrepo repository
  # comes from group netconf instead - see netopeer2-server.init.
  USERID:=netopeer2=664:netopeer2=664
  MENU:=1
  FATTRS:=/usr/sbin/netopeer2-server::::cap_net_bind_service=ep
endef

define Package/netopeer2-cli
  SECTION:=net
  CATEGORY:=Network
  TITLE:=Netopeer2 cli tool
  URL:=https://github.com/CESNET/netopeer2
  DEPENDS:=+libpthread +libyang +libnetconf2 +libssh +libopenssl
endef

define Package/netopeer2/description
 Netopeer2 is a set of tools implementing network configuration tools based on the NETCONF
 Protocol. This is the second generation of the toolset, originally available as the Netopeer
 project. Netopeer2 is based on the new generation of the NETCONF and YANG libraries -
 libyang and libnetconf2. The Netopeer server uses sysrepo as a NETCONF datastore implementation.
endef

CMAKE_OPTIONS += \
	-DCMAKE_INSTALL_PREFIX:PATH=/usr \
	-DCMAKE_BUILD_TYPE:STRING=Release \
	-DENABLE_TESTS:BOOL=OFF \
	-DBUILD_CLI:BOOL=ON \
	-DENABLE_URL:BOOL=$(NETOPEER2_ENABLE_URL) \
	-DGENERATE_HOSTKEY:BOOL=OFF \
	-DINSTALL_MODULES:BOOL=OFF \
	-DMERGE_LISTEN_CONFIG:BOOL=OFF \
	-DPIDFILE_PREFIX:PATH=/var/run/netconf

# the docker build system drops */host dependencies from the dependency
# graph (package-metadata.pl, INSIDE_DOCKER) because the toolchain image
# ships the standard host tools prebuilt - sysrepoctl is not among them,
# so build the host tool chain explicitly when it is missing
define Build/Prepare
	$(call Build/Prepare/Default)
	+[ -x "$(STAGING_DIR_HOSTPKG)/bin/sysrepoctl" ] || ( \
		$(MAKE) -C $(TOPDIR)/package/libs/pcre2 host-compile && \
		$(MAKE) -C $(TOPDIR)/package/libs/libyang host-compile && \
		$(MAKE) -C $(TOPDIR)/package/network/services/sysrepo host-compile \
	)
endef

define Package/netopeer2-server/install
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/sbin/netopeer2-server $(1)/usr/sbin/

	$(INSTALL_DIR) $(1)/etc/netopeer2/modules
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/share/yang/modules/netopeer2/*.yang $(1)/etc/netopeer2/modules/

	# effective modules.list: the ietf-netconf :url feature follows
	# NETOPEER2_ENABLE_URL, same switch that drives -DENABLE_URL and the
	# libcurl dependency - keeps the YANG feature, server code and dep in sync
	$(if $(filter ON,$(NETOPEER2_ENABLE_URL)), \
		sed '/^ietf-netconf@/s/$$/ -e url/' ./files/modules.list > $(PKG_BUILD_DIR)/modules.list, \
		cp ./files/modules.list $(PKG_BUILD_DIR)/modules.list)
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/modules.list $(1)/etc/netopeer2/modules.list

	# pre-generate the sysrepo repository so first boot skips the
	# expensive on-device module installation (see gen-sysrepo-repo.sh).
	# generate into PKG_BUILD_DIR first: if generation fails under a
	# partial build (make -i), the cp below fails with "cannot stat",
	# which the build-log error detection recognizes - a failure must
	# not silently ship an ipk without the repository
	sh ./files/gen-sysrepo-repo.sh \
		$(STAGING_DIR_HOSTPKG)/bin/sysrepoctl \
		$(PKG_BUILD_DIR)/.sysrepo-repo \
		$(1)/etc/netopeer2/modules \
		$(PKG_BUILD_DIR)/modules.list
	$(CP) $(PKG_BUILD_DIR)/.sysrepo-repo $(1)/etc/sysrepo

	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/netopeer2-server-setup.default $(1)/etc/uci-defaults/97_netopeer2-server
	$(INSTALL_BIN) ./files/netopeer2-server-merge-hostkey.default $(1)/etc/uci-defaults/98_netopeer2-server
	$(INSTALL_BIN) ./files/netopeer2-server-merge-config.default $(1)/etc/uci-defaults/99_netopeer2-server

	$(INSTALL_DIR) $(1)/etc/init.d/
	$(INSTALL_BIN) ./files/netopeer2-server.init $(1)/etc/init.d/netopeer2-server

	$(INSTALL_DIR) $(1)/usr/share/acl.d
	$(INSTALL_DATA) ./files/netopeer2_acl.json $(1)/usr/share/acl.d/
endef

define Package/netopeer2-cli/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/netopeer2-cli $(1)/usr/bin/
endef

$(eval $(call BuildPackage,netopeer2-server))
$(eval $(call BuildPackage,netopeer2-cli))
