#! /bin/sh
#
# redmine       Redmine task tracking system
#
# chkconfig: - 70 30
# description:  \
#       Redmine is a flexible project management web application.
#       Written using Ruby on Rails framework, it is cross-platform and cross-database.
# processname: ruby
# config: /etc/sysconfig/redmine

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

SourceIfNotEmpty /etc/sysconfig/redmine

RDMSERVE=server
RDM_OPTS="-d -e $RDM_ENV"
PIDFILE=/tmp/redmine.pid
LOCKFILE=none
RETVAL=0
PS_COLUMNS="200"

RDMSERVE="$RPM_SRV/$RDMSERVE"

[ -x "$RDMSERVE" ] || exit 1

start()
{
        start_daemon --displayname redmine --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --user "$RDM_USER" --expect-user "$RDM_USER" -- $RDMSERVE $RDM_OPTS
        ps aux --columns 200 | grep 'script/server' | grep -v grep | awk '{print $2}' > $PIDFILE
        RETVAL=$?
        return $RETVAL
}

stop()
{
        stop_daemon --displayname redmine --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user "$RDM_USER" -- ruby
        RETVAL=$?
        return $RETVAL
}

restart()
{
        stop
        start
}

reload()
{
        msg_reloading redmine
        stop_daemon --pidfile "$PIDFILE" --expect-user "$RDM_USER" -HUP -- ruby
        RETVAL=$?
        return $RETVAL
}

# See how we were called.
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        reload)
                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 --displayname redmine --pidfile "$PIDFILE" --expect-user "$RDM_USER" -- ruby
                RETVAL=$?
                ;;
        *)
                msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
                RETVAL=1
esac

exit $RETVAL