#!/bin/sh
### BEGIN INIT INFO
# Provides:          restorecond
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the Daemon used to maintain path file context
# Description:       uses inotify to look for creation of new files
#                    listed in the /etc/selinux/restorecond.conf file,
#                    and restores the correct security context.
### END INIT INFO

# Return values according to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running

PATH=/sbin:/bin:/usr/bin:/usr/sbin

# Check for daemon presence
test -x /usr/sbin/restorecond  || exit 0

# Test to see if SELinux is enabled
[ -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled || exit 0

# Check that we are root ... so non-root users stop here
if [ $(id -u) != 0 ] && [ "$1" != status ]; then
	exit 4
fi


# Get lsb functions
. /lib/lsb/init-functions
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Test to see if we are configured
if [ ! -f  /etc/selinux/restorecond.conf  ] && [ "$1" != status ]; then
    log_failure_msg "There is no configuration file for restorecond."
    log_failure_msg "Please create /etc/selinux/restorecond.conf."
    exit 6
fi

RETVAL=0

# See how we were called.
case "$1" in
    start)
        log_daemon_msg "Starting file context maintaining daemon" "restorecond"
        unset LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE
        start-stop-daemon --start --quiet  --oknodo --pidfile /var/run/restorecond.pid --exec /usr/sbin/restorecond
        RETVAL=$?
        log_end_msg $RETVAL
        ;;
    stop)
        log_daemon_msg "Stopping  file context maintaining daemon" "restorecond"
        unset LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE
        start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/restorecond.pid --retry 2 --exec /usr/sbin/restorecond
	RETVAL=$?
        log_end_msg $RETVAL
        ;;
    force-reload|restart)
        log_action_begin_msg "Restarting restorecond"
        unset LANG LC_TIME LC_ALL LC_MESSAGES LC_NUMERIC LC_MONETARY LC_COLLATE
        $0 stop  || RETVAL=1
        sleep 1
        $0 start || RETVAL=1
        log_action_end_msg $RETVAL
	;;
    status)
	status_of_proc /usr/sbin/restorecond restorecond
	exit $?
	;;
    *)
        log_success_msg "Usage: $0 {start|stop|restart|reload|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL
