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

%config
    mysqlproxy_enable="$openpkg_rc_def"
    mysqlproxy_addr="127.0.0.1"
    mysqlproxy_port="4040"
    mysqlproxy_log_prolog="true"
    mysqlproxy_log_epilog="true"
    mysqlproxy_log_numfiles="10"
    mysqlproxy_log_minsize="1M"
    mysqlproxy_log_complevel="9"
    mysqlproxy_logfile="@l_prefix@/var/mysqlproxy/mysqlproxy.log"

%common
    mysqlproxy_inifile="@l_prefix@/etc/mysqlproxy/mysqlproxy.ini"
    mysqlproxy_luafile="@l_prefix@/etc/mysqlproxy/mysqlproxy.lua"
    mysqlproxy_pidfile="@l_prefix@/var/mysqlproxy/mysqlproxy.pid"
    mysqlproxy_signal () {
        [ -f $mysqlproxy_pidfile ] && kill -$1 `cat $mysqlproxy_pidfile`
    }

%status -u @l_susr@ -o
    mysqlproxy_usable="unknown"
    mysqlproxy_active="no"
    rcService mysqlproxy enable yes && \
        mysqlproxy_signal 0 && mysqlproxy_active="yes"
    echo "mysqlproxy_enable=\"$mysqlproxy_enable\""
    echo "mysqlproxy_usable=\"$mysqlproxy_usable\""
    echo "mysqlproxy_active=\"$mysqlproxy_active\""

%start -u @l_susr@
    rcService mysqlproxy enable yes || exit 0
    rcService mysqlproxy active yes && exit 0
    @l_prefix@/bin/mysql-proxy \
        --daemon \
        --user=@l_nusr@ \
        --log-file=${mysqlproxy_logfile} \
        --pid-file=${mysqlproxy_pidfile} \
        --proxy-lua-script=${mysqlproxy_luafile} \
        --defaults-file=${mysqlproxy_inifile}

%stop -u @l_susr@
    rcService mysqlproxy enable yes || exit 0
    rcService mysqlproxy active no  && exit 0
    mysqlproxy_signal TERM
    sleep 2
    rm -f $mysqlproxy_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService mysqlproxy enable yes || exit 0
    shtool rotate -f \
        -n ${mysqlproxy_log_numfiles} -s ${mysqlproxy_log_minsize} -d \
        -z ${mysqlproxy_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${mysqlproxy_log_prolog}" \
        -E "${mysqlproxy_log_epilog}; rc mysqlproxy restart" \
        $mysqlproxy_logfile

