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

%config
    gotty_enable="$openpkg_rc_def"
    gotty_flags="-a 127.0.0.1 -p 8080 -w @l_prefix@/bin/gotty-ssh localhost 127.0.0.1 tmux -l new-session -A -s gotty"
    gotty_log_prolog="true"
    gotty_log_epilog="true"
    gotty_log_numfiles="10"
    gotty_log_minsize="1M"
    gotty_log_complevel="9"

%common
    gotty_logdir="@l_prefix@/var/gotty/log"
    gotty_pidfile="@l_prefix@/var/gotty/run/gotty.pid"
    gotty_logfile="@l_prefix@/var/gotty/log/gotty.log"
    gotty_signal () {
        [ -f $gotty_pidfile ] && kill -$1 `cat $gotty_pidfile`
    }

%status -u @l_rusr@ -o
    gotty_usable="unknown"
    gotty_active="no"
    rcService gotty enable yes && \
        gotty_signal 0 && gotty_active="yes"
    echo "gotty_enable=\"$gotty_enable\""
    echo "gotty_usable=\"$gotty_usable\""
    echo "gotty_active=\"$gotty_active\""

%start -u @l_rusr@
    rcService gotty enable yes || exit 0
    rcService gotty 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/gotty \
                $gotty_flags \
        </dev/null >>$gotty_logfile 2>&1 &
        echo $! >$gotty_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService gotty enable yes || exit 0
    shtool rotate -f \
        -n ${gotty_log_numfiles} -s ${gotty_log_minsize} -d \
        -z ${gotty_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${gotty_log_prolog}" \
        -E "${gotty_log_epilog}; rc gotty restart" \
        $gotty_logfile

