#!/bin/sh

# PROVIDE: proxysql
# REQUIRE: LOGIN mysql
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# proxysql_enable (bool):          Set to NO by default.
#               Set it to YES to enable proxysql.
# proxysql_user (string):          Set user that proxysql will run under
#               Default is "proxysql".
# proxysql_group (string):         Set group that proxysql will run under
#               Default is "proxysql".
# proxysql_config (string):        Set path to config file
#               Default is "%%ETCDIR%%/proxysql.cfg".
# proxysql_args (string):          Set extra arguments to pass to proxysql
#               Default is "".

. /etc/rc.subr

name=proxysql
rcvar=proxysql_enable

load_rc_config $name

: ${proxysql_enable:="NO"}
: ${proxysql_command:="%%PREFIX%%/sbin/proxysql"}
: ${proxysql_user:="proxysql"}
: ${proxysql_group:="proxysql"}
: ${proxysql_config:="%%ETCDIR%%/proxysql.cfg"}
: ${proxysql_datadir:="/var/db/proxysql"}
: ${proxysql_args:=""}
: ${proxysql_restart_delay:="10"}

##### REMOVE daemon, allow proxysql to fork
pidfile="/var/run/proxysql/proxysql.pid"
command="%%PREFIX%%/sbin/proxysql"
command_args="-c ${proxysql_config} \
              -D ${proxysql_datadir} \
              --no-version-check ${proxysql_args}"
required_files="${proxysql_config}"
extra_commands="reload initial"
reload_cmd=proxysql_reload
initial_cmd=proxysql_initial

proxysql_initial()
{
    su -m ${proxysql_user} -c "${proxysql_command} ${command_args} --initial"
}

proxysql_reload()
{
    su -m ${proxysql_user} -c "${proxysql_command} ${command_args} --reload"
}

load_rc_config $name
run_rc_command "$1"
