#!/bin/sh
#
# workaroudn for LP: #1460152
#

set -e

. /lib/apparmor/functions

# check each profile if the cache is out of sync (we can not use
# the mtime here because the apparmor seems to be doing the
# strictly great check on the mtime)
for p in $(find "$PROFILES" -maxdepth 1 -type f); do
    profile_md5=$(md5sum "$p")
    cached_p="$(echo $p | sed "s#$PROFILES#$PROFILES_CACHE#")"
    if [ ! -f "$cached_p" ]; then
        continue
    fi
    profile_md5_marker="$cached_p.profile-md5sum"
    cached_md5="$(cat "$profile_md5_marker" || true)"
    if [ "$profile_md5" != "$cached_md5" ]; then
        # remove, re-generate, update-md5 of cache
        rm -f "$cached_p"
        "$PARSER" --write-cache --replace "$p"
        echo "$profile_md5" > "$profile_md5_marker"
    fi
done
