#!/bin/sh

# ispCP ω (OMEGA) a Virtual Hosting Control Panel
# Copyright (C) 2006-2010 by isp Control Panel - http://ispcp.net
#
# Version: $Id: ispcp_daemon 2505 2010-01-06 20:30:53Z nuxwin $
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
#
# The Original Code is "ispCP ω (OMEGA) a Virtual Hosting Control Panel".
#
# The Initial Developer of the Original Code is ispCP Team.
# Portions created by Initial Developer are Copyright (C) 2006-2010 by
# isp Control Panel. All Rights Reserved.
#
# The ispCP ω Home Page is:
#
#    http://isp-control.net
#

### BEGIN INIT INFO
# Provides:             ispcp_daemon
# Required-Start:       $network $local_fs $remote_fs
# Required-Stop:
# Should-Stop:          $local_fs
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:    ispCP GUI-Backend communication Daemon
### END INIT INFO
# Note: do not modify any of these vars here, use /etc/default/$NAME instead

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=ispcp_daemon
DAEMON=/var/www/ispcp/daemon/$NAME
PID=/var/run/$NAME.pid
DESC="ispCP GUI-Backend communication Daemon"
LOGDIR=/var/log/ispcp
LOGFILE=${LOGDIR}/${NAME}.log
DIETIME=3

START=1

# Debian LSB extensions (will be used if init-functions doesn't override them):
log_daemon_msg() {
	if [ ! -z "${2:-}" ]; then
		log_success_msg "${1:-}: ${2:-}"
	else
		log_success_msg "${1:-}"
	fi
}
log_end_msg() {
	local status="$1"
}

# if not present (e.g. *BSD) make sure to provide compatible methods via /etc/default/$NAME
if [ -f /lib/lsb/init-functions ]; then
	. /lib/lsb/init-functions
fi

test -x $DAEMON || {
	if [ "$1" = "stop" ]; then
		log_warning_msg "$DAEMON not installed, but stopping anyway."
	else
		exit 5
	fi }

# Read config file if present.
if [ -r /etc/default/$NAME ]; then
	. /etc/default/$NAME
fi

if [ $START -eq 0 ]; then
	log_warning_msg "Not starting $DESC: edit /etc/default/$NAME."
	exit 1
fi

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"

	start_daemon -p $PID $DAEMON -p $PID

	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"

	killproc -p $PID $DAEMON

	log_end_msg $?

	[ ! -f "$PID" ] || rm -f $PID
	;;
  restart|force-reload)
	log_daemon_msg "Stopping $DESC" "$NAME"

	killproc -p $PID $DAEMON

	log_end_msg $?

	[ ! -f "$PID" ] || rm -f $PID

	[ -n "$DIETIME" ] && sleep "$DIETIME"

	log_daemon_msg "Starting $DESC" "$NAME"

	start_daemon -p $PID $DAEMON -- -p $PID

	log_end_msg $?
	;;
  status)
	log_daemon_msg "Checking status of $DESC"

	pidofproc $DAEMON

	status="$?"
	log_end_msg "$status"

	case "$status" in
		0)
			log_success_msg "$NAME running"
		;;
		1)
			log_warning_msg "$NAME not running (but pid file found)"
		;;
		2)
			log_warning_msg "$NAME not running (but lock file found)"
		;;
		3)
			log_warning_msg "$NAME not running"
		;;
		4|*)
			log_failure_msg "Service status is unknown"
			exit 1
		;;
        esac
	;;

  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
