#!/bin/sh
#
# processname: /sbin/mcstransd
# pidfile:     /var/run/mcstransd.pid
#
# 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
#
### BEGIN INIT INFO
# Provides:       mcstrans
# Required-Start: $remote_fs
# Required-Stop:  $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: This starts and stops mcstransd
# Description:       This starts the SELinux Context Translation System Daemon
### END INIT INFO
#

PATH=/sbin:/bin:/usr/bin:/usr/sbin
LOCK_FILE=/var/lock/subsys/mcstrans
SETRANS_DIR=/var/run/setrans
prog="mcstransd"

# Source function library.
. /etc/rc.status

rc_reset

# Allow anyone to run status
if [ "$1" = "status" ] ; then
	echo -n $"Checking for $prog: "
	checkproc -p $LOCK_FILE $prog
	rc_status -v
	rc_exit
fi

# Check that we are root ... so non-root users stop here
if [ $EUID -ne 0 ] ; then
    echo $"Access denied. Only root can run this daemon"
    rc_failed 4
    rc_status -v
    rc_exit
fi

# Check whether SELinux is enabled
if  [ ! -x /usr/sbin/selinuxenabled ] && /usr/sbin/selinuxenabled ; then
    echo $"SELinux should be enabled to run this daemon"
    rc_failed 1
    rc_status -v
    rc_exit
fi

mkdir -p $SETRANS_DIR

start(){
	test -x /sbin/mcstransd  || exit 5
	echo -n $"Starting $prog: "
	unset HOME MAIL USER USERNAME
	startproc -p $LOCK_FILE $prog "$EXTRAOPTIONS"
	rc_status -v
}

stop(){
	echo -n $"Stopping $prog: "
	killproc -p $LOCK_FILE -TERM $prog
	rc_status -v
}

reload(){
	echo -n $"Reloading configuration of $prog: "
	killproc -HUP $prog
	rc_status -v
}

restart(){
	stop
	start
}

condrestart(){
	[ -e $LOCK_FILE ] && restart || :
}

# See how we were called.
case "$1" in
    condrestart)
	condrestart
	;;
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload)
		reload
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
		rc_failed 3
		rc_status -v
esac

rc_exit
