#!/bin/sh
# ------------------------------------------------------------------
#
#    Copyright (C) 2013-2014 Canonical Ltd.
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#
# ------------------------------------------------------------------

set -e

add_sysfs="no"
cleanup() {
    mv -f /sbin/apparmor_parser.orig /sbin/apparmor_parser
    mv -f /usr/lib/python3/dist-packages/apparmor/click.py.orig /usr/lib/python3/dist-packages/apparmor/click.py
}

uid=`id -u`
if [ "$uid" = "0" ]; then
    # divert apparmor_parser and substitute /bin/true so we don't need to load
    # policy into the kernel
    cp -f /sbin/apparmor_parser /sbin/apparmor_parser.orig
    cp -f /bin/true /sbin/apparmor_parser
    cp -f /usr/lib/python3/dist-packages/apparmor/click.py /usr/lib/python3/dist-packages/apparmor/click.py.orig
    sed -i 's/^mock_testenv = False/mock_testenv = True/' /usr/lib/python3/dist-packages/apparmor/click.py
    trap cleanup EXIT HUP INT QUIT TERM
fi

# copy the tests to another directory without PYTHONPATH so that it will use
# the system imports
cp -a ./test-clicktool.py $ADTTMP
sed -i 's/from src.apparmor import click/from apparmor import click/' $ADTTMP/test-clicktool.py
$ADTTMP/test-clicktool.py
