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

%config
    tty2web_enable="$openpkg_rc_def"
    tty2web_flags="-a 127.0.0.1 -p 8888 -w @l_prefix@/bin/tty2web-ssh localhost 127.0.0.1 tmux -l new-session -A -s tty2web"
    tty2web_log_prolog="true"
    tty2web_log_epilog="true"
    tty2web_log_numfiles="10"
    tty2web_log_minsize="1M"
    tty2web_log_complevel="9"

%common
    tty2web_logdir="@l_prefix@/var/tty2web/log"
    tty2web_pidfile="@l_prefix@/var/tty2web/run/tty2web.pid"
    tty2web_logfile="@l_prefix@/var/tty2web/log/tty2web.log"
    tty2web_signal () {
        [ -f $tty2web_pidfile ] && kill -$1 `cat $tty2web_pidfile`
    }

%status -u @l_rusr@ -o
    tty2web_usable="unknown"
    tty2web_active="no"
    rcService tty2web enable yes && \
        tty2web_signal 0 && tty2web_active="yes"
    echo "tty2web_enable=\"$tty2web_enable\""
    echo "tty2web_usable=\"$tty2web_usable\""
    echo "tty2web_active=\"$tty2web_active\""

%start -u @l_rusr@
    rcService tty2web enable yes || exit 0
    rcService tty2web active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup env -i \
            PATH="@l_prefix@/bin:@l_prefix@/sbin:/usr/bin:/usr/sbin:/bin:/sbin" \
            MANPATH="@l_prefix@/man:/usr/share/man" \
            TERM="xterm" \
            @l_prefix@/bin/tty2web \
                $tty2web_flags \
        </dev/null >>$tty2web_logfile 2>&1 &
        echo $! >$tty2web_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService tty2web enable yes || exit 0
    rcService tty2web active no  && exit 0
    tty2web_signal TERM
    sleep 2
    rm -f $tty2web_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService tty2web enable yes || exit 0
    shtool rotate -f \
        -n ${tty2web_log_numfiles} -s ${tty2web_log_minsize} -d \
        -z ${tty2web_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tty2web_log_prolog}" \
        -E "${tty2web_log_epilog}; rc tty2web restart" \
        $tty2web_logfile

