#!/bin/sh /etc/rc.common
# Copyright (C) 2021 Tianling Shen <cnsztl@immortalwrt.org>

USE_PROCD=1
START=99

CONF="go-aliyundrive-webdav"
PROG="/usr/bin/go-aliyundrive-webdav"

start_service() {
	config_load "$CONF"

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

	local port rt path user pwd verbose
	config_get port "config" "port"
	config_get rt "config" "rt"
	config_get path "config" "path"
	config_get user "config" "user"
	config_get pwd "config" "pwd"
	config_get_bool verbose "config" "verbose"

	procd_open_instance "$CONF"

	procd_set_param command "$PROG"

	procd_append_param command "-rt" "$rt"
	procd_append_param command "-port" "$port"
	procd_append_param command "-path" "$path"
	procd_append_param command "-user" "$user"
	procd_append_param command "-pwd" "$pwd"
	[ "$verbose" -eq "0" ] || procd_append_param command "-v"

	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param respawn

	procd_close_instance
}

reload_service() {
	stop
	start
}

service_triggers() {
	procd_add_reload_trigger "go-aliyundrive-webdav"
}
