# Template file for 'apparmor'
pkgname=apparmor
reverts="4.0.2_1"
version=3.1.7
revision=5
build_wrksrc=libraries/libapparmor
build_style=gnu-configure
conf_files="/etc/apparmor.d/local/* /etc/apparmor/*"
make_dirs="/etc/apparmor.d/disable 0755 root root"
configure_args="$(vopt_with python) $(vopt_with perl)"
hostmakedepends="bison flex gettext python3 which python3-setuptools pkg-config
 perl $(vopt_if python swig)"
makedepends="$(vopt_if python python3-devel)"
depends="runit-void-apparmor libapparmor-${version}_${revision} python3-notify2
 python3-psutil python3-dbus iproute2"
checkdepends="dejagnu"
short_desc="Mandatory access control to restrict programs"
maintainer="Orphaned <orphan@voidlinux.org>"
license="GPL-2.0-only"
homepage="https://gitlab.com/apparmor/apparmor"
changelog="https://gitlab.com/apparmor/apparmor/-/wikis/Release_Notes_${version}"
distfiles="https://launchpad.net/apparmor/${version%.*}/${version}/+download/apparmor-${version}.tar.gz"
checksum=c6c161d6dbd99c2f10758ff347cbc6848223c7381f311de62522f22b0a16de64
replaces="apparmor-vim>=0"

# /usr/lib/perl5/core_perl/CORE/perl.h:3360:22: error: unknown type name 'off64_t';
CFLAGS="-D_LARGEFILE64_SOURCE"
CXXFLAGS="-D_LARGEFILE64_SOURCE"

build_options="python perl"

case "$XBPS_TARGET_MACHINE" in
	x86_64*|aarch64*|i686*)
		build_options_default+=" python"
	;;
esac

if [ -z "$CROSS_BUILD" ]; then
	build_options_default+=" perl"
fi

pre_build() {
	# Replace release profiles with our own
	cd ${wrksrc}
	cp ${FILESDIR}/profiles/* profiles/apparmor.d/
}

post_build() {
	cd ${wrksrc}

	make ${makejobs} -C binutils
	make ${makejobs} -C utils
	make ${makejobs} -C parser
	make ${makejobs} -C profiles
}

post_install() {
	cd ${wrksrc}
	commonflags="DESTDIR=\"${DESTDIR}\" SBINDIR=\"${DESTDIR}/usr/bin\" USR_SBINDIR=\"${DESTDIR}/usr/bin\""
	make $commonflags install -C binutils
	make $commonflags \
		BINDIR="${DESTDIR}/usr/bin" \
		VIM_INSTALL_PATH="${DESTDIR}/usr/share/vim/vimfiles/syntax" \
		install -C utils
	make $commonflags \
		APPARMOR_BIN_PREFIX="${DESTDIR}/usr/lib/apparmor" \
		install -C parser
	make DESTDIR="${DESTDIR}" install -C profiles

	# requires perl bindings not generated when cross-compiling
	if [ -z "$build_option_perl" ]; then
		rm -f ${DESTDIR}/usr/bin/aa-notify
	fi

	# we installed a custom conflicting profile
	rm ${DESTDIR}/etc/apparmor.d/{,local/}php-fpm

	vinstall utils/aa-notify.desktop 644 usr/share/applications
	vdoc "${FILESDIR}/README.voidlinux"
}

libapparmor_package() {
	short_desc+=" - Library"
	license="LGPL-2.1-only"
	pkg_install() {
		vmove "usr/lib/libapparmor.so*"
		if [ "$build_option_perl" ]; then
			vmove usr/lib/perl5
		fi
		if [ "$build_option_python" ]; then
			vmove "${py3_sitelib}/LibAppArmor*"
		fi
		vmove usr/share/man/man2
		vmove usr/share/man/man3
	}
}

libapparmor-devel_package() {
	short_desc+=" - Library development files"
	license="LGPL-2.1-only"
	depends="lib${sourcepkg}-${version}_${revision}"
	pkg_install() {
		vmove usr/include
		vmove "usr/lib/*.a"
		vmove "usr/lib/*.so"
		vmove usr/lib/pkgconfig
	}
}
