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

%config
    samba_enable="$openpkg_rc_def"
    samba_daemons="smbd nmbd"
    samba_log_prolog="true"
    samba_log_epilog="true"
    samba_log_numfiles="10"
    samba_log_minsize="1M"
    samba_log_complevel="9"

%common
    samba_signal () {
        local retcode=0
        local daemons=
        for daemons in $samba_daemons; do
            [ -f @l_prefix@/var/samba/run/${daemons}.pid ] \
            && kill -$1 `cat @l_prefix@/var/samba/run/${daemons}.pid`
            retcode=$(($retcode+$?))
        done
        return $retcode
    }

%status -u @l_susr@ -o
    samba_usable="unknown"
    samba_active="no"
    rcService samba enable yes && \
        samba_signal 0 && samba_active="yes"
    echo "samba_enable=\"$samba_enable\""
    echo "samba_usable=\"$samba_usable\""
    echo "samba_active=\"$samba_active\""

%start -u @l_susr@
    rcService samba enable yes || exit 0
    rcService samba active yes && exit 0
    for daemons in $samba_daemons; do
        case $daemons in
            ?mbd)
                @l_prefix@/sbin/$daemons -D
                ;;
            winbindd)
                @l_prefix@/sbin/$daemons -B
                ;;
            *)
                @l_prefix@/sbin/$daemons
                ;;
        esac
    done

%stop -u @l_susr@
    rcService samba enable yes || exit 0
    samba_signal TERM
    sleep 2
    for daemons in $samba_daemons; do
        rm -f @l_prefix@/var/samba/run/${daemons}.pid
    done

%restart -u @l_susr@
    rcService samba enable yes || exit 0
    rc samba stop start

%reload -u @l_susr@
    rcService samba enable yes || exit 0
    rcService samba active no  && exit 0
    samba_signal HUP

%daily -u @l_susr@
    rcService samba enable yes || exit 0
    logfiles=
    for daemons in $samba_daemons; do
        logfiles="$logfiles @l_prefix@/var/samba/run/log.$daemons"
    done
    shtool rotate -f \
        -n ${samba_log_numfiles} -s ${samba_log_minsize} -d \
        -z ${samba_log_complevel} -m 644 -o @l_susr@ -g @l_mgrp@ \
        -P "${samba_log_prolog}" \
        -E "${samba_log_epilog}; rc samba restart" \
        $logfiles

