#!/bin/sh
#
# ezasynchronouspublisher       eZ Publish asychronous publishing init script for RHEL and SLES
#
# chkconfig:                    2345 86 37
# description:                  eZ Publish asychronous publishing init script for RHEL and SLES

### BEGIN INIT INFO
# Provides: eZ Publish asynchronous publishing daemon
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts instance of eZ Publish asynchronous publishing daemon
# Description: eZ Publish asychronous publishing init script for RHEL and SLES
### END INIT INFO

### Configuration
# Do not edit this file to configure the script. Use /etc/ezasynchronouspublisher.conf
### End configuration

STARTUP_SCRIPT=$(readlink -f $0)
NAME=`basename $0`

# Read configuration files
DEFAULT_CONF="${STARTUP_SCRIPT}.defaults"
GLOBAL_CONF="/etc/ezasynchronouspublisher.conf"
LOCAL_CONF="/etc/ezasynchronouspublisher/${NAME}.conf"

# Default configuration
if [ -e "${DEFAULT_CONF}" ]; then
    . "${DEFAULT_CONF}"
fi

# Global configuration
if [ -e "${GLOBAL_CONF}" ]; then
    . "${GLOBAL_CONF}"
fi

# Local configuration
if [ -e "${LOCAL_CONF}" ]; then
    . "${LOCAL_CONF}"
fi

DAEMON_OPTS=" -n"
STARTUP_SCRIPT_PATH=`dirname "${STARTUP_SCRIPT}"`
APP_PATH=${STARTUP_SCRIPT_PATH%/bin/startup/*}
DAEMON="${APP_PATH}/bin/php/ezasynchronouspublisher.php"
PID_FOLDER="/var/run/ezpublish"
PID_FILE="$PID_FOLDER/${NAME}.pid"
LOCK_FILE="/var/lock/subsys/${NAME}"
DAEMON_OPTS="${DAEMON_OPTS} --pid-file=${PID_FILE}"

if [ ${SITEACCESS} ]; then
    DAEMON_OPTS="${DAEMON_OPTS} -s ${SITEACCESS}"
fi

# prepend PHP's path if customized
if [ ${PHP_EXECUTABLE}  ]; then
    DAEMON_EXEC="${PHP_EXECUTABLE} ${DAEMON}"
else
    DAEMON_EXEC=${DAEMON}
fi

# Source function library.
if [ -f /etc/init.d/functions ]; then
# Red Hat
  . /etc/init.d/functions
elif [ -f /lib/lsb/init-functions ]; then
# LSB, SLES, ..
  . /lib/lsb/init-functions
  alias daemon=start_daemon
  alias status=MyStatus
else
  echo "Error: your platform is not supported by $NAME" > /dev/stderr
  exit 1
fi

if [ ! -d "${PID_FOLDER}" ]; then
    mkdir "${PID_FOLDER}"
    chown $RUN_AS_USER:$RUN_AS_USER "${PID_FOLDER}"
fi

start() {
    [ -x $DAEMON ] || exit 5
    echo -n $"Starting $NAME: "
    curr_dir=`pwd`
    cd $APP_PATH
    daemon --user $RUN_AS_USER --check $NAME --pidfile $PID_FILE $DAEMON $DAEMON_OPTS
    retval=$?
    echo
    cd $curr_dir
    [ $retval -eq 0 ] && touch $LOCK_FILE
    return $retval
}

stop() {
    echo -n $"Stopping $NAME: "
    killproc -p $PID_FILE $NAME
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $LOCK_FILE
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

d_status() {
    status -p $PID_FILE $NAME
}

d_status_q() {
    d_status >/dev/null 2>&1
}


case "$1" in
    start)
        d_status_q && exit 0
        $1
        ;;
    stop)
        d_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        d_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        d_status
        ;;
    condrestart|try-restart)
        d_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?

