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

%config
    daemontools_enable="$openpkg_rc_def"
    daemontools_dir="@l_prefix@/var/daemontools/service"

%status -u @l_susr@ -o
    daemontools_usable="unknown"
    daemontools_active="no"
    rcService daemontools enable yes && \
        @l_prefix@/bin/svok $daemontools_dir/* && \
        daemontools_active="yes"
    echo "daemontools_enable=\"$daemontools_enable\""
    echo "daemontools_usable=\"$daemontools_usable\""
    echo "daemontools_active=\"$daemontools_active\""

%start -u @l_susr@
    rcService daemontools enable yes || exit 0
    rcService daemontools active yes && exit 0
    cmd="@l_prefix@/bin/svscan $daemontools_dir 2>&1"
    cmd="$cmd | @l_prefix@/bin/readproctitle service errors: "
    cmd="$cmd `awk </dev/null 'BEGIN { for (i=0; i<400; i++) { printf(\".\"); } }'`"
    nohup sh -c "$cmd &" </dev/null >/dev/null 2>&1

%stop -u @l_susr@
    rcService daemontools enable yes || exit 0
    rcService daemontools active no  && exit 0
    @l_prefix@/bin/svc -dx $daemontools_dir/* $daemontools_dir/*/log
    sleep 2

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

