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

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

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

%status -u @l_rusr@ -o
    bittorrent_usable="no"
    bittorrent_active="no"
    rcService bittorrent enable yes && \
        bittorrent_signal 0 && bittorrent_active="yes"
    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
    export HOME="$bittorrent_datadir"
    ( nohup @l_prefix@/bin/bittorrent-tracker \
        --port "$bittorrent_port" \
        --bind "$bittorrent_bind" \
        --dfile "$bittorrent_datafile" \
        --data_dir "$bittorrent_datadir" \
        --logfile "$bittorrent_logfile" \
        --pid "$bittorrent_pidfile" \
        --twisted 1 bittorrent_flags \
        </dev/null >/dev/null 2>&1 &
    ) >/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

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

%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

