#!/bin/bash
#
# kate: tab-indents off; tab-width 4; indent-width 4;
#


# Fix the selinux context debacle of the AWS Inspector agent
semanage -i <<-EOD
fcontext -a -t bin_t /etc/rc\.d/init\.d/awsagent
fcontext -a -t bin_t /etc/init\.d/awsagent
EOD

# Make sure it's applied if the files are already present
[ -f /etc/rc.d/init.d/awsagent ] && restorecon /etc/rc.d/init.d/awsagent
[ -f /etc/init.d/awsagent      ] && restorecon /etc/init.d/awsagent

# And restart it if it's already running with the wrong context
if [ -f /var/run/awsagent.pid ]; then
    AWS_AGENT_PID=$(grep -E '^[0-9]+$' /var/run/awsagent.pid)
    if [ -n "${AWS_AGENT_PID}" ] && grep -q ':initrc_t:' /proc/${AWS_AGENT_PID}/attr/current; then
        systemctl --no-block try-restart awsagent.service
   fi
fi

