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

%config
    netatalk_enable="$openpkg_rc_def"
    netatalk_name=""
    netatalk_zone=""
    netatalk_daemons="afpd cnid_metad"
    netatalk_flags_atalkd=""
    netatalk_flags_aftpd="-c 50"
    netatalk_flags_cnid_metad=""
    netatalk_flags_papd=""
    netatalk_log_prolog="true"
    netatalk_log_epilog="true"
    netatalk_log_numfiles="10"
    netatalk_log_minsize="1M"
    netatalk_log_complevel="9"

%common
    netatalk_etcdir="@l_prefix@/etc/netatalk"
    netatalk_vardir="@l_prefix@/var/netatalk"
    netatalk_signal () {
        local retcode=0
        local daemon
        for daemon in $netatalk_daemons; do
            [ -f $netatalk_vardir/run/$daemon.pid ] \
            && kill -$1 `cat $netatalk_vardir/run/$daemon.pid`
            retcode=$(($retcode + $?))
        done
        return $retcode
    }

%status -u @l_susr@ -o
    netatalk_usable="unknown"
    netatalk_active="no"
    rcService netatalk enable yes && \
        netatalk_signal 0 && netatalk_active="yes"
    echo "netatalk_enable=\"$netatalk_enable\""
    echo "netatalk_usable=\"$netatalk_usable\""
    echo "netatalk_active=\"$netatalk_active\""

%start -u @l_susr@
    rcService netatalk enable yes || exit 0
    rcService netatalk active yes && exit 0
    for daemon in $netatalk_daemons; do
        cmd=""; log=""
        case $daemon in
            atalkd )
                if [ ".$netatalk_name" = . ]; then
                    netatalk_name="`hostname | cut -d. -f1`"
                fi
                eval "cmd=\"@l_prefix@/sbin/$daemon \$netatalk_flags_$daemon\""
                cmd="$cmd; @l_prefix@/bin/nbprgstr -p 4 \"$netatalk_name:Workstation$netatalk_zone\" &"
                cmd="$cmd; @l_prefix@/bin/nbprgstr -p 4 \"$netatalk_name:netatalk$netatalk_zone\" &"
                log="$netatalk_vardir/log/$daemon.log"
                ;;
            afpd )
                eval "cmd=\"@l_prefix@/sbin/$daemon \$netatalk_flags_$daemon\""
                log="$netatalk_vardir/log/$daemon.log"
                ;;
            cnid_metad )
                eval "cmd=\"@l_prefix@/sbin/$daemon -l log_note -f $netatalk_vardir/log/$daemon.log $netatalk_flags_$daemon\""
                log="/dev/null"
                ;;
            papd )
                eval "cmd=\"@l_prefix@/sbin/$daemon \$netatalk_flags_$daemon\""
                log="$netatalk_vardir/log/$daemon.log"
                ;;
        esac
        ( nohup $cmd </dev/null >$log 2>&1 &
        ) >/dev/null 2>&1
    done

%stop -u @l_susr@
    rcService netatalk enable yes || exit 0
    rcService netatalk active no  && exit 0
    netatalk_signal TERM
    sleep 2
    for daemon in $netatalk_daemons; do
        rm -f $netatalk_vardir/run/$daemon.pid
    done

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

%daily -u @l_susr@
    rcService netatalk enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    for daemon in $netatalk_daemons; do
        if [ -f $netatalk_vardir/log/$daemon.log ]; then
            shtool rotate -f \
                -n $netatalk_log_numfiles -s $netatalk_log_minsize -d \
                -z $netatalk_log_complevel -m 664 -o @l_susr@ -g @l_sgrp@ \
                -P "$netatalk_log_prolog" \
                -E "$netatalk_log_epilog; echo 1 >$hintfile" \
                $netatalk_vardir/log/$daemon.log
        fi
    done
    if [ -s $hintfile ]; then
        rc netatalk restart
    fi
    rcTmp -k

