#!/bin/bash

# chkconfig:   2345 85 15
# description: SOGo server
# processname:  sogod
# config:      /etc/sysconfig/sogo
# config:      /etc/httpd2/conf/sites-enabled/SOGo.conf
# pidfile:     /run/sogo/sogo.pid

### BEGIN INIT INFO
# Provides:          sogo
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: SOGo server
### END INIT INFO

# SOGo init script for ALT Linux
#
# Copyright (C) 2015 Andrey Cherepanov <cas@altlinux.org>
#
# 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
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

NAME=sogo
DAEMON=/usr/sbin/sogod
DESC="SOGo"

USER=_sogo
PREFORK=3

PIDFILE=/run/$NAME/$NAME.pid
LOGFILE=/var/log/$NAME/$NAME.log
LOCKFILE=/var/lock/subsys/$NAME

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

checkDir() {
    directory="$1"
    if [ ! -d "$directory" ]
    then
	echo "$directory does not exist."
	exit 1
    fi

    if [ `/usr/bin/stat "$directory" -c %U` != "$USER" ]
    then
	echo "$directory is not owned by the '$USER' user."
	exit 1
    fi
}

checkDir /run/$NAME
checkDir /var/spool/$NAME
checkDir /var/log/$NAME

#if [ -z "$GNUSTEP_SYSTEM_ROOT" ]
#then
#  . /etc/GNUstep/GNUstep.conf
#  . ${GNUSTEP_MAKEFILES}/GNUstep.sh
#fi

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

start()
{
        start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --user "$USER" "$DAEMON" $DAEMON_OPTS
        RETVAL=$?
        return $RETVAL
}

stop()
{
        stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user "$USER" "$DAEMON"
        RETVAL=$?
        return $RETVAL
}


restart()
{
        stop
        sleep 1
        start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
        status)
		status --pidfile "$PIDFILE" --displayname "$DESC" -- "$DAEMON"
                RETVAL=$?
                ;;
        *)
                msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
                RETVAL=1


esac

exit $?
