#!/bin/sh
# Copyright (c) 2000-2012 Synology Inc. All rights reserved.


log_begin_msg() {
	[ -z "$1" ] && return 1 || echo -n "$@"
}

log_end_msg() {
	[ -z "$1" ] && return 1 || echo "$@"
}

# Return value follows LSB
status_of_proc() {
	local daemon="$1" name="$2" pid=
	[ -z "$name" ] && name=`basename $daemon`

	if [ -f "/var/run/$name/$name.pid" ]; then
		pid="`cat /var/run/$name/$name.pid`"
	else
		echo "$name is not running"
		return 3  # LSB_STAT_NOT_RUNNING
	fi

	if [ -L "/proc/$pid/exe" ] && [ "$(basename `readlink "/proc/$pid/exe"`)" = "$name" ]; then
		echo "$name is running"
		return 0  # LSB_STAT_RUNNING
	fi
	echo "$name is not running"
	return 1  # LSB_STAT_DEAD_FPID
}

start_stop_daemon() {
	local action="$1"
	shift
	case "$action" in
		"--start") if ! status_of_proc $1; then $@; fi ;;
		"--stop") if status_of_proc $1; then killall `basename $1`; fi ;;
		*) ;;
	esac
}
