#!/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 ":9104".
# mysqld_exporter_config (string):        Set configuration file path for mysqld_exporter
#               Default is "%%PREFIX%%/etc/my.cnf".

. /etc/rc.subr

name=mysqld_exporter
desc="mysqld_exporter for use with Prometheus"
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"}
: ${mysqld_exporter_config:="%%PREFIX%%/etc/my.cnf"}

conf_file="--config.my-cnf=${mysqld_exporter_config}"

pidfile=/var/run/mysqld_exporter.pid
command="/usr/sbin/daemon"
procname="%%PREFIX%%/bin/mysqld_exporter"
command_args="-f -p ${pidfile} -T ${name} \
    /usr/bin/env ${procname} \
    --web.listen-address=${mysqld_exporter_listen_address} \
    ${conf_file} \
    ${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};
    else
        chown ${mysqld_exporter_user}:${mysqld_exporter_group} ${pidfile};
    fi
}

run_rc_command "$1"
