#!/bin/sh

SELINUX_STATE="$1"; shift

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

. /etc/init.d/functions

AUTORELABEL=
SourceIfNotEmpty /etc/selinux/config

rm -f /.autorelabel /etc/selinux/.autorelabel

if is_no "$AUTORELABEL"; then
	echo
	echo "*** Warning -- SELinux ${SELINUXTYPE-} policy relabel is required."
	echo "*** /etc/selinux/config indicates you want to manually fix labeling"
	echo "*** problems.  Dropping you to a shell; the system will reboot"
	echo "*** when you leave the shell."

	echo 0 > /selinux/enforce
	PS1="(SELinux Repair) \# #"; export PS1
	sulogin
	echo "$SELINUX_STATE" > /selinux/enforce

	exit 3 # should result to automatic reboot
else
	echo
	echo "*** Warning -- SELinux ${SELINUXTYPE-} policy relabel is required."
	echo "*** Relabeling could take a very long time, depending on file"
	echo "*** system size and speed of hard drives."

	# if /sbin/init is not labeled correctly this process is running in the
	# wrong context, so a reboot will be required after relabel
	REBOOTFLAG="$(restorecon -v /sbin/init)"
	FIXFILES=fixfiles

	echo 0 > /selinux/enforce
	$FIXFILES -F restore > /dev/null 2>&1
	echo "$SELINUX_STATE" > /selinux/enforce

	if [ -n "$REBOOTFLAG" ]; then
		exit 3 # should result to automatic reboot
	else
		echo "*** Trying to continue normal startup."
	fi
fi

