#!/bin/sh

###
# Live never ending Tale
#
# live-net-service script
#
# Copyright: (C) 2009-2013 Jordi Pujol <jordi_pujol@telefonica.net>
# License:   GPLv2
#
# Live never ending Tale Project Homepage:
# http://livenet.selfip.com
###

# this script should not be executed directly,
# but must be included in a service script
#
# The purpose of this development is to manage all services in /etc/init.d
# using an unique parametrized program, that standarizes routines to do the
# corresponding service actions.
# A service script will contain the LSB headers and after include this file,
# An example of /etc/init.d/service-name follows:
# #!/bin/sh
# ...
# ### END INIT INFO
# . /lib/init/service-init
# DAEMON_ACTIONS_EXTRA="user-status port-accept port-deny"
# . /lib/init/service

if [ -n "${DAEMON_DISABLED}" ]; then
	echo "'${NAME}' is not enabled." >&2
	logger -t service "service ${NAME} : Error: daemon is not enabled."
	exit 1
fi

[ -z "${DAEMON_VERBOSE}" ] || \
	logger -t service "service ${NAME} : ${SERVICE} ${@}"

DAEMON="${DAEMON:-"$(which "${NAME}")"}"
DAEMON="${DAEMON:-"/usr/sbin/${NAME}"}"
if ! test -x "${DAEMON}"; then
	echo "Error: Can't find daemon '${DAEMON}' for service '${NAME}'." >&2
	logger -t service "Error: Can't find daemon '${DAEMON}' for service '${NAME}'."
	exit 1
fi
DAEMON_PNAME="$(basename "${DAEMON}")"

DAEMON_PIDFILE="${DAEMON_PIDFILE:-"/var/run/${NAME}.pid"}"
if [ -z "${1}" ] ; then
	case "$(basename "${0}")" in
		S??*) rc="start" ;;
		K??*) rc="stop" ;;
		*) rc="usage" ;;
	esac
else
	rc="${1}"
	shift
fi

if [ -z "${DAEMON_ACTIONS_CMD}" ]; then
	DAEMON_ACTIONS_CMD="${rc}"
else
	DAEMON_ACTIONS_CMD=""
fi

case "${rc}" in
	start)
		if [ -z "${DAEMON_START}" ]; then
			log_warning_msg "Warning: service ${NAME} is not able to ${rc}." >&2
			logger -t service "Warning: service ${NAME} is not able to ${rc}."
		fi
        log_begin_msg "$(echo "${DAEMON_DESC}" | grep -qsE '^[[:upper:]]+' || echo "Starting ")\
${DAEMON_DESC}: "
		if pid="$(pidofproc -p "${DAEMON_PIDFILE}" "${DAEMON_PNAME}")"; then
            log_warning_msg ". Already running"
            log_end_msg 0
		else
			if [ -n "${DAEMON_LOG}" ]; then
				start-stop-daemon --start --quiet --oknodo \
				${DAEMON_BACKGROUND:+"--background"} \
				--exec "${DAEMON}" -- ${DAEMON_ACTIONS_CMD} \
				${@:-"${DAEMON_START_OPTS}"} \
				>> "${DAEMON_LOG}" 2>&1
				log_end_msg ${?}
			else
				start-stop-daemon --start --quiet --oknodo \
				${DAEMON_BACKGROUND:+"--background"} \
				--exec "${DAEMON}" -- ${DAEMON_ACTIONS_CMD} \
				${@:-"${DAEMON_START_OPTS}"}
				log_end_msg ${?}
			fi
			[ -z "${DAEMON_PIDFILE_CREATE}" ] || \
				pidofproc "${DAEMON_PNAME}" > "${DAEMON_PIDFILE}"
		fi
		;;
	stop)
        log_begin_msg "$(echo "${DAEMON_DESC}" | grep -qsE '^[[:upper:]]+' || echo "Stopping ")\
${DAEMON_DESC}: "
		if pid="$(pidofproc -p "${DAEMON_PIDFILE}" "${DAEMON_PNAME}")"; then
			if [ -z "${DAEMON_STOP_SIG}" ]; then
				start-stop-daemon --start --quiet --oknodo \
				${DAEMON_BACKGROUND:+"--background"} \
				--exec "${DAEMON}" \
				-- ${DAEMON_ACTIONS_CMD} \
				${@:-"${DAEMON_STOP_OPTS}"} \
				log_end_msg ${?}
			else
				start-stop-daemon --stop --quiet --oknodo \
				${DAEMON_STOP_SIG:+"--signal"} ${DAEMON_STOP_SIG} \
				--exec "${DAEMON}"
				log_end_msg ${?}
			fi
			while "$(pidofproc -p "${DAEMON_PIDFILE}" "${DAEMON_PNAME}" >/dev/null 2>&1)"; do
				sleep 1s
				echo -n "."
			done
			rm -f "${DAEMON_PIDFILE}" > /dev/null 2>&1
		else
			if [ -n "${DAEMON_ACTIONS_CMD}" ] || [ -n "${DAEMON_STOP}" ]; then
				start-stop-daemon --start --quiet --oknodo \
				${DAEMON_BACKGROUND:+"--background"} \
				--exec "${DAEMON}" \
				-- ${DAEMON_ACTIONS_CMD} \
				${@:-"${DAEMON_STOP_OPTS}"}
				log_end_msg ${?}
			else
				log_warning_msg ". Daemon is not running"
			fi
		fi
		;;
	restart)
		if [ -z "${DAEMON_START}" ] || [ -z "${DAEMON_STOP}" ]; then
			log_warning_msg "Warning: service ${NAME} is not able to ${rc}." >&2
			logger -t service "Warning: service ${NAME} is not able to ${rc}."
		fi
        log_begin_msg "$(echo "${DAEMON_DESC}" | grep -qsE '^[[:upper:]]+' || echo "Restarting ")\
${DAEMON_DESC}: "
		"${SERVICE}" stop
		sleep 1	# wait one extra second
		"${SERVICE}" start
		;;
	status)
        log_begin_msg "Status of ${DAEMON_DESC}: "
		if pid="$(pidofproc -p "${DAEMON_PIDFILE}" "${DAEMON_PNAME}")"; then
            log_warning_msg ". Daemon ${NAME} is running in process ${pid}"
            log_end_msg 0
		else
            log_warning_msg ". Daemon is not running"
            log_end_msg 0
		fi
		# even if the daemon is not running, maybe it has set some values,
		${DAEMON} ${DAEMON_ACTIONS_CMD} \
			${DAEMON_STATUS_OPTS}
		;;
	reload)
		if [ -z "${DAEMON_RELOAD_OPTS}" ] && \
		[ -z "${DAEMON_RELOAD_SIG}" ]; then
			log_warning_msg "Warning: service ${NAME} is not able to ${rc}." >&2
			logger -t service "Warning: service ${NAME} is not able to ${rc}."
		fi
        log_begin_msg "$(echo "${DAEMON_DESC}" | grep -qsE '^[[:upper:]]+' || echo "Reloading ")\
${DAEMON_DESC}: "
		if pid="$(pidofproc -p "${DAEMON_PIDFILE}" "${DAEMON_PNAME}")"; then
			if [ -z "${DAEMON_RELOAD_SIG}" ]; then
				${DAEMON} \
				${DAEMON_ACTIONS_CMD} \
				${@:-"${DAEMON_RELOAD_OPTS}"}
				log_end_msg ${?}
			else
				start-stop-daemon --stop --quiet --oknodo \
				--signal "${DAEMON_RELOAD_SIG}" \
				--exec "${DAEMON}"
				log_end_msg ${?}
			fi
		else
			log_warning_msg "Warning: Daemon is not running."
			${DAEMON} reload
			log_end_msg ${?}
		fi
		;;
	*)
		if [ -n "${rc}" ] && \
		[ -n "${DAEMON_ACTIONS_EXTRA}" ] && \
		echo "${DAEMON_ACTIONS_EXTRA}" | grep -qswe "${rc}"; then
			log_begin_msg "${rc} ${DAEMON_DESC}: "
			${DAEMON} \
			${DAEMON_ACTIONS_CMD} \
			"${@}"
			log_end_msg ${?}
		else
			echo "Usage: service ${NAME} (start|stop|restart|status|reload|\
${DAEMON_ACTIONS_EXTRA:+"$(echo "${DAEMON_ACTIONS_EXTRA}" | tr -s ' ' '|')|"}usage)" >&2
		fi
		;;
esac

:
