#!/bin/sh /etc/rc.common

USE_PROCD=1
START=99

CONF="filebrowser"
PROG="/usr/bin/filebrowser"
DB_PATH="/etc/filebrowser/database.db"

start_service() {
	config_load "$CONF"

	local enabled
	config_get_bool enabled "config" "enabled" "0"
	[ "$enabled" -eq "1" ] || return 1

	mkdir -p "${DB_PATH%/*}"

	local listen_port root_path base_url disable_exec
	config_get listen_port "config" "listen_port" "8989"
	config_get root_path "config" "root_path" "/mnt/"
	config_get base_url "config" "base_url"
	config_get_bool disable_exec "config" "disable_exec" "1"

	procd_open_instance
	procd_set_param command "$PROG"
	procd_append_param command --database "$DB_PATH"
	procd_append_param command --address "[::]"
	procd_append_param command --port "$listen_port"
	procd_append_param command --root "$root_path"
	[ -z "$base_url" ] || procd_append_param command --baseurl "$base_url"
	[ "$disable_exec" = "0" ] || procd_append_param command --disable-exec

	procd_set_param limits core="unlimited"
	procd_set_param limits nofile="1000000 1000000"
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn

	procd_close_instance

}

service_triggers() {
	procd_add_reload_trigger "$CONF"
}
