#!/bin/sh /etc/rc.common
# procd init script for quicprochat server.
# Reads configuration from /etc/config/quicprochat (uci).

START=95
STOP=10
USE_PROCD=1

PROG=/usr/bin/qpc-server
DATA_DIR=/var/lib/quicprochat

start_service() {
    local listen data_dir log_level tls_cert tls_key production

    config_load quicprochat
    config_get listen server listen '0.0.0.0:7000'
    config_get data_dir server data_dir "$DATA_DIR"
    config_get log_level server log_level 'info'
    config_get tls_cert server tls_cert "${data_dir}/server-cert.der"
    config_get tls_key server tls_key "${data_dir}/server-key.der"
    config_get_bool production server production 1

    [ -d "$data_dir" ] || mkdir -p "$data_dir"

    procd_open_instance
    procd_set_param command "$PROG"

    procd_set_param env \
        RUST_LOG="$log_level" \
        QPC_LISTEN="$listen" \
        QPC_DATA_DIR="$data_dir" \
        QPC_TLS_CERT="$tls_cert" \
        QPC_TLS_KEY="$tls_key" \
        QPC_PRODUCTION="$production"

    procd_set_param respawn 3600 5 5
    procd_set_param stderr 1
    procd_set_param stdout 1
    procd_set_param user qpc
    procd_close_instance
}

service_triggers() {
    procd_add_reload_trigger "quicprochat"
}
