#Ref: https://github.com/archlinux/svntogit-packages/blob/packages/apparmor/trunk/PKGBUILD#L61=
abinfo "Setting MAKEFLAGS ..."
export CORE_PERL=/usr/bin/core_perl
export MAKEFLAGS+=" POD2MAN=${CORE_PERL}/pod2man"
export MAKEFLAGS+=" POD2HTML=${CORE_PERL}/pod2html"
export MAKEFLAGS+=" PODCHECKER=${CORE_PERL}/podchecker"
export MAKEFLAGS+=" PROVE=${CORE_PERL}/prove"

abinfo "Re-generating Autotools scripts ..."
cd "${SRCDIR}"/libraries/libapparmor
autoreconf -fvis

abinfo "Configuring libapparmor ..."
./configure \
	--prefix=/usr \
	--sbindir=/usr/bin \
	--with-perl \
	--with-python \
	--with-ruby

abinfo "Building libapparmor ..."
make

for component in binutils parser profiles utils changehat/pam_apparmor changehat/mod_apparmor utils/vim; do
	abinfo "Building apparmor-${component} ..."
	make -C "${SRCDIR}/${component}"
done

abinfo "Installing libapparmor ..."
make install \
	-C "${SRCDIR}/libraries/libapparmor" \
	DESTDIR="${PKGDIR}"

abinfo "Installing changehat/pam_apparmor ..."
make install \
	-C "${SRCDIR}/changehat/pam_apparmor" \
	DESTDIR="${PKGDIR}" \
	SECDIR="${PKGDIR}/usr/lib/security"

abinfo "Installing apparmor-binutils ..."
make install \
	-C "${SRCDIR}/binutils" \
	DESTDIR="${PKGDIR}" \
	SBINDIR="${PKGDIR}/usr/bin" \
	USR_SBINDIR="${PKGDIR}/usr/bin"

abinfo "Installing apparmor-parser ..."
make install \
	-C "${SRCDIR}/parser" \
	DESTDIR="${PKGDIR}" \
	SBINDIR="${PKGDIR}/usr/bin" \
	USR_SBINDIR="${PKGDIR}/usr/bin" \
	APPARMOR_BIN_PREFIX="${PKGDIR}/usr/lib/apparmor"

abinfo "Installing apparmor-parser's systemd units ..."
make install-systemd \
	-C "${SRCDIR}/parser" \
	DESTDIR="${PKGDIR}" \
	SYSTEMD_UNIT_DIR="${PKGDIR}/usr/lib/systemd/system" \
	APPARMOR_BIN_PREFIX="${PKGDIR}/usr/lib/apparmor" \
	USR_SBINDIR="${PKGDIR}/usr/bin"

abinfo "Installing apparmor-profiles ..."
make install \
	-C "${SRCDIR}/profiles" \
	DESTDIR="${PKGDIR}"

abinfo "Installing apparmor-utils"
make install \
	-C "${SRCDIR}/utils" \
	DESTDIR="${PKGDIR}" \
	USR_SBINDIR="${PKGDIR}/usr/bin" \
	BINDIR="${PKGDIR}/usr/bin" \
	VIM_INSTALL_PATH="${PKGDIR}/usr/share/vim/vimfiles/syntax"

# Ref: https://gitlab.com/apparmor/apparmor/issues/34
abinfo "Fixing perl binding permissions ..."
find "${PKGDIR}/usr/lib/perl5/" -type f -iname "*.so" -exec chmod -v 755 {} \;

# Ref: https://gitlab.com/apparmor/apparmor/issues/40
#abinfo "Removing empty core_perl directory ..."
#rm -Rv "${PKGDIR}/usr/lib/perl5/*/core_perl"

# Ref: https://gitlab.com/apparmor/apparmor/issues/35
abinfo "Moving Ruby binding from site_ruby to vendor_ruby ..."
mv -v "${PKGDIR}/usr/lib/ruby/site_ruby/" \
	"${PKGDIR}/usr/lib/ruby/vendor_ruby"
