#!/bin/sh
#
# memcached		memcached
#
# chkconfig:	345 60 40
#
# description:	memcached is a cache daemon.
#
# processname:	memcached
# pidfile:      /var/run/memcached/*.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "memcached"
		exit 1
	fi
else
	exit 0
fi

instance_start() {
	(
		. /etc/memcached.d/${1}
		msg_starting "memcached (${1})"
		# user/group
		arg_group="${MEMCACHED_GROUP:+:$MEMCACHED_GROUP}"
		ssdargs="$ssdargs ${MEMCACHED_USER:+-c $MEMCACHED_USER$arg_group}"
		ssdargs="$ssdargs ${MEMCACHED_SERVICE_RUN_NICE_LEVEL:+-N $MEMCACHED_SERVICE_RUN_NICE_LEVEL}"
		
		
		if [ -n "$MEMCACHED_SOCKET" ]; then
			args="$args -s $MEMCACHED_SOCKET"
		else
			args="$args ${MEMCACHED_ADDRESS:+-l $MEMCACHED_ADDRESS}"
			args="$args ${MEMCACHED_PORT:+-p $MEMCACHED_PORT}"
		fi
		
		args="$args ${MEMCACHED_MEMUSAGE:+-m $MEMCACHED_MEMUSAGE}"
		args="$args ${MEMCACHED_MAXCONN:+-c $MEMCACHED_MAXCONN}"
		args="$args $MEMCACHED_OPTS"

		# set umask the same way daemon() does.
		[ -z "$DEFAULT_SERVICE_UMASK" ] && DEFAULT_SERVICE_UMASK=022
		umask ${SERVICE_UMASK:-$DEFAULT_SERVICE_UMASK}

		env -i PATH=$PATH /sbin/start-stop-daemon --start -x /usr/sbin/memcached -p /var/run/memcached/${1}.pid -o $ssdargs -g memcached -- -d -P /var/run/memcached/${1}.pid $args

		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			ok
		else
			fail
		fi

	)
	return $?
}

instance_stop() {
	(
		. /etc/memcached.d/${1}
		msg_stopping "memcached (${1})"
		killproc --pidfile memcached/${1}.pid memcached
	)
	return $?
}

instance_status() {
	status --pidfile memcached/${1}.pid memcached-${1} memcached-${1}
	return $?
}

get_instances() {
	local files
	
	
	if [ -n "$(find /etc/memcached.d/ -maxdepth 0 -empty)" ]; then 
		nls "Error: no memcached configuration files in %s, nothing to run" "/etc/memcached.d/"
		exit 1
	fi
	
	if [ -n "$1" ]; then
		files="/etc/memcached.d/$1"
	else
		files="$(find /etc/memcached.d/ -maxdepth 1 -type f)"
	fi
	
	echo "$files"  | while read file; do
		if [ ! -f "$file" ]; then
			nls "Error: configuration for instance '%s' not found" $file
			exit 1
		fi
		
# 		if [ -x "$file" ]; then
# 			nls "Notice: ignored instance '%s' configuration, not executable" $file
# 			continue
# 		fi
		
		echo $file
	done
}

start() {
	local file
	local nstarted=0
	local nfailed=0
	
	get_instances $1 | while read file; do
		if [ -f /var/lock/subsys/memcached-${file##*/} ]; then
			msg_already_running "memcached (${file##*/})"
			continue
		fi
		
		instance_start ${file##*/}
		
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			let nstarted=$nstarted+1
			touch /var/lock/subsys/memcached-${file##*/}
		else
			let nfailed=$nfailed+1
		fi
	done
	
	if [ $nfailed -gt 0 ]; then
		nls "Warning: some instances failed to start"
	fi
}

stop() {
	local file
	
	if [ -n "$(find /var/lock/subsys/ -maxdepth 0 -name \"memcached-*\" -empty)" ]; then
		msg_not_running "memcached"
		return
	fi
	
	get_instances $1 | while read file; do
		if [ ! -f /var/lock/subsys/memcached-${file##*/} ]; then
			msg_not_running "memcached (${file##*/})"
			continue
		fi
		
		instance_stop ${file##*/}
		
		rm -f /var/lock/subsys/memcached-${file##*/} >/dev/null 2>&1
	done
}

list_status() {
	local file

	get_instances $1 | while read file; do
		instance_status ${file##*/}
	done
}

RETVAL=0
# See how we were called.
case "$1" in
start)
	start $2
	;;
stop)
	stop $2
	;;
restart|force-reload)
	stop $2
	start $2
	;;
status)
	list_status $2
	;;
*)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
