#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=audit-userspace
PKG_VERSION:=3.1.5
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/linux-audit/$(PKG_NAME).git
PKG_SOURCE_VERSION:=v$(PKG_VERSION)
PKG_MIRROR_HASH:=60f479476d4f1b0beadbe3e516ea67490d38ca9b01db53e56f52b1731340d5bb

PKG_MAINTAINER:=Thomas Petazzoni <thomas.petazzoni@bootlin.com>
PKG_LICENSE:=GPL-2.0-or-later LGPL-2.1-or-later
PKG_LICENSE_FILES:=COPYING COPYING.LIB
PKG_CPE_ID:=cpe:/a:linux_audit_project:linux_audit

PKG_CONFIG_DEPENDS:=CONFIG_KERNEL_IO_URING
PKG_FIXUP:=autoreconf

PKG_BUILD_FLAGS:=no-mips16
PKG_INSTALL:=1

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

define Package/audit/Default
  TITLE:=Audit
  URL:=https://github.com/linux-audit/
endef

define Package/audit/Default/description
  The audit package contains the user space utilities for
  storing and searching the audit records generated by
  the audit subsystem in the kernel.
endef

define Package/libaudit
$(call Package/audit/Default)
  SECTION:=libs
  CATEGORY:=Libraries
  TITLE+= (libaudit)
endef

define Package/libaudit/description
$(call Package/audit/Default/description)
  This package contains the audit shared library.
endef

define Package/libauparse
$(call Package/audit/Default)
  SECTION:=libs
  CATEGORY:=Libraries
  TITLE+= (libauparse)
  DEPENDS:= +libaudit
endef

define Package/libauparse/description
$(call Package/audit/Default/description)
  This package contains the audit parsing shared library.
endef

define Package/audit-utils
$(call Package/audit/Default)
  SECTION:=admin
  CATEGORY:=Administration
  TITLE+= (utilities)
  DEPENDS:= +libaudit +libauparse
endef

define Package/audit-utils/description
$(call Package/audit/Default/description)
  This package contains the audit utilities.
endef

define Package/auditd
$(call Package/audit/Default)
  SECTION:=admin
  CATEGORY:=Administration
  TITLE+= (daemon)
  DEPENDS:= +libaudit +libauparse +audit-utils +libev
endef

define Package/auditd/description
$(call Package/audit/Default/description)
  This package contains the audit daemon.
endef

CONFIGURE_VARS += \
	LDFLAGS_FOR_BUILD="$(HOST_LDFLAGS)" \
	CPPFLAGS_FOR_BUILD="$(HOST_CPPFLAGS)" \
	CFLAGS_FOR_BUILD="$(HOST_CFLAGS)" \
	CC_FOR_BUILD="$(HOSTCC)"

CONFIGURE_ARGS += \
	--with-debug \
	--disable-systemd \
	--disable-zos-remote \
	--disable-gssapi-krb5 \
	--without-libcap-ng \
	--without-python \
	--without-python3 \
	--without-golang

ifeq ($(ARCH),aarch64)
CONFIGURE_ARGS += --with-aarch64
else ifeq ($(ARCH),arm)
CONFIGURE_ARGS += --with-arm
endif

HOST_CONFIGURE_ARGS += \
	--disable-systemd \
	--disable-zos-remote \
	--disable-gssapi-krb5 \
	--without-libcap-ng \
	--without-python \
	--without-python3 \
	--without-golang

define Host/Install
	+$(HOST_MAKE_VARS) $(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR)/lib $(HOST_MAKE_FLAGS) install
	+$(HOST_MAKE_VARS) $(MAKE) $(HOST_JOBS) -C $(HOST_BUILD_DIR)/init.d $(HOST_MAKE_FLAGS) install
endef

# We can't use the default, as the default passes $(MAKE_ARGS), which
# overrides CC, CFLAGS, etc. and defeats the *_FOR_BUILD definitions
# passed in CONFIGURE_VARS
define Build/Compile
	$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR)/$(MAKE_PATH)
endef

define Build/Install
	$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR)/lib $(MAKE_INSTALL_FLAGS) install
	$(MAKE) $(PKG_JOBS) -C $(PKG_BUILD_DIR)/init.d $(MAKE_INSTALL_FLAGS) install
	$(call Build/Install/Default,install)
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/usr/include
	$(CP) $(PKG_INSTALL_DIR)/usr/include/* $(1)/usr/include/
	$(INSTALL_DIR) $(1)/usr/lib/pkgconfig
	$(INSTALL_DATA) $(PKG_INSTALL_DIR)/usr/lib/pkgconfig/*.pc $(1)/usr/lib/pkgconfig/
	$(INSTALL_DIR) $(1)/usr/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/* $(1)/usr/lib/
endef

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

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

define Package/audit-utils/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(CP) $(PKG_INSTALL_DIR)/usr/bin/* $(1)/usr/bin/
	$(INSTALL_DIR) $(1)/usr/sbin
	$(CP) \
		$(PKG_INSTALL_DIR)/usr/sbin/{audisp-remote,audisp-syslog,auditctl,augenrules,aureport,ausearch,autrace} \
		$(1)/usr/sbin/
endef

define Package/auditd/install
	$(INSTALL_DIR) $(1)/etc/audit
	$(CP) $(PKG_INSTALL_DIR)/etc/audit/* $(1)/etc/audit/
	# af_unix plugin is not installed. Remove it's .conf.
	if [[ -f $(1)/etc/audit/plugins.d/af_unix.conf ]] ; then rm $(1)/etc/audit/plugins.d/af_unix.conf ; fi
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/audit.init $(1)/etc/init.d/audit
	$(INSTALL_DIR) $(1)/usr/sbin
	$(CP) $(PKG_INSTALL_DIR)/usr/sbin/auditd $(1)/usr/sbin/
endef

$(eval $(call HostBuild))
$(eval $(call BuildPackage,libaudit))
$(eval $(call BuildPackage,libauparse))
$(eval $(call BuildPackage,audit-utils))
$(eval $(call BuildPackage,auditd))
