#!@l_prefix@/lib/openpkg/bash @l_prefix@/etc/rc
##
##  rc.bittorrent -- Run-Commands
##

%config
    bittorrent_enable="$openpkg_rc_def"
    bittorrent_port="6969"
    bittorrent_log_prolog="true"
    bittorrent_log_epilog="true"
    bittorrent_log_numfiles="10"
    bittorrent_log_minsize="1M"
    bittorrent_log_complevel="9"

%common
    bittorrent_datafile="@l_prefix@/var/bittorrent/bttrack.data"
    bittorrent_logfile="@l_prefix@/var/bittorrent/bttrack.log"
    bittorrent_pidfile="@l_prefix@/var/bittorrent/bttrack.pid"
    bittorrent_signal () {
        [ -f $bittorrent_pidfile ] \
        && kill -$1 `cat $bittorrent_pidfile`
    }

%status -u @l_susr@ -o
    bittorrent_usable="no"
    bittorrent_active="no"
    echo "bittorrent_enable=\"$bittorrent_enable\""
    echo "bittorrent_usable=\"$bittorrent_usable\""
    echo "bittorrent_active=\"$bittorrent_active\""

%start -u @l_rusr@
    rcService bittorrent enable yes || exit 0
    rcService bittorrent active yes && exit 0
    ( nohup @l_prefix@/bin/bttrack \
          --port $bittorrent_port \
          --dfile $bittorrent_datafile \
          --logfile $bittorrent_logfile \
          </dev/null >/dev/null 2>&1 &
      echo $! >$bittorrent_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService bittorrent enable yes || exit 0
    rcService bittorrent active no  && exit 0
    bittorrent_signal TERM
    sleep 2
    rm -f $bittorrent_pidfile 2>/dev/null || true

%daily -u @l_susr@
    rcService bittorrent enable yes || exit 0
    shtool rotate -f \
        -n ${bittorrent_log_numfiles} -s ${bittorrent_log_minsize} -d \
        -z ${bittorrent_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${bittorrent_log_prolog}" \
        -E "${bittorrent_log_epilog}; rc bittorrent restart" \
        $bittorrent_logfile

