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

%config
    heimdal_enable="$openpkg_rc_def"
    heimdal_daemons="kdc kadmind kpasswdd"
    heimdal_flags_kdc=""
    heimdal_flags_kadmind=""
    heimdal_flags_kpasswdd=""
    heimdal_flags_ipropd_master=""
    heimdal_flags_ipropd_slave=""
    heimdal_log_prolog="true"
    heimdal_log_epilog="true"
    heimdal_log_numfiles="10"
    heimdal_log_minsize="1M"
    heimdal_log_complevel="9"

%common
    heimdal_logdir="@l_prefix@/var/heimdal/log"
    heimdal_pidfile_kdc="@l_prefix@/var/heimdal/run/kdc.pid"
    heimdal_pidfile_kadmind="@l_prefix@/var/heimdal/run/kadmind.pid"
    heimdal_pidfile_kpasswdd="@l_prefix@/var/heimdal/run/kpasswdd.pid"
    heimdal_pidfile_ipropd_master="@l_prefix@/var/heimdal/run/ipropd-master.pid"
    heimdal_pidfile_ipropd_slave="@l_prefix@/var/heimdal/run/ipropd-slave.pid"
    heimdal_signal () {
        [ -f $heimdal_pidfile_kdc ] \
            && kill -$1 `cat $heimdal_pidfile_kdc`
        local rc_heimdal_kdc=$?
        [ -f $heimdal_pidfile_kadmind ] \
            && kill -$1 `cat $heimdal_pidfile_kadmind`
        local rc_heimdal_kadmind=$?
        [ -f $heimdal_pidfile_kpasswdd ] \
            && kill -$1 `cat $heimdal_pidfile_kpasswdd`
        local rc_heimdal_kpasswdd=$?
        [ -f $heimdal_pidfile_ipropd_master ] \
            && kill -$1 `cat $heimdal_pidfile_ipropd_master`
        local rc_heimdal_ipropd_master=$?
        [ -f $heimdal_pidfile_ipropd_slave ] \
            && kill -$1 `cat $heimdal_pidfile_ipropd_slave`
        local rc_heimdal_ipropd_slave=$?
        [ $rc_heimdal_kdc           -eq 0 -o \
          $rc_heimdal_kadmind       -eq 0 -o \
          $rc_heimdal_kpasswdd      -eq 0 -o \
          $rc_heimdal_ipropd_master -eq 0 -o \
          $rc_heimdal_ipropd_slave  -eq 0     ]
    }

%status -u @l_susr@ -o
    heimdal_usable="no"
    heimdal_active="no"
    rcService heimdal enable yes && \
        heimdal_signal 0 && heimdal_active="yes"
    echo "heimdal_enable=\"$heimdal_enable\""
    echo "heimdal_usable=\"$heimdal_usable\""
    echo "heimdal_active=\"$heimdal_active\""

%start -u @l_susr@
    rcService heimdal enable yes || exit 0
    rcService heimdal active yes && exit 0
    for daemon in $heimdal_daemons; do
        case "$daemon" in
            kdc )
                nohup @l_prefix@/sbin/kdc $heimdal_flags_kdc &
                echo $! >$heimdal_pidfile_kdc
                ;;
            kadmind )
                nohup @l_prefix@/sbin/kadmind $heimdal_flags_kadmind &
                echo $! >$heimdal_pidfile_kadmind
                ;;
            kpasswdd )
                nohup @l_prefix@/sbin/kpasswdd $heimdal_flags_kpasswdd &
                echo $! >$heimdal_pidfile_kpasswdd
                ;;
            ipropd-master )
                nohup @l_prefix@/sbin/ipropd-master $heimdal_flags_ipropd_master &
                echo $! >$heimdal_pidfile_ipropd_master
                ;;
            ipropd-slave )
                nohup @l_prefix@/sbin/ipropd-slave $heimdal_flags_ipropd_slave \
                    `cat @l_prefix@/etc/heimdal/ipropd.master` &
                echo $! >$heimdal_pidfile_ipropd_slave
                ;;
        esac
    done

%stop -u @l_susr@
    rcService heimdal enable yes || exit 0
    rcService heimdal active no && exit 0
    heimdal_signal TERM
    sleep 5
    rm -f $heimdal_pidfile_kdc           2>/dev/null || true
    rm -f $heimdal_pidfile_kadmind       2>/dev/null || true
    rm -f $heimdal_pidfile_kpasswdd      2>/dev/null || true
    rm -f $heimdal_pidfile_ipropd_master 2>/dev/null || true
    rm -f $heimdal_pidfile_ipropd_slave  2>/dev/null || true

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

%daily -u @l_susr@
    rcService heimdal enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    for daemon in $heimdal_daemons; do
        if [ -f $heimdal_logdir/$daemon.log ]; then
            shtool rotate -f \
                -n $heimdal_log_numfiles -s $heimdal_log_minsize -d \
                -z $heimdal_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
                -P "$heimdal_log_prolog" \
                -E "$heimdal_log_epilog; echo 1 >$hintfile" \
                $heimdal_logdir/$daemon.log
        fi
    done
    if [ -s $hintfile ]; then
        rc heimdal restart
    fi
    rcTmp -k

