#!/sbin/openrc-run

# SPDX-FileCopyrightText: 2025 famfo
# SPDX-FileContributor: famfo
#
# SPDX-License-Identifier: MIT

# The default config file, log file and log_level can be overrideden by setting
# the respective variables (CONF, LOG_FILE and RUST_LOG) in conf.d/iocaine
#
# Note: this init script assums that an iocaine user and group exists

config_file="${CONF:-/etc/iocaine/config.kdl}"
log_file="${LOG_FILE:-/var/log/iocaine.log}"
log_level="${RUST_LOG:-warn}"

name="iocaine"
supervisor="supervise-daemon"
command="iocaine"
command_args="-c $config_file start"
extra_commands="checkconfig"
output_log="$log_file"
error_log="$log_file"
supervise_daemon_args="-e RUST_LOG=$log_level"

command_user="iocaine"
command_group="iocaine"

depend() {
        use net
        after firewall
}

start_pre() {
        if [[ "${RC_CMD}" != "restart" ]]; then
                checkconfig
                checkpath --file --owner "$command_user:$command_group" --mode 0640 "$log_file"
        fi
}

stop_pre() {
        if [[ "${RC_CMD}" == "restart" ]]; then
                checkconfig
        fi
}

checkconfig() {
        ebegin "Checking iocaine config $config_file"
        "$command" -c "$config_file" show config 1> /dev/null
        eend "$?"
}

