#
# Copyright (C) 2017 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:=sysrepo
PKG_VERSION:=2.2.105
PKG_RELEASE:=3

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

PKG_HASH:=cbef7df4dac537362730948a5d19c7ee700c8273e09adb161be655b9a54b810a

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

CMAKE_INSTALL:=1
PKG_BUILD_PARALLEL:=1

HOST_BUILD_DEPENDS:=libyang/host

# install host build into the workdir staging (a writable volume under the
# docker build system) - the toolchain image's /toolchain/hostpkg is read-only
HOST_BUILD_PREFIX:=$(TOPDIR)/staging_dir/hostpkg

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/host-build.mk
include $(INCLUDE_DIR)/cmake.mk

# find the host libyang installed in our writable prefix
CMAKE_HOST_FIND_ROOT_PATH:=$(HOST_BUILD_PREFIX);$(CMAKE_HOST_FIND_ROOT_PATH)

define Package/libsysrepo
  SECTION:=libs
  CATEGORY:=Libraries
  TITLE:=YANG-based data store library
  URL:=https://www.sysrepo.org/
  DEPENDS:=+libyang +libatomic +librt +libpthread
endef

define Package/sysrepo
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=YANG-based data store daemon and plugin
  URL:=https://www.sysrepo.org/
  DEPENDS:=+libsysrepo
  USERID:=netconf=663:netconf=663
endef

define Package/sysrepoctl
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=sysrepo cli tool
  URL:=https://www.sysrepo.org/
  DEPENDS:=+libsysrepo
endef

define Package/sysrepocfg
  SECTION:=utils
  CATEGORY:=Utilities
  TITLE:=sysrepo configuration tool
  URL:=https://www.sysrepo.org/
  DEPENDS:=+libsysrepo
endef

define Package/sysrepo/description
 Sysrepo is an YANG-based configuration and operational state data store for Unix/Linux applications.
endef

CMAKE_OPTIONS += \
	-DCMAKE_BUILD_TYPE:STRING=Release \
	-DENABLE_TESTS:BOOL=OFF \
	-DBUILD_EXAMPLES:BOOL=OFF \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DREPO_PATH:PATH=/etc/sysrepo \
	-DSYSREPO_GROUP:STRING=netconf \
  -DSRPD_PLUGINS_PATH:PATH=/usr/lib/sysrepo-plugind/plugins

# host sysrepoctl is used to pre-generate /etc/sysrepo at image build time
# (see netopeer2) - versions stay in sync since it is the same source tree
CMAKE_HOST_OPTIONS += \
	-DCMAKE_BUILD_TYPE:STRING=Release \
	-DENABLE_TESTS:BOOL=OFF \
	-DBUILD_EXAMPLES:BOOL=OFF \
	-DREPO_PATH:PATH=/etc/sysrepo

define Package/libsysrepo/install
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libsysrepo.so* $(1)/usr/lib/
endef

define Package/sysrepo/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/sysrepo-plugind $(1)/usr/bin/

	$(INSTALL_DIR) $(1)/etc/init.d/
	$(INSTALL_BIN) ./files/sysrepo.init $(1)/etc/init.d/sysrepo
endef

define Package/sysrepoctl/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/sysrepoctl $(1)/usr/bin/
endef

define Package/sysrepocfg/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/sysrepocfg $(1)/usr/bin/
endef

$(eval $(call BuildPackage,libsysrepo))
$(eval $(call BuildPackage,sysrepo))
$(eval $(call BuildPackage,sysrepoctl))
$(eval $(call BuildPackage,sysrepocfg))
$(eval $(call HostBuild))
