#!/bin/sh

set -eu


if [ ! -f /etc/selinux/config ]; then
    return
fi

if ! command -v sefcontext_compile >/dev/null 2>&1; then
    echo "Pre-compiled SELinux file contexts not built: sefcontext_compile not found (missing package selinux-utils)"
    return
fi

. /etc/selinux/config

if [ -z ${SELINUXTYPE+x} ]; then
    echo "Pre-compiled SELinux file contexts not built: SELINUXTYPE not defined in /etc/selinux/config"
    return
fi

if [ ! -d "/etc/selinux/$SELINUXTYPE/contexts/files" ]; then
    echo "Pre-compiled SELinux file contexts not build: configuration for policy $SELINUXTYPE not found"
    return
fi

if [ -f "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts" ]; then
    sefcontext_compile "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts" || true
fi

if [ -f "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts.homedirs" ]; then
    sefcontext_compile "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts.homedirs" || true
fi

if [ -f "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts.local" ]; then
    sefcontext_compile "/etc/selinux/$SELINUXTYPE/contexts/files/file_contexts.local" || true
fi
