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

include $(TOPDIR)/rules.mk

PKG_NAME:=usermngr
PKG_VERSION:=1.4.7

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/usermngr.git
PKG_SOURCE_VERSION:=62390ff8d9e1c80babf3621f8b374dcd2078ff6f
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

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

include $(INCLUDE_DIR)/package.mk
include $(TOPDIR)/feeds/iopsys/bbfdm/bbfdm.mk

MAKE_PATH:=src

define Package/usermngr
  SECTION:=utils
  CATEGORY:=Utilities
  SUBMENU:=TRx69
  DEPENDS:=+shadow-utils +libopenssl +libuci +libubox +ubus
  DEPENDS+=+libbbfdm-api +libbbfdm-ubus +bbfdmd
  DEPENDS+=+@BUSYBOX_CONFIG_ADDUSER +@BUSYBOX_CONFIG_DELUSER +@BUSYBOX_CONFIG_ADDGROUP +@BUSYBOX_CONFIG_DELGROUP +shadow-usermod
  DEPENDS+=+@BUSYBOX_CONFIG_CMP
  DEPENDS+=+@USERMNGR_SECURITY_HARDENING:SHADOW_UTILS_USE_PAM
  DEPENDS+=+@USERMNGR_SECURITY_HARDENING:BUSYBOX_CONFIG_PAM
  DEPENDS+=+USERMNGR_SECURITY_HARDENING:linux-pam
  DEPENDS+=+USERMNGR_SECURITY_HARDENING:passwdqc
  DEPENDS+=+USERMNGR_ENABLE_VENDOR_EXT:shadow-chage
  TITLE:=Package to add Device.Users. datamodel support
endef

define Package/usermngr/description
  Package to add Device.Users. datamodel support
endef

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

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

ifeq ($(CONFIG_USERMNGR_SECURITY_HARDENING),y)
MAKE_FLAGS += USERMNGR_SECURITY_HARDENING=y
endif

ifeq ($(CONFIG_USERMNGR_ENABLE_VENDOR_EXT),y)
MAKE_FLAGS += USERMNGR_ENABLE_VENDOR_EXT=y
endif

ifeq ($(CONFIG_USERMNGR_VENDOR_PREFIX),"")
VENDOR_PREFIX = $(CONFIG_BBF_VENDOR_PREFIX)
else
VENDOR_PREFIX = $(CONFIG_USERMNGR_VENDOR_PREFIX)
endif

TARGET_CFLAGS += -DBBF_VENDOR_PREFIX=\\\"$(VENDOR_PREFIX)\\\"

define Package/usermngr/install
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_DIR) $(1)/etc/users/roles
	$(INSTALL_DIR) $(1)/etc/users/schema
	$(INSTALL_DIR) $(1)/usr/sbin
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-sync-shells $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-sync-roles $(1)/etc/uci-defaults/
ifeq ($(CONFIG_USERMNGR_SECURITY_HARDENING),y)
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-security-hardening $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-set-ssh-pam $(1)/etc/uci-defaults/
else
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-disabled-security $(1)/etc/uci-defaults/
	$(INSTALL_BIN) ./files/etc/uci-defaults/91-unset-ssh-pam $(1)/etc/uci-defaults/
endif
	$(INSTALL_BIN) ./files/etc/init.d/users $(1)/etc/init.d/users
	$(INSTALL_BIN) ./files/etc/config/users $(1)/etc/config/users
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/src/usermngr $(1)/usr/sbin/usermngr
	$(CP) ./acl.schema.json $(1)/etc/users/schema/acl.schema.json
	$(BBFDM_REGISTER_SERVICES) ./bbfdm_service.json $(1) $(PKG_NAME)
endef

$(eval $(call BuildPackage,usermngr))
