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

%config
    fileschanged_enable="$openpkg_rc_def"
    fileschanged_flags="-a -r"

%common
    fileschanged_pidfile="@l_prefix@/var/fileschanged/fileschanged.pid"
    fileschanged_tmpfile="@l_prefix@/var/fileschanged/fileschanged.txt"
    fileschanged_logfile="@l_prefix@/var/fileschanged/fileschanged.log"
    fileschanged_cfgfile="@l_prefix@/etc/fileschanged/fileschanged.conf"
    fileschanged_signal () {
        [ -f $fileschanged_pidfile ] && kill -$1 `cat $fileschanged_pidfile`
    }

%status -u @l_susr@ -o
    fileschanged_usable="unknown"
    fileschanged_active="no"
    rcService fileschanged enable yes && \
        fileschanged_signal 0 && fileschanged_active="yes"
    echo "fileschanged_enable=\"$fileschanged_enable\""
    echo "fileschanged_usable=\"$fileschanged_usable\""
    echo "fileschanged_active=\"$fileschanged_active\""

%start -u @l_susr@
    rcService fileschanged enable yes || exit 0
    rcService fileschanged active yes && exit 0
    sed -e '/^ *#.*/d' \
        -e '/^ *$/d' \
        -e 's/\([^ ]*\) \([^ ]*\) *$/\1/' \
        <$fileschanged_cfgfile >$fileschanged_tmpfile
    if [ ".`cat $fileschanged_tmpfile`" = . ]; then
        echo "ERROR: no files or directories configured for monitoring" 1>&2
        exit 1
    fi
    ( nohup @l_prefix@/bin/fileschanged \
        ${fileschanged_flags} \
        -x @l_prefix@/bin/fileschanged-dispatch \
        -l $fileschanged_tmpfile \
        </dev/null >>$fileschanged_logfile 2>&1 &
        echo $! >$fileschanged_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService fileschanged enable yes || exit 0
    shtool rotate -f \
        -n ${fileschanged_log_numfiles} -s ${fileschanged_log_minsize} -d \
        -z ${fileschanged_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${fileschanged_log_prolog}" \
        -E "${fileschanged_log_epilog}; rc fileschanged restart" \
        $fileschanged_logfile

