#!/bin/sh
#
# Zabbix daemon start/stop script.
#
# Copyright (C) 2000-2019 Zabbix SIA

### BEGIN INIT INFO
# Provides:          zabbix-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Should-Start:      mysql
# Should-Stop:       mysql
# Short-Description: Start zabbix-server daemon
### END INIT INFO

# define LSB log_* functions.
. /lib/lsb/init-functions

NAME=zabbix_server
DAEMON=/usr/local/sbin/${NAME}
DESC="Zabbix server daemon"
PID=/tmp/$NAME.pid
RUNDIR=/var/run/zabbix

test -f $DAEMON || exit 0

if [ ! -d $RUNDIR ]; then
	mkdir $RUNDIR
	chown zabbix.zabbix $RUNDIR
fi

case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	start-stop-daemon --start --oknodo --pidfile $PID --exec $DAEMON
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	start-stop-daemon --stop --quiet --pidfile $PID --retry=TERM/10/KILL/5 && return 0
	start-stop-daemon --stop --oknodo --exec $DAEMON --name $NAME --retry=TERM/10/KILL/5
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
	status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
	;;
  restart|force-reload)
  	log_daemon_msg "Restarting $DESC" "$NAME"
	$0 stop
	case "$?" in
	  0|1)
		$0 start
		case "$?" in
			0) log_end_msg 0 ;;
			1) log_end_msg 1 ;; # Old process is still running
			*) log_end_msg 1 ;; # Failed to start
		esac
		;;
	  *)
		# Failed to stop
		log_end_msg 1
		;;
	esac
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
