#!@l_prefix@/bin/openpkg rc
##
##  rc.nginx -- Run-Commands
##

%config
    nginx_enable="$openpkg_rc_def"
    nginx_flags=""
    nginx_log_prolog="true"
    nginx_log_epilog="true"
    nginx_log_numfiles="10"
    nginx_log_minsize="1M"
    nginx_log_complevel="9"

%common
    nginx_pidfile="@l_prefix@/var/nginx/run/nginx.pid"
    nginx_logfile="@l_prefix@/var/nginx/log/nginx.log"
    nginx_signal () {
        [ -f $nginx_pidfile ] && kill -$1 `cat $nginx_pidfile`
    }

%status -u @l_susr@ -o
    nginx_usable="unknown"
    nginx_active="no"
    rcService nginx enable yes && \
        nginx_signal 0 && nginx_active="yes"
    echo "nginx_enable=\"$nginx_enable\""
    echo "nginx_usable=\"$nginx_usable\""
    echo "nginx_active=\"$nginx_active\""

%start -u @l_susr@
    rcService nginx enable yes || exit 0
    rcService nginx active yes && exit 0
    @l_prefix@/sbin/nginx ${nginx_flags}

%stop -u @l_susr@
    rcService nginx enable yes || exit 0
    rcService nginx active no  && exit 0
    @l_prefix@/sbin/nginx -s quit
    sleep 2
    @l_prefix@/sbin/nginx -s stop
    sleep 1
    nginx_signal TERM
    sleep 2
    rm -f $nginx_pidfile >/dev/null 2>&1 || true

%restart -u @l_susr@
    rcService nginx enable yes || exit 0
    rcService nginx active no  && exit 0
    rc nginx stop start

%reload -u @l_susr@
    rcService nginx enable yes || exit 0
    rcService nginx active no  && exit 0
    @l_prefix@/sbin/nginx -s reload

%daily -u @l_susr@
    rcService nginx enable yes || exit 0
    shtool rotate -f \
        -n $nginx_log_numfiles -s $nginx_log_minsize -d \
        -z $nginx_log_complevel -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "$nginx_log_prolog" \
        -E "$nginx_log_epilog; @l_prefix@/sbin/nginx -s reopen" \
        $nginx_logfile

