#!/bin/sh /etc/rc.common
# shellcheck disable=SC2039
# shellcheck disable=SC3043

# shellcheck disable=SC2034
{
	START=99
	USE_PROCD=1
}
PROG="/usr/sbin/openwisp_config"
PROG_NAME="OpenWISP config agent"
CONTROL_FILE=/tmp/openwisp/applying_conf

add_option() {
	local cfg="$1"
	local flag="$2"
	local option="$3"
	local default="$4"
	local value
	config_get value "$cfg" "$option" "$default"
	[ -n "$value" ] && procd_append_param command "$flag" "$value"
}

parse_config() {
	local cfg="$1"
	add_option "$cfg" "--url" url
	add_option "$cfg" "--interval" interval
	add_option "$cfg" "--management_interval" management_interval
	add_option "$cfg" "--registration_interval" registration_interval
	add_option "$cfg" "--verify-ssl" verify_ssl
	add_option "$cfg" "--uuid" uuid
	add_option "$cfg" "--key" key
	add_option "$cfg" "--shared-secret" shared_secret
	add_option "$cfg" "--consistent-key" consistent_key
	add_option "$cfg" "--hardware-id-script" hardware_id_script
	add_option "$cfg" "--hardware-id-key" hardware_id_key
	add_option "$cfg" "--bootup-delay" bootup_delay
	add_option "$cfg" "--merge-config" merge_config
	add_option "$cfg" "--test-config" test_config
	add_option "$cfg" "--test-retries" test_retries
	add_option "$cfg" "--test-script" test_script
	add_option "$cfg" "--connect-timeout" connect_timeout
	add_option "$cfg" "--max-time" max_time
	add_option "$cfg" "--capath" capath
	add_option "$cfg" "--cacert" cacert
	add_option "$cfg" "--mac-interface" mac_interface
	add_option "$cfg" "--management-interface" management_interface
	add_option "$cfg" "--default-hostname" default_hostname
	add_option "$cfg" "--pre-reload-hook" pre_reload_hook
	add_option "$cfg" "--post-reload-hook" post_reload_hook
	add_option "$cfg" "--post-reload-delay" post_reload_delay
	add_option "$cfg" "--post-registration-hook" post_registration_hook
	add_option "$cfg" "--checksum-max-retries" checksum_max_retries
	add_option "$cfg" "--checksum-retry-delay" checksum_retry_delay
}

start_service() {
	config_load openwisp
	respawn_threshold=$(config_get http respawn_threshold)
	respawn_timeout=$(config_get http respawn_timeout)
	respawn_retry=$(config_get http respawn_retry)
	# @todo This should be changed to a list element.
	# So that we no longer need this workaround. And so we could also use
	# the new add_option function. However, the agent must also be changed!
	unmanaged=$(config_get http unmanaged)
	if [ -n "$unmanaged" ]; then
		# replace spaces with commas to avoid problems when
		# passing this arg to procd_set_param command
		unmanaged=$(echo "$unmanaged" | tr ' ' ',')
		unmanaged="--unmanaged $unmanaged"
	fi
	procd_open_instance
	# shellcheck disable=SC2086
	procd_set_param command $PROG $unmanaged
	config_foreach parse_config controller
	procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
	procd_close_instance
	logger -s "$PROG_NAME started" -t openwisp -p daemon.info
}

service_triggers() {
	procd_add_reload_trigger openwisp
}

stop_service() {
	logger -s "$PROG_NAME stopping" -t openwisp -p daemon.info
}

reload_service() {
	logger -s "$PROG_NAME received reload trigger" -t openwisp -p daemon.info
	# avoid reloading while configuration is being applied
	# will wait for a maximum of 30 seconds
	for _ in $(seq 1 30); do
		if [ -f "$CONTROL_FILE" ]; then
			sleep 1
		else
			break
		fi
	done
	rm -f "$CONTROL_FILE"
	start
}
