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

%config
    cvsd_enable="$openpkg_rc_def"
    cvsd_log_prolog="true"
    cvsd_log_epilog="true"
    cvsd_log_numfiles="10"
    cvsd_log_minsize="1M"
    cvsd_log_complevel="9"

%common
    cvsd_pidfile="@l_prefix@/var/cvsd/cvsd.pid"
    cvsd_cfgfile="@l_prefix@/etc/cvsd/cvsd.conf"
    cvsd_signal () {
        [ -f $cvsd_pidfile ] && kill -$1 `cat $cvsd_pidfile`
    }
    cvsd_chroot () {
        local d
        d=`sed -ne 's;^ *[Rr][Oo][Oo][Tt][Jj][Aa][iI][lL]  *;;p' $cvsd_cfgfile`
        [ ".$d" != . ] && \
        [ -d $d ] && \
        [ -d $d/bin ] && \
        [ -d $d/lib ] && \
        [ -d $d/dev ] && \
        [ -d $d/etc ] && \
        [ -d $d/usr ] && \
        [ -d $d/tmp ]
    }

%status -u @l_susr@ -o
    cvsd_usable="no"
    cvsd_active="no"
    rcService cvsd enable yes && \
        cvsd_chroot && cvsd_usable="yes"
    rcService cvsd enable yes && \
        cvsd_signal 0 && cvsd_active="yes"
    echo "cvsd_enable=\"$cvsd_enable\""
    echo "cvsd_usable=\"$cvsd_usable\""
    echo "cvsd_active=\"$cvsd_active\""

%start -u @l_susr@
    rcService cvsd enable yes || exit 0
    rcService cvsd active yes && exit 0
    cvsd_chroot || exit 0;
    @l_prefix@/sbin/cvsd

%stop -u @l_susr@
    rcService cvsd enable yes || exit 0
    rcService cvsd active no && exit 0
    cvsd_signal TERM
    rm -f $cvsd_pidfile 2>/dev/null || true

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

%daily -u @l_susr@
    rcService cvsd enable yes || exit 0
    shtool rotate -f \
        -n ${cvsd_log_numfiles} -s ${cvsd_log_minsize} -d \
        -z ${cvsd_log_complevel} -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "${cvsd_log_prolog}" \
        -E "${cvsd_log_epilog}; rc cvsd restart" \
        @l_prefix@/var/cvsd/cvsd.log

