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

%config
    sks_enable="$openpkg_rc_def"
    sks_db_flags=""
    sks_recon_flags=""
    sks_log_prolog="true"
    sks_log_epilog="true"
    sks_log_numfiles="10"
    sks_log_minsize="1M"
    sks_log_complevel="9"

%common
    sks_db_rundir="@l_prefix@/var/sks"
    sks_db_logfile="@l_prefix@/var/sks/db.log"
    sks_db_pidfile="@l_prefix@/var/sks/db.pid"
    sks_db_signal () {
        [ -f $sks_db_pidfile ] && kill -$1 `cat $sks_db_pidfile`
    }
    sks_recon_rundir="@l_prefix@/var/sks"
    sks_recon_logfile="@l_prefix@/var/sks/recon.log"
    sks_recon_pidfile="@l_prefix@/var/sks/recon.pid"
    sks_recon_signal () {
        [ -f $sks_recon_pidfile ] && kill -$1 `cat $sks_recon_pidfile`
    }

%status -u @l_rusr@ -o
    sks_usable="unknown"
    sks_active="no"
    rcService sks enable yes && \
        sks_db_signal 0 && sks_recon_signal 0 && sks_active="yes"
    echo "sks_enable=\"$sks_enable\""
    echo "sks_usable=\"$sks_usable\""
    echo "sks_active=\"$sks_active\""

%start -u @l_rusr@
    rcService sks enable yes || exit 0
    rcService sks active yes && exit 0
    ( cd $sks_db_rundir || exit $?
      cmd="@l_prefix@/sbin/sks"
      cmd="$cmd $sks_db_flags"
      cmd="$cmd db"
      eval "nohup $cmd </dev/null >/dev/null 2>&1 &"
      echo $! >$sks_db_pidfile
    ) >/dev/null 2>&1
    sleep 1
    ( cd $sks_recon_rundir || exit $?
      cmd="@l_prefix@/sbin/sks"
      cmd="$cmd $sks_recon_flags"
      cmd="$cmd recon"
      eval "nohup $cmd </dev/null >/dev/null 2>&1 &"
      echo $! >$sks_recon_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService sks enable yes || exit 0
    rcService sks active no  && exit 0
    sks_db_signal    TERM
    sks_recon_signal TERM
    sleep 2
    rm -f $sks_db_pidfile    >/dev/null 2>&1 || true
    rm -f $sks_recon_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService sks enable yes || exit 0
    shtool rotate -f \
        -n ${sks_log_numfiles} -s ${sks_log_minsize} -d \
        -z ${sks_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${sks_log_prolog}" \
        -E "${sks_log_epilog}; rc sks restart" \
        $sks_db_logfile
    shtool rotate -f \
        -n ${sks_log_numfiles} -s ${sks_log_minsize} -d \
        -z ${sks_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${sks_log_prolog}" \
        -E "${sks_log_epilog}; rc sks restart" \
        $sks_recon_logfile

