#!/bin/bash

#
# /etc/rc.d/init.d/S41atd - Start/Stop the atd daemon(s).
#

# Comment out the following exit line to enable this script.
#exit 0

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

DAEMON="/usr/sbin/atd"
ATDSPOOL=/var/spool/at

get_pid() {
    echo $1
}


case "$1" in

    start)
        if [ -f /var/run/atd.pid ]; then
            exit 1
        fi
        if [ ! -f $DAEMON ]; then
            exit 1
        fi
        msg -n "Starting atd:"
        if [ ! -d $ATDSPOOL ]
        then
            mkdir $ATDSPOOL
        fi
	daemon --survive=5 $DAEMON $ATDSPOOL
        if [ "$?" = "0" ]; then 
             ps_line=`ps -ax | grep "$DAEMON" | grep -v grep`
             pid=`get_pid $ps_line`
             if [ "$pid" != "" ]; then
                 echo $pid > /var/run/atd.pid
		 touch /var/lock/subsys/atd
             fi
        fi
	msg
        ;;

    stop)
        if [ ! -f /var/run/atd.pid ]; then
            exit 1
        fi
        if [ ! -f $DAEMON ]; then
            exit 1
        fi
        msg -n "Stopping atd:"
	killproc $DAEMON
	msg
	rm -f /var/lock/subsys/atd
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    *)
        echo "Usage: $0 (start|stop|restart)"
        exit 1
        ;;

esac

exit 0

