#! /bin/sh
# Copyright (c) 2012 Andreas Lang-Nevyjel
#
# RedHat stuff
# chkconfig: 2345 12 88
# description: memcache daemon
#
### BEGIN INIT INFO
# Provides:          memcached
# Required-Start:
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: memcached daemon
# Description:       Start memcached
### END INIT INFO
# 

# Read config	

export SYSTEMD_NO_WRAP="1"
if [ -f /etc/rc.status ] ; then
    . /etc/rc.status
else
    . /etc/rc.status_suse
fi

if [ -f /etc/redhat-release ] ; then
    export RH=1
    . /etc/init.d/functions
else
    export RH=0
fi

# check for debian
if [ -f /etc/debian_version ] ; then
    export DB=1
else
    export DB=0
fi


MEMCACHED_BIN=/opt/cluster/sbin/memcached
test -x $MEMCACHED_BIN || { echo "$MEMCACHED_BIN not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
MEMCACHED_CONFIG=/etc/sysconfig/memcached
test -r $MEMCACHED_CONFIG || { echo "$MEMCACHED_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

. $MEMCACHED_CONFIG

PID_FILE=/var/run/memcached/memcached.pid


rc_reset

case "$1" in
    start)
	echo -n "Starting memcached "
        if [ "${RH}" = "1" ] ; then
            daemon $MEMCACHED_BIN -u $MEMCACHED_USER $MEMCACHED_PARAMS
        elif [ "${DB}" = "1" ] ; then
            $MEMCACHED_BIN -u $MEMCACHED_USER $MEMCACHED_PARAMS
        else
            /sbin/startproc -u $MEMCACHED_USER -g $MEMCACHED_GROUP $MEMCACHED_BIN $MEMCACHED_PARAMS
        fi
	rc_status -v
	;;
    stop)
	echo -n "Shutting down memcached "
	 if [ "${DB}" = "1" ] ; then
            if [ -f ${PID_FILE} ] ; then
                kill $(cat ${PID_FILE})
            else
                echo "not running"
                rc_failed
            fi
        elif [ "${RH}" = "1" ] ; then
		    killproc $MEMCACHED_BIN
		else
            killproc -TERM $MEMCACHED_BIN
		fi
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service memcached "
	killproc -HUP $MEMCACHED_BIN
	rc_status -v
	;;
    reload)
	echo -n "Reload service memcached "
	killproc -HUP $MEMCACHED_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for service memcached "
        if [ "${RH}" = "1" ] ; then
            status $GMOND_BIN
	elif [ "${DB}" = "1" ] ; then
            if [ -f ${PID_FILE} ] ; then
                true
            else
                rc_failed
            fi  
        else
	checkproc $MEMCACHED_BIN
	fi
	rc_status -v
	;;
    probe)
	test ${MEMCACHED_CONFIG} -nt /var/run/memcached.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
