#!/bin/sh
set -e

test -d "$CRUFT_ROOT/etc/apparmor.d" || exit 0

test -d "$CRUFT_ROOT/etc/apparmor.d/local" && echo /etc/apparmor.d/local

find "$CRUFT_ROOT/etc/apparmor.d/" -maxdepth 1 -type f -printf "%f\n" | while read -r file
do
	match=0
	if [ -e "$CRUFT_ROOT/etc/apparmor.d/cache/$file" ]
	then
		match=1
		echo "@/etc/apparmor.d/$file"
		echo "/etc/apparmor.d/cache/$file"
	fi
	if [ -e "$CRUFT_ROOT/etc/apparmor.d/disable/$file" ]
	then
		if [ $match = 0 ]
		then
			match=1
			echo "@/etc/apparmor.d/$file"
		fi
		echo "/etc/apparmor.d/disable/$file"
	fi
	if [ -e "$CRUFT_ROOT/etc/apparmor.d/local/$file" ]
	then
		if [ $match = 0 ]
		then
			echo "@/etc/apparmor.d/$file"
		fi
		echo "/etc/apparmor.d/local/$file"
	fi
done


test -d "$CRUFT_ROOT/etc/apparmor.d/abstractions" || exit 0

if [ -d "$CRUFT_ROOT/etc/apparmor.d/local/abstractions" ]
then
	echo apparmor
	echo /etc/apparmor.d/local/abstractions
fi

find "$CRUFT_ROOT/etc/apparmor.d/abstractions" -maxdepth 1 -type f -printf "%f\n" |
while read -r file
do
	if [ -e "$CRUFT_ROOT/etc/apparmor.d/local/abstractions/$file" ]
	then
		echo "@/etc/apparmor.d/abstractions/$file"
		echo "/etc/apparmor.d/local/abstractions/$file"
	fi
done

exit 0
