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

%config
    lighttpd_enable="$openpkg_rc_def"
    lighttpd_log_files="@l_prefix@/var/lighttpd/log/lighttpd.access.log"
    lighttpd_log_prolog="true"
    lighttpd_log_epilog="true"
    lighttpd_log_numfiles="10"
    lighttpd_log_minsize="1M"
    lighttpd_log_complevel="9"
    lighttpd_err_files="@l_prefix@/var/lighttpd/log/lighttpd.error.log"
    lighttpd_err_prolog="true"
    lighttpd_err_epilog="true"
    lighttpd_err_numfiles="10"
    lighttpd_err_minsize="1M"
    lighttpd_err_complevel="9"

%common
    lighttpd_conffile="@l_prefix@/etc/lighttpd/lighttpd.conf"
    lighttpd_pidfile="@l_prefix@/var/lighttpd/lighttpd.pid"
    lighttpd_signal () {
        [ -f $lighttpd_pidfile ] && kill -$1 `cat $lighttpd_pidfile`
    }

%status -u @l_susr@ -o
    lighttpd_usable="no"
    lighttpd_active="no"
    @l_prefix@/sbin/lighttpd -f $lighttpd_conffile -t >/dev/null 2>&1 && \
        lighttpd_usable="yes"
    rcService lighttpd enable yes && \
        lighttpd_signal 0 && lighttpd_active="yes"
    echo "lighttpd_enable=\"$lighttpd_enable\""
    echo "lighttpd_usable=\"$lighttpd_usable\""
    echo "lighttpd_active=\"$lighttpd_active\""

%start -u @l_susr@
    rcService lighttpd enable yes || exit 0
    rcService lighttpd active yes && exit 0
    @l_prefix@/sbin/lighttpd -f $lighttpd_conffile

%stop -u @l_susr@
    rcService lighttpd enable yes || exit 0
    rcService lighttpd active no  && exit 0
    lighttpd_signal TERM
    sleep 2
    rm -f $lighttpd_pidfile >/dev/null 2>&1 || true

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

%reload -u @l_susr@
    rcService lighttpd enable yes || exit 0
    rcService lighttpd active no  && exit 0
    lighttpd_signal HUP

%daily -u @l_susr@
    rcService lighttpd enable yes || exit 0
    if [ ".$lighttpd_log_files" != . ]; then
        shtool rotate -f \
            -n ${lighttpd_log_numfiles} -s ${lighttpd_log_minsize} -d \
            -z ${lighttpd_log_complevel} -m 644 -o @l_nusr@ -g @l_ngrp@ \
            -P "${lighttpd_log_prolog}" \
            -E "${lighttpd_log_epilog}; rc lighttpd reload" \
            $lighttpd_log_files
    fi
    if [ ".$lighttpd_err_files" != . ]; then
        shtool rotate -f \
            -n ${lighttpd_err_numfiles} -s ${lighttpd_err_minsize} -d \
            -z ${lighttpd_err_complevel} -m 644 -o @l_nusr@ -g @l_ngrp@ \
            -P "${lighttpd_err_prolog}" \
            -E "${lighttpd_err_epilog}; rc lighttpd reload" \
            $lighttpd_err_files
    fi

