#!/usr/bin/ksh
#
# rrdcached:   This shell script takes care of starting and stopping rrdcached
#
# Description: Data caching daemon for RRDTool
#
# processname: rrdcached
# pidfile: /var/run/rrdcached/rrdcached.pid
# config: /etc/sysconfig/rrdcached
#
# Jul 04, 2011, Michael Perzl (michael@perzl.org)
#

PROG=rrdcached

PROG_BIN=/opt/freeware/bin/${PROG}

PIDFILE=/var/run/${PROG}/${PROG}.pid


# define some generic commands
AWK=/usr/bin/awk
CAT=/usr/bin/cat
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
KILL=/usr/bin/kill
MKDIR=/usr/bin/mkdir
PRINTF=/usr/bin/printf
PS=/usr/bin/ps
RM=/usr/bin/rm
SLEEP=/usr/bin/sleep


# check for missing binaries (stale symlinks should not happen)
test -x ${PROG_BIN} ||
    {
      $ECHO "${PROG_BIN} not installed"
      if [ "$1" = "stop" ] ; then
          exit 0
      else
          exit 5
      fi
    }


# source config
if [ -f /etc/sysconfig/${PROG} ] ; then
    . /etc/sysconfig/${PROG}
fi


case "$1" in
    start)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $ECHO "RRDTool cache daemon is already running with PID ${pid}."
                exit 1
            else
                $RM -f ${PIDFILE}
            fi
        fi
        $PRINTF "Starting RRDTool cache daemon... "

        ## start daemon and write PID to file ${PIDFILE}
        $MKDIR -p /var/run/${PROG}
        ${PROG_BIN} -p ${PIDFILE} ${RRDCACHED_OPTIONS}
        $ECHO "done."
        ;;
    stop)
        $PRINTF "Stopping RRDTool cache daemon... "
        ## stop daemon
        if [ -r ${PIDFILE} ]; then
            $KILL -TERM `$CAT ${PIDFILE}`
            $RM -f ${PIDFILE}
        fi
        $ECHO "done."
        ;;
    status)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $ECHO "RRDTool cache daemon is running with PID ${pid}."
            else
                $ECHO "RRDTool cache daemon is not running."
            fi
        else
            $ECHO "RRDTool cache daemon is not running."
        fi
        ;;
    condrestart)
        if [ -r ${PIDFILE} ]; then
            pid=`$CAT ${PIDFILE}`
            if [ "`$PS -ef | $GREP -v grep | $GREP ${PROG} | $GREP ${pid} | $AWK '{ print $2 }' | $GREP ${pid}`" = "${pid}" ] ; then
                $0 stop
                $ECHO "Sleeping for 1 second for graceful ${PROG} shutdown ..."
                $SLEEP 1
                $0 start
            fi
        fi
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $ECHO "Sleeping for 1 second for graceful ${PROG} shutdown ..."
        $SLEEP 1
        $0 start
        ;;
    *)
        $ECHO "Usage: $0 {start|stop|status|condrestart|restart}"
        exit 1
        ;;
esac

