#!/bin/sh

. /warewulf/config

if test -f "/etc/sysconfig/selinux"; then
    . /etc/sysconfig/selinux
else
    echo "Skipping SELinux configuration: Host config not found: /etc/sysconfig/selinux"
    exit
fi

if test "$SELINUX" == "disabled"; then
    echo "Skipping SELinux setup per /etc/sysconfig/selinux"
    exit
fi

if grep -q "selinux=0" /proc/cmdline; then
    echo "Skipping SELinux setup per kernel command line"
    exit
fi

if [ $(findmnt / --noheadings --output SOURCE) == 'rootfs' ]; then
    echo "Skipping SELinux configuration: rootfs does not support SELinux contexts"
    echo
    echo "WARNING: SELinux prep is being skipped, but SELinux is enabled on host! This may"
    echo "WARNING: cause the system to not work properly. Try setting 'Root=tmpfs'"
    sleep 5
    exit
fi

echo "Setting up SELinux"
/sbin/load_policy -i
/sbin/restorecon -e /sys -r /
