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

START=90
STOP=10

USE_PROCD=1

PROG=/usr/bin/syncthing

config_cb() {
	[ $# -eq 0 ] && return

	option_cb() {
		local option="$1"
		local value="$2"

		case $option in

		# Support old option names
		logfile)
			option='log_file'
			;;

		macprocs)
			option='maxprocs'
			;;

		esac

		eval $option="$value"
	}
}

service_triggers() {
	procd_add_reload_trigger 'syncthing'
}

start_service() {
	# Options with default value different with the syncthing should be defined
	# explicitly here
	local enabled=0
	local db_delete_retention_interval=''
	local db_maintenance_interval=''
	local gui_address='http://0.0.0.0:8384'
	local gui_apikey=''
	local home='/etc/syncthing'
	local log_file='/var/log/syncthing.log'
	local log_level='INFO'
	local log_max_old_files=7
	local log_max_size=1048576
	local maxprocs=0
	local nice=0
	local user='syncthing'

	config_load 'syncthing'

	local group=$(id -gn $user)

	[ "$enabled" -gt 0 ] || return 0

	mkdir -p "$home"
	# A separate step to handle an upgrade use case
	[ -d "$home" ] && chown -R $user:$group "$home"

	# Changes to "niceness"/maxprocs are not picked up either by reload_config
	# or by restart: the service has to be stopped/started for it to take effect
	if [ $maxprocs -le 0 ]; then
		# Default to the number of cores in this case
		maxprocs=$(grep -c ^processor /proc/cpuinfo)
	fi

	procd_open_instance
	procd_set_param command "$PROG"
	procd_set_param env GOMAXPROCS="$maxprocs"
	procd_append_param command serve
	[ -z "$db_delete_retention_interval" ] || \
		procd_append_param command --db-delete-retention-interval="$db_delete_retention_interval"
	[ -z "$db_maintenance_interval" ] || \
		procd_append_param command --db-maintenance-interval="$db_maintenance_interval"
	procd_append_param command --gui-address="$gui_address"
	[ -z "$gui_apikey" ] || \
		procd_append_param command --gui_apikey="$gui_apikey"
	procd_append_param command --home="$home"
	procd_append_param command --log-file="$log_file"
	[ -z "$log_level" ] || \
		procd_append_param command --log-level="$log_level"
	[ -z "$log_max_old_files" ] || \
		procd_append_param command --log-max-old-files="$log_max_old_files"
	[ -z "$log_max_size" ] || \
		procd_append_param command --log-max-size="$log_max_size"
	procd_append_param command --no-browser
	procd_append_param command --no-port-probing
	procd_append_param command --no-restart
	# The package is built with noupgrade tag so --no-upgrade is not necessary

	procd_set_param nice "$nice"
	procd_set_param term_timeout 15
	procd_set_param user "$user"
	procd_set_param group "$group"
	procd_set_param respawn
	procd_set_param stdout 0
	procd_set_param stderr 1
	procd_close_instance
}

reload_service() {
	stop
	start
}
