#!/bin/bash
### BEGIN INIT INFO
# Provides: sogo-eas
# Required-Start: $remote_fs $network
# Required-Stop: $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: SOGo-EAS server
### END INIT INFO

# SOGo init script for Debian GNU/Linux
#
# Copyright (C) 2007-2019 Inverse inc.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
PATH=/sbin:/bin:/usr/sbin:/usr/bin

NAME=sogo
EASNAME=sogo-eas
DAEMON=/usr/sbin/sogod
DESC="SOGo-EAS"

USER=sogo
EASUSER=sogoeas
PREFORK=120

PIDFILE=/run/$EASUSER/$EASNAME.pid
LOGFILE=/var/log/$EASUSER/$EASNAME.log

if [ -f /etc/default/$NAME ]; then
. /etc/default/$NAME
fi

. /lib/lsb/init-functions

if [ ! -x $DAEMON ]; then
	log_failure_msg "$DAEMON is not executable."
exit 1
fi

set -e

. /usr/share/GNUstep/Makefiles/GNUstep.sh

DAEMON_OPTS="-WOWorkersCount $PREFORK -WOPidFile $PIDFILE -WOLogFile $LOGFILE"

case "$1" in
	start)
		log_daemon_msg "Starting $DESC" "$EASNAME"
		# Enforce directory existence and permissions
		install -o $EASUSER -g $USER -m 755 -d /run/$EASUSER
		install -o $EASUSER -g $USER -m 750 -d /var/log/$EASUSER

		if ! start-stop-daemon -c $EASUSER --quiet --start --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
		then
			log_progress_msg "already running"
		fi
		chown root:root $PIDFILE
		log_end_msg 0
	;;
	stop)
		log_daemon_msg "Stopping $DESC" "$EASNAME"
		start-stop-daemon --stop --pidfile $PIDFILE --retry=TERM/20/KILL/5 --oknodo
		rm -f $PIDFILE
		log_end_msg 0
	;;
	restart|force-reload)
		log_daemon_msg "Restarting $DESC" "$EASNAME"
		start-stop-daemon --stop --pidfile $PIDFILE --retry=TERM/20/KILL/5 --oknodo
		rm -f $PIDFILE
		# Enforce directory existence and permissions
		install -o $EASUSER -g $USER -m 755 -d /run/$EASUSER
		install -o $EASUSER -g $USER -m 750 -d /var/log/$EASUSER
		start-stop-daemon -c $EASUSER --quiet --start --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS
		log_end_msg 0
	;;
	status)
		status_of_proc -p $PIDFILE "$DAEMON" $EASNAME && exit 0 || exit $?
	;;
	*)
		echo "Usage: $EASNAME {start|stop|restart|status}" >&amp;2
		exit 1
	;;
esac

exit 0
