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

%config
    bs_enable="$openpkg_rc_def"
    bs_apachectl="@l_prefix@/libexec/bs/tools/bsapachectl"

%start -u @l_rusr@
    rcService bs enable yes || exit 0
    if [ -f @l_bsdir@/bin/bsmon -a -f @l_bsdir@/etc/bsmon.cfg ]; then
        @l_prefix@/bin/perl @l_bsdir@/bin/bsmon -f @l_bsdir@/etc/bsmon.cfg
    fi
    if [ -f @l_bsdir@/bin/bbd -a -f @l_bsdir@/adm/bb-display.cfg ]; then
        @l_prefix@/bin/perl @l_bsdir@/bin/bbd -b @l_bsdir@
    fi
    for f in @l_bsdir@/adm/uxmon-net*; do
        set `basename $f`
        case $1 in
            *bak | *% )
                continue
                ;;
            *)
                @l_prefix@/bin/perl @l_bsdir@/uxmon/uxmon -r @l_bsdir@/uxmon/uxmon-rules.pl -b @l_bsdir@ -c $1
                ;;
            esac
     done
     ((ps ax 2>/dev/null) || \
     (ps -e -o pid,comm,args 2>/dev/null) || \
     (ps -e 2>/dev/null)) \
     | grep "bsmon"|grep -v "PID"|grep "perl"|awk '{printf("%d ",$1)}' \
         >@l_prefix@/var/bs/bs.pid
     ${bs_apachectl} start

%stop -u @l_rusr@
    rcService bs enable yes || exit 0
    if [ -f @l_prefix@/var/bs/bs.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/bs.pid`
        rm -f @l_prefix@/var/bs/bs.pid 2>/dev/null || true
    fi
    if [ -f @l_prefix@/var/bs/bbd.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/bbd.pid`
        rm -f @l_prefix@/var/bs/bbd.pid 2>/dev/null || true
    fi
    if [ -f @l_prefix@/var/bs/uxmon.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/uxmon.pid`
        rm -f @l_prefix@/var/bs/uxmon.pid 2>/dev/null || true
    fi
    if [ -f @l_prefix@/var/bs/run/apache.pid ]; then
        ${bs_apachectl} stop
        rm -f @l_prefix@/var/bs/run/apache.pid 2>/dev/null || true
    fi

%restart -u @l_rusr@
    rcService bs enable yes || exit 0
    if [ -f @l_prefix@/var/bs/bs.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/bs.pid`
        rm -f @l_prefix@/var/bs/bs.pid 2>/dev/null || true
        sleep 2
    fi
    if [ -f @l_prefix@/var/bs/bbd.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/bbd.pid`
        rm -f @l_prefix@/var/bs/bbd.pid 2>/dev/null || true
        sleep 2
    fi
    if [ -f @l_prefix@/var/bs/uxmon.pid ]; then
        kill -TERM `cat @l_prefix@/var/bs/uxmon.pid`
        rm -f @l_prefix@/var/bs/uxmon.pid 2>/dev/null || true
        sleep 2
    fi
    if [ -f @l_bsdir@/bin/bsmon -a -f @l_bsdir@/etc/bsmon.cfg ]; then
        @l_prefix@/bin/perl @l_bsdir@/bin/bsmon -f @l_bsdir@/etc/bsmon.cfg
    fi
    ${bs_apachectl} stop
    if [ -f @l_bsdir@/bin/bbd -a -f @l_bsdir@/adm/bb-display.cfg ]; then
        @l_prefix@/bin/perl @l_bsdir@/bin/bbd -b @l_bsdir@
    fi
    for f in @l_bsdir@/adm/uxmon-net*; do
        set `basename $f`
        case $1 in
            *bak | *%)
                continue
                ;;
            *)
                @l_prefix@/bin/perl @l_bsdir@/uxmon/uxmon -r @l_bsdir@/uxmon/uxmon-rules.pl -b @l_bsdir@ -c $1
                ;;
        esac
    done
    ps ax 2>/dev/null| grep -w "bs" 2>/dev/null|grep -v "PID"| grep "perl" | awk '{printf("%d ",$1)}' \
        >@l_prefix@/var/bs/bs.pid
    ${bs_apachectl} start

%reload -u @l_rusr@
    rcService bs enable yes || exit 0
    if [ -f @l_prefix@/var/bs/bs.pid ]; then
        kill -HUP `cat @l_prefix@/var/bs/bs.pid`
    fi
    if [ -f @l_prefix@/var/bs/bbd.pid ]; then
        kill -HUP `cat @l_prefix@/var/bs/bbd.pid`
    fi
    if [ -f @l_prefix@/var/bs/uxmon.pid ]; then
        kill -HUP `cat @l_prefix@/var/bs/uxmon.pid`
    fi
    if [ -f@l_prefix@/var/bs/apache.pid ]; then
        kill -HUP `cat @l_prefix@/var/bs/run/apache.pid`
    fi

