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

%config
    pureftpd_enable="$openpkg_rc_def"
    pureftpd_flags=""
    pureftpd_bind="127.0.0.1"
    pureftpd_port="21"
    pureftpd_log_prolog="true"
    pureftpd_log_epilog="true"
    pureftpd_log_numfiles="10"
    pureftpd_log_minsize="1M"
    pureftpd_log_complevel="9"

%common
    pureftpd_pidfile="@l_prefix@/var/pureftpd/pureftpd.pid"
    pureftpd_signal () {
        [ -f $pureftpd_pidfile ] && kill -$1 `cat $pureftpd_pidfile`
    }

%status -u @l_susr@ -o
    pureftpd_usable="unknown"
    pureftpd_active="no"
    rcService pureftpd enable yes && \
        pureftpd_signal 0 && pureftpd_active="yes"
    echo "pureftpd_enable=\"$pureftpd_enable\""
    echo "pureftpd_usable=\"$pureftpd_usable\""
    echo "pureftpd_active=\"$pureftpd_active\""

%start -u @l_susr@
    rcService pureftpd enable yes || exit 0
    rcService pureftpd active yes && exit 0
    flags="$pureftpd_flags"
    echo $flags | grep -- -S     >/dev/null; rc1=$?
    echo $flags | grep -- --bind >/dev/null; rc2=$?
    if [ $rc1 -ne 0 -a $rc2 -ne 0 ]; then
        if [ ".$pureftpd_bind" != . ]; then
            flags="$flags --bind $pureftpd_bind"
        else
            flags="$flags --bind 0.0.0.0"
        fi
        if [ ".$pureftpd_port" != . ]; then
            flags="$flags,$pureftpd_port"
        fi
    fi
    @l_prefix@/sbin/pure-ftpd --daemonize --pidfile $pureftpd_pidfile $flags

%stop -u @l_susr@
    rcService pureftpd enable yes || exit 0
    rcService pureftpd active no  && exit 0
    pureftpd_signal TERM
    sleep 2

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

%daily -u @l_susr@
    rcService pureftpd enable yes || exit 0
    shtool rotate -f \
        -n ${pureftpd_log_numfiles} -s ${pureftpd_log_minsize} -d \
        -z ${pureftpd_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${pureftpd_log_prolog}" \
        -E "${pureftpd_log_epilog}; rc pureftpd restart" \
        @l_prefix@/var/pureftpd/pureftpd.log

