#!/bin/sh

# PROVIDE: mysqld_exporter
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# mysqld_exporter_enable (bool):          Set to NO by default.
#               Set it to YES to enable mysqld_exporter.
# mysqld_exporter_user (string):          Set user that mysqld_exporter will run under
#               Default is "nobody".
# mysqld_exporter_group (string):         Set group that mysqld_exporter will run under
#               Default is "nobody".
# mysqld_exporter_args (string):          Set extra arguments to pass to mysqld_exporter
#               Default is "".
# mysqld_exporter_listen_address (string):Set ip:port that mysqld_exporter will listen on
#               Default is ":9100".
# mysqld_exporter_conffile (string):      Set configuration file path for mysqld_exporter
#               Default is "".

. /etc/rc.subr

name=mysqld_exporter
rcvar=mysqld_exporter_enable

load_rc_config $name

: ${mysqld_exporter_enable:="NO"}
: ${mysqld_exporter_user:="nobody"}
: ${mysqld_exporter_group:="nobody"}
: ${mysqld_exporter_args:=""}
: ${mysqld_exporter_listen_address:=":9104"}
if [ -f "/usr/local/etc/mysqld_exporter/my.cnf" ]; then
: ${mysqld_exporter_conffile:="/usr/local/etc/mysqld_exporter/my.cnf"}
else
: ${mysqld_exporter_conffile:=""}
fi

if [ -z ${mysqld_exporter_conffile} ]; then
conf_arg=""
else
conf_arg="--config.my-cnf=${mysqld_exporter_conffile}"
fi

pidfile=/var/run/mysqld_exporter.pid
command="/usr/sbin/daemon"
procname="/usr/local/bin/mysqld_exporter"
command_args="-p ${pidfile} /usr/bin/env ${procname} \
    --web.listen-address=${mysqld_exporter_listen_address} \
    ${conf_arg} \
    ${mysqld_exporter_args}"

start_precmd=mysqld_exporter_startprecmd

mysqld_exporter_startprecmd()
{
    if [ ! -e ${pidfile} ]; then
        install -o ${mysqld_exporter_user} -g ${mysqld_exporter_group} /dev/null ${pidfile};
    fi
}

load_rc_config $name
run_rc_command "$1"
