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

%config
    rsync_enable="$openpkg_rc_def"
    rsync_daemon="no"
    rsync_flags=""
    rsync_nice="10"
    rsync_log_prolog="true"
    rsync_log_epilog="true"
    rsync_log_numfiles="10"
    rsync_log_minsize="1M"
    rsync_log_complevel="9"

%common
    rsync_cfgfile="@l_prefix@/etc/rsync/rsync.conf"
    rsync_logfile="@l_prefix@/var/rsync/rsync.log"
    rsync_pidfile="@l_prefix@/var/rsync/rsync.pid"
    rsync_signal () {
        [ -f $rsync_pidfile ] && kill -$1 `cat $rsync_pidfile`
    }

%status -u @l_susr@ -o
    rsync_usable="unknown"
    rsync_active="no"
    rcService rsync enable yes && \
        rsync_signal 0 && rsync_active="yes"
    echo "rsync_enable=\"$rsync_enable\""
    echo "rsync_usable=\"$rsync_usable\""
    echo "rsync_active=\"$rsync_active\""

%start -u @l_susr@
    rcService rsync enable yes || exit 0
    rcService rsync active yes && exit 0
    rcVarIsYes rsync_daemon || exit 0
    rm -f $rsync_pidfile 2>/dev/null || true
    nice -n $rsync_nice @l_prefix@/bin/rsync \
         $rsync_flags --daemon --config=$rsync_cfgfile
    sleep 2

%stop -u @l_susr@
    rcService rsync enable yes || exit 0
    rcService rsync active no  && exit 0
    rcVarIsYes rsync_daemon || exit 0
    rsync_signal TERM
    sleep 2

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

%daily -u @l_susr@
    rcService rsync enable yes || exit 0
    rcVarIsYes rsync_daemon || exit 0
    shtool rotate -f \
        -n ${rsync_log_numfiles} -s ${rsync_log_minsize} -d \
        -z ${rsync_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${rsync_log_prolog}" \
        -E "${rsync_log_epilog}; rc rsync restart" \
        $rsync_logfile

