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

%config
    minio_enable="$openpkg_rc_def"
    minio_flags=""
    minio_log_prolog="true"
    minio_log_epilog="true"
    minio_log_numfiles="10"
    minio_log_minsize="1M"
    minio_log_complevel="9"

%common
    minio_dbdir="@l_prefix@/var/minio/db"
    minio_pidfile="@l_prefix@/var/minio/run/minio.pid"
    minio_logfile="@l_prefix@/var/minio/log/minio.log"
    minio_signal () {
        [ -f $minio_pidfile ] && kill -$1 `cat $minio_pidfile`
    }

%status -u @l_rusr@ -o
    minio_usable="unknown"
    minio_active="no"
    rcService minio enable yes && \
        minio_signal 0 && minio_active="yes"
    echo "minio_enable=\"$minio_enable\""
    echo "minio_usable=\"$minio_usable\""
    echo "minio_active=\"$minio_active\""

%start -u @l_rusr@
    rcService minio enable yes || exit 0
    rcService minio active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        . @l_prefix@/etc/minio/minio.cf
        export MINIO_ACCESS_KEY
        export MINIO_SECRET_KEY
        HOME="$minio_dbdir/cfg"
        export HOME
        cmd="nohup @l_prefix@/bin/minio"
        cmd="$cmd --config-dir $minio_dbdir/cfg"
        cmd="$cmd server"
        cmd="$cmd $minio_flags"
        cmd="$cmd --address $MINIO_LISTEN_ADDRESS"
        cmd="$cmd $MINIO_STORAGE_PATHS"
        $cmd </dev/null >>$minio_logfile 2>&1 &
        echo $! >$minio_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService minio enable yes || exit 0
    shtool rotate -f \
        -n ${minio_log_numfiles} -s ${minio_log_minsize} -d \
        -z ${minio_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${minio_log_prolog}" \
        -E "${minio_log_epilog}; rc minio restart" \
        $minio_logfile

