
#!/bin/sh
# This is the /etc/init.d file for puppetd
# Modified for CSW
#
# description: puppetd - Puppet Automation Client
#

. /lib/svc/share/smf_include.sh

# prefix=/opt/csw
# exec_prefix=/opt/csw
sysconfdir=/etc
sbindir=/usr/ruby/1.8/sbin

pidfile=/var/lib/puppet/run/agent.pid

case "$1" in
start)
    cd /
    # Start daemons.

    printf "Starting Puppet client services:"


    if [ ! `getent group puppet` ]
    then 
	groupadd puppet
    fi

    if [ ! `getent passwd puppet` ]
    then 
	useradd -g puppet -s /bin/false -d /var/lib/puppet puppet
    fi

    if [ ! -d /var/run/puppet ]
    then
	mkdir -p /var/run/puppet
	chown puppet:puppet /var/run/puppet
    fi

    if [ ! -d /var/log/puppet ]
    then
	mkdir -p /var/log/puppet
	chown puppet:puppet /var/log/puppet
    fi

    if [ ! -d /var/lib/puppet ]
    then
	mkdir -p /var/lib/puppet/run
	chown -R puppet:puppet /var/lib/puppet
    fi

    # /opt/csw/sbin/puppetd
    ${sbindir}/puppetd

    printf " puppetd"
    echo ""
    ;;
stop)
    if [ -f $pidfile ]
    then
	printf "Stopping Puppet client services:"
	kill `cat $pidfile`
    else
	echo "pid file not found."
    fi

    printf " puppetd"
    echo ""
    ;;
restart)
    printf "Restarting Puppet client services:"
    kill -HUP `cat $pidfile`

    printf " puppetd"
    echo ""
    ;;
reload)
    printf "Reloading Puppet client services:"

    kill -HUP `cat $pidfile`

    printf " puppetd"
    echo ""
    ;;
status)
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        curpid=`pgrep puppetd`
        if [ "$pid" -eq "$curpid" ]; then
            exit 0
        else
            exit 1
        fi
    else
        exit 1
    fi
esac
exit 0
