#!/bin/bash
#
# chef-client Startup script for the Chef client
#
# chkconfig: - 98 02
# description: Client component of the Chef systems integration framework.

### BEGIN INIT INFO
# Provides: chef-client
# Required-Start: $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Should-Start: $named $time
# Should-Stop: $named $time
# Short-Description: Startup script for the Chef client
# Description: Client component of the Chef systems integration framework.
### END INIT INFO
# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1
exec="/usr/bin/chef-client"
prog="chef-client"

# Source function library.
. /etc/init.d/functions

SourceIfExists /etc/sysconfig/$prog

CONFIG=${CONFIG-/etc/chef/client.rb}
PIDFILE=${PIDFILE-/var/run/chef/client.pid}
LOCKFILE=${LOCKFILE-/var/lock/subsys/$prog}
LOGFILE=${LOGFILE-/var/log/chef/client.log}
INTERVAL=${INTERVAL-900}
SPLAY=${SPLAY-20}
OPTIONS=${OPTIONS-}

start()
{
        if [ ! -d /var/run/chef ]; then
            mkdir -p /var/run/chef
            chmod 0750 /var/run/chef
            chown _chef:_chef /var/run/chef
        fi
        start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --user _chef --displayname chef-client -- $exec -d -c "$CONFIG" -L "$LOGFILE" -P "$PIDFILE" -i "$INTERVAL" -s "$SPLAY" $OPTIONS
        RETVAL=$?
        return $RETVAL
}

stop()
{
        stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _chef --displayname chef-client -- ruby
        RETVAL=$?
        return $RETVAL
}

restart()
{
        stop
        sleep 3
        start
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload|restart)
                restart
                ;;
        condstop)
                if [ -e "$LOCKFILE" ]; then
                        stop
                fi
                ;;
        condrestart)
                if [ -e "$LOCKFILE" ]; then
                        restart
                fi
                ;;
        condreload)
                if [ -e "$LOCKFILE" ]; then
                        reload
                fi
                ;;
        status)
                status --pidfile "$PIDFILE" --expect-user _chef --displayname chef-client -- ruby
                RETVAL=$?
                ;;
        *)
                msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
                RETVAL=1
esac

exit $RETVAL
