#!/bin/sh
#
#   nginx
#   php-fpm
#
# chkconfig:   - 85 15
# description:  Run vesta web server
# processname: vesta
. /etc/rc.d/init.d/functions

# Vesta PATH
. /etc/profile.d/vesta.sh

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/vesta/nginx/sbin/vesta-nginx"
nignx_conf="/usr/local/vesta/nginx/conf/nginx.conf"
php="/usr/local/vesta/php/sbin/vesta-php"
php_conf="/usr/local/vesta/php/etc/php-fpm.conf"

vesta_user=admin
vesta_group=admin

start() {
    [ -x $nginx ] || exit 5
    [ -f $nignx_conf ] || exit 6
    [ -x $php ] || exit 1
    [ -f $php_conf ] || exit 2

    echo -n $"Starting vesta-nginx: "
    daemon $nginx
    retval=$?
    echo
    [[ $retval -ne 0 ]] && exit $retval

    echo -n $"Starting vesta-php: "
    daemon $php
    echo
    return $retval
}

stop() {
    echo -n $"Stopping vesta-nginx: "
    killproc -p /var/run/vesta-nginx.pid vesta-nginx -QUIT
    retval=$?
    echo
    [ -f ${pidfile} ] && /bin/rm -f ${pidfile}
    [[ $retval -ne 0 ]] && exit $retval

    echo -n $"Stopping vesta-php: "
    killproc -p /var/run/vesta-php.pid vesta-php -QUIT
    retval=$?
    echo
    [ -f /var/run/vesta-php.pid ] && /bin/rm -f /var/run/vesta-php.pid
    [ -f /var/run/vesta-php.sock ] && /bin/rm -f /var/run/vesta-php.sock
    return $retval
}

restart(){
    stop
    start
}

reload() {
    echo -n $"Reloading vesta-nginx: "
    killproc -p /var/run/vesta-nginx.pid vesta-nginx -USR2
    retval=$?
    echo
    [[ $retval -ne 0 ]] && exit $retval

    echo -n $"Reloading vesta-php: "
    killproc -p /var/run/vesta-php.pid vesta-php -USR2
    retval=$?
    echo
    return $retval
}

rh_status(){
    status -p /var/run/vesta-nginx.pid vesta-nginx
    status -p /var/run/vesta-php.pid vesta-php
}

case "$1" in
    start)
        start;;
    stop)
        stop;;
    restart)
        restart;;
    reload)
        reload;;
    status)
        rh_status;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 3
esac
