#!/bin/sh
#
# PROVIDE: stalwart
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

name="stalwart"
rcvar="stalwart_enable"

load_rc_config stalwart

: ${stalwart_enable:="NO"}
: ${stalwart_user:="%%USERS%%"}
: ${stalwart_group:="%%GROUPS%%"}
: ${stalwart_config:="%%ETCDIR%%/config.toml"}
: ${stalwart_log:="/var/log/stalwart.log"}
: ${stalwart_path:="/var/db/stalwart"}

pidfile="/var/run/stalwart/server.pid"
procname="%%LOCALBASE%%/bin/stalwart"
start_cmd="stalwart_start"
start_precmd="stalwart_prestart"

stalwart_env="STALWART_PATH=${stalwart_path} RUN_AS_USER=${stalwart_user} RUN_AS_GROUP=${stalwart_group}"

stalwart_prestart() {
    if [ ! -f "${stalwart_log}" ]; then
        install -o ${stalwart_user} -g ${stalwart_group} -m 0640 /dev/null ${stalwart_log}
    fi

    if [ ! -d "/var/run/stalwart" ] ; then
        install -d -o ${stalwart_user} -g ${stalwart_group} "/var/run/stalwart"
    fi

    # if empty, RocksDB needs to be initialized
    if [ ! -d "${stalwart_path}" ] || [ -z "$(ls -A "${stalwart_path}" 2>/dev/null)" ]; then
        stalwart_init
    fi

}

stalwart_start()
{
    echo -n "Starting Stalwart: "

    /usr/sbin/daemon -p ${pidfile} \
        -T ${name} \
        -o ${stalwart_log} \
        ${procname} --config ${stalwart_config} \
        ${stalwart_flags}

    echo "done."
}

stalwart_init()
{
    mkdir -p ${stalwart_path}
    /usr/bin/install -d -o ${stalwart_user} -g ${stalwart_group} -m 0755 ${stalwart_path}
    # will tell user of a password it generated in the config we are throwing away
    /usr/bin/su -m ${stalwart_user} -c "${procname} -I ${stalwart_path}" 2> /dev/null

    # clean up autogenerated etc and logs directories we didn't want
    rm -f ${stalwart_path}/etc/config.toml
    rmdir ${stalwart_path}/etc
    rmdir ${stalwart_path}/logs
}

run_rc_command "$1"
