#!/bin/sh
# Author: Jamie Strandboge <jamie@ubuntu.com>
# Copyright (C) 2013-2015 Canonical Ltd.
#
# This script is distributed under the terms and conditions of the GNU General
# Public License, Version 3 or later. See http://www.gnu.org/copyleft/gpl.html
# for details.
set -e

rc="0"

expected_policy_groups_1604="container-management desktop display-server firewall-management locale-management mir-client network-admin network-client network-firewall network-listener network-management network-monitor network-service network-status physical-memory-access read-system-logs snapd snap-management system-monitor timeserver-management timezone-management unix-listener update-schedule-management"

for v in 16.04 ; do
    expected_groups="$expected_policy_groups_1604"
    vendor="ubuntu-core"
#    if [ "$v" = "15.10" ]; then
#        expected_groups="$expected_policy_groups_1510"
#    fi
    tmp=`ls -1 /usr/share/apparmor/easyprof/policygroups/$vendor/$v`
    for p in $expected_groups ; do
        found=""
        for i in $tmp ; do
            if [ "$p" = "$i" ]; then
                found="yes"
                continue
            fi
        done
        if [ -z "$found" ]; then
            echo "Could not find '$p'" >&2
            rc="1"
        fi
    done

    unexpected=""
    for p in $tmp ; do
        found=""
        for i in $expected_groups ; do
            if [ "$p" = "$i" ]; then
                found="yes"
                continue
            fi
        done
        if [ -z "$found" ]; then
            echo "Found unexpected '$p'" >&2
            rc="1"
        fi
    done
done

if [ "$rc" = "0" ]; then
    echo "PASS"
else
    echo "FAIL"
fi
exit "$rc"
