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

START=90
STOP=10

enabled="$(uci get baidupcs-web.config.enabled)"
port="$(uci get baidupcs-web.config.port)"
download_dir="$(uci get baidupcs-web.config.download_dir)"
max_download_rate="$(uci get baidupcs-web.config.max_download_rate || echo '0')"
max_upload_rate="$(uci get baidupcs-web.config.max_upload_rate || echo '0')"
max_download_load="$(uci get baidupcs-web.config.max_download_load || echo '1')"
max_parallel="$(uci get baidupcs-web.config.max_parallel || echo '8')"
aria2="$(uci get baidupcs-web.config.aria2)"
aria2_RPC="$(uci get baidupcs-web.config.aria2_RPC)"
aria2_secret="$(uci get baidupcs-web.config.aria2_secret)"

start() {
	stop
	[ "$enabled" == "1" ] || exit 0
	mkdir -p "${download_dir}"
	baidupcs-web config set                         \
	--savedir               "${download_dir}"       \
	--max_download_rate     "${max_download_rate}"  \
	--max_upload_rate       "${max_upload_rate}"    \
	--max_download_load     "${max_download_load}"  \
	--max_parallel          "${max_parallel}"       \
	--max_upload_parallel   "${max_parallel}"       \
	>/dev/null 2>&1
	[ $aria2 == '1' ] && {
	baidupcs-web -a -au "${aria2_RPC}" -as "${aria2_secret}" >/dev/null 2>&1 &
	} ||
	baidupcs-web web --port "${port}" --access >/dev/null 2>&1 &
}

stop() {
	killall baidupcs-web >/dev/null 2>&1
}
