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

%config
    apache2_enable="$openpkg_rc_def"
    apache2_log_files="@l_prefix@/var/apache2/log/access.log"
    apache2_log_rotsteps="10"
    apache2_log_rotminsize="10M"
    apache2_log_rotcomplevel="9"
    apache2_log_rotprolog="true"
    apache2_log_rotepilog="true"
    apache2_err_files="@l_prefix@/var/apache2/log/error.log"
    apache2_err_rotsteps="10"
    apache2_err_rotminsize="1M"
    apache2_err_rotcomplevel="9"
    apache2_err_rotprolog="true"
    apache2_err_rotepilog="true"

%common
    apache2_pidfile="@l_prefix@/var/apache2/run/apache2.pid"
    apache2_signal () {
        [ -f $apache2_pidfile ] && kill -$1 `cat $apache2_pidfile`
    }

%status -u @l_susr@ -o
    apache2_usable="no"
    apache2_active="no"
    @l_prefix@/sbin/apache2 -t 2>/dev/null && apache2_usable="yes"
    rcService apache2 enable yes && \
        apache2_signal 0 && apache2_active="yes"
    echo "apache2_enable=\"$apache2_enable\""
    echo "apache2_usable=\"$apache2_usable\""
    echo "apache2_active=\"$apache2_active\""

%start -u @l_susr@
    rcService apache2 enable yes || exit 0
    rcService apache2 active yes && exit 0
    ( cd @l_prefix@/var/apache2/run
      [ -e @l_prefix@/etc/apache2/apache2.sh ] &&
          . @l_prefix@/etc/apache2/apache2.sh
      @l_prefix@/sbin/apache2 -k start
    ) || exit $?

%stop -u @l_susr@
    rcService apache2 enable yes || exit 0
    rcService apache2 active no && exit 0
    @l_prefix@/sbin/apache2 -k stop
    sleep 2

%restart -u @l_susr@
    rcService apache2 enable yes || exit 0
    rcService apache2 active no && exit 0
    @l_prefix@/sbin/apache2 -k restart

%reload -u @l_susr@
    rcService apache2 enable yes || exit 0
    rcService apache2 active no && exit 0
    @l_prefix@/sbin/apache2 -k graceful

%daily -u @l_susr@
    rcService apache2 enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    shtool rotate -f \
        -n ${apache2_log_rotsteps} -s ${apache2_log_rotminsize} -d \
        -z ${apache2_log_rotcomplevel} -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "$apache2_log_rotprolog" \
        -E "$apache2_log_rotepilog; echo 1 >$hintfile" \
        ${apache2_log_files}
    shtool rotate -f \
        -n ${apache2_err_rotsteps} -s ${apache2_err_rotminsize} -d \
        -z ${apache2_err_rotcomplevel} -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "$apache2_err_rotprolog" \
        -E "$apache2_err_rotepilog; echo 1 >$hintfile" \
        ${apache2_err_files}
    if [ -s $hintfile ]; then
        @l_prefix@/sbin/apache2 -k graceful
    fi
    rcTmp -k

