#!/bin/sh
# Author: Danny Kukawka <dkukawka@suse.de>
#
# /etc/init.d/rcpolicykit
#
### BEGIN INIT INFO
# Provides:          policykitd
# Required-Start:    boot.localnet dbus 
# Should-Start:      acpid resmgr
# Required-Stop:     
# Should-Stop:
# Default-Start:     2 3 5
# Default-Stop:      
# Short-Description: PolicyKit is a framework for defining policy for system-wide components
# Description:       PolicyKit is a framework for defining policy for system-wide components
#		     and for desktop pieces to configure it. It is used by HAL.
#                    
### END INIT INFO

# Check for binary
POLICYKIT_BIN=/usr/sbin/polkitd
test -x $POLICYKIT_BIN || exit 5

# Parameters (startup)
POLICYKIT_PARA="";
POLICYKIT_PIDDIR="/var/run/polkit";
POLICYKIT_PID=$POLICYKIT_PIDDIR/pid;
DBUSDAEMON_PIDDIR="/var/run/dbus";
DBUSDAEMON_PID=$DBUSDAEMON_PIDDIR/pid;

# Source LSB init functions
# providing start_daemon, killproc, pidofproc,
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	
	if [ ! -d $POLICYKIT_PIDDIR ]; then
                mkdir -p $POLICYKIT_PIDDIR;
#		chown polkit:polkit $POLICYKIT_PIDDIR;
        fi
        if [ -e $POLICYKIT_PID ]; then
                if checkproc $POLICYKIT_BIN ; then
			echo "PolicyKit already started. Not starting."
			exit 0;
                else
                        echo "Removing stale PID file $POLICYKIT_PID.";
                        rm -f $POLICYKIT_PID;
                fi
        fi
        
	echo -n "Starting PolicyKit daemon";
        startproc -p $POLICYKIT_PID $POLICYKIT_BIN $POLICYKIT_PARA
        rc_status -v
        ;;
    start-with-dbus)
	if [ ! -e $DBUSDAEMON_PID ]; then
               echo -n "DBUS is not running. Starting D-BUS daemon";
                rcdbus start;
        fi
	$0 start
	;;
    stop)
	echo -n "Shutting down PolicyKit"
       	killproc -p $POLICYKIT_PID -TERM $POLICYKIT_BIN
       	rc_status
	rm -f $POLICYKIT_PID;
       	rc_status -v
        ;;
    try-restart)
        $0 status >/dev/null &&  $0 restart
        rc_status
        ;;
    restart)
        $0 stop
        $0 start
	# PolicyKit does not save state so we have to restart resmgr
	# in order to restore desktop-console settings (#220265)
	if /sbin/resmgr sessions > /dev/null 2>/dev/null; then
		/usr/sbin/rcresmgr restart
	fi
        ;;
    force-reload)
        $0 try-restart
	rc_status
        ;;
    reload)
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for service PolicyKit daemon"
        checkproc -p $POLICYKIT_PID $POLICYKIT_BIN
        rc_status -v
        ;;
    probe)
        ## Optional: Probe for the necessity of a reload, print out the
        ## argument to this init script which is required for a reload.
        ## Note: probe is not (yet) part of LSB (as of 1.2)
        # test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|start-with-dbus|reload|probe}"
        exit 1
        ;;
esac
rc_exit


