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

USE_PROCD=1
APP=/usr/sbin/mosquitto
CONFIG=/tmp/mosquitto.conf
PSK_FILE=/tmp/mosquitto.psk
START=99
STOP=01

handle_port() {
	echo "listener $1" >>$CONFIG
}

setup_config() {
	config_load mosquitto
	local section_name="mqtt"
	config_get enabled "$section_name" enabled "0"

	if [ "$enabled" == "1" ]; then

		config_get custom_enabled "$section_name" custom_enabled "0"
		if [ "$custom_enabled" == "1" ]; then
			rm "$CONFIG"
			config_get CONFIG "$section_name" custom_section_id
			return 1
		fi

		config_list_foreach "$section_name" local_port handle_port

		config_get acl_file_path "$section_name" acl_file_path
		config_get password_file "$section_name" password_file

		config_get use_tls_ssl "$section_name" use_tls_ssl
		config_get tls_type "$section_name" tls_type

		config_get ca_file "$section_name" ca_file
		config_get cert_file "$section_name" cert_file
		config_get key_file "$section_name" key_file
		config_get tls_version "$section_name" tls_version

		config_get psk "$section_name" psk
		config_get identity "$section_name" identity

		config_get persistence "$section_name" persistence
		config_get anonymous_access "$section_name" anonymous_access

		config_get max_queued_messages "$section_name" max_queued_messages
		config_get max_packet_size "$section_name" max_packet_size "1048576"
		config_get require_certificate "$section_name" require_certificate "1"
	fi

}

read_broker_config() {
	local section_id="$1"
	config_get client_enabled "$section_id" client_enabled

	if [ "$client_enabled" == "1" ]; then
		config_get connection_name "$section_id" connection_name
		config_get remote_addr "$section_id" remote_addr
		config_get remote_port "$section_id" remote_port

		config_get use_bridge_login "$section_id" use_bridge_login
		config_get remote_username "$section_id" remote_username
		config_get remote_password "$section_id" remote_password
		config_get remote_clientid "$section_id" remote_clientid

		config_get use_remote_tls "$section_id" use_remote_tls

		config_get bridge_ca_file "$section_id" bridge_cafile
		config_get bridge_cert_file "$section_id" bridge_certfile
		config_get bridge_key_file "$section_id" bridge_keyfile
		config_get bridge_tls_version "$section_id" bridge_tls_version
		config_get bridge_insecure "$section_id" bridge_insecure
		config_get bridge_protocol_version "$section_id" bridge_protocol_version
		config_get bridge_alpn "$section_id" bridge_alpn

		config_get try_private "$section_id" try_private
		config_get cleansession "$section_id" cleansession
		config_get notifications "$section_id" notifications
		config_get notifications_local "$section_id" notifications_local

		if [ "$notifications" == "1" ]; then notifications="true"; else notifications="false"; fi
		if [ "$notifications_local" == "1" ]; then notifications_local="true"; else notifications_local="false"; fi

		config_get keepalive_interval "$section_id" keepalive_interval
	fi
}

set_topic() {
	local name="$2"
	local topic direction qos connection_name

	config_get connection_name $1 connection_name '""'

	if [ "$connection_name" == "$name" ]; then
		config_get topic $1 topic '""'
		config_get direction $1 direction '""'
		config_get qos $1 qos '""'
		echo "topic $topic $direction $qos" >>$CONFIG
	fi
}

find_topics() {
	export found_topics=1
}

fill_mosquitto_bridge_config() {
	local section_id="$1"
	read_broker_config "$section_id"
	if [ "$client_enabled" == "1" ]; then
		echo "" >>$CONFIG
		echo "connection $connection_name" >>$CONFIG
		echo "address $remote_addr:$remote_port" >>$CONFIG
		echo "notifications $notifications" >>$CONFIG
		echo "notifications_local_only $notifications_local" >>$CONFIG
		echo "bridge_protocol_version $bridge_protocol_version" >>$CONFIG

		# Use TLS/SSL for bridge connection
		if [ "$use_remote_tls" == "1" ]; then
			[ -n "$bridge_ca_file" ] && echo "bridge_cafile $bridge_ca_file" >>$CONFIG
			[ -n "$bridge_cert_file" ] && echo "bridge_certfile $bridge_cert_file" >>$CONFIG
			[ -n "$bridge_key_file" ] && echo "bridge_keyfile $bridge_key_file" >>$CONFIG
			[ -n "$bridge_alpn" ] && echo "bridge_alpn $bridge_alpn" >>$CONFIG
			[ -n "$bridge_tls_version" ] && echo "bridge_tls_version $bridge_tls_version" >>$CONFIG
			if [ "$bridge_insecure" = "1" ]; then
				echo "bridge_insecure true" >>$CONFIG
			fi
		fi

		# Use remote login
		if [ "$use_bridge_login" == "1" ]; then
			echo "remote_clientid $remote_clientid" >>$CONFIG
			[ -n "$remote_username" ] && {
				echo "remote_username $remote_username" >>$CONFIG
				[ -n  "$remote_password" ] && {
					echo "remote_password $remote_password" >>$CONFIG
				}
			}
		fi

		# Set other options
		if [ "$try_private" != "1" ]; then
			echo "try_private false" >>$CONFIG
		fi

		if [ "$cleansession" == "1" ]; then
			echo "cleansession true" >>$CONFIG
		fi

		# Set topic (-s)
		#echo $topic | sed -e 's/[ \t]*;[ \t]*/\n/g' | sed -e '/^[^ \t]/s/^/topic /g' >> $CONFIG
		config_foreach set_topic "topic" "$connection_name"

		[ -n "$keepalive_interval" ] && echo "keepalive_interval $keepalive_interval" >> $CONFIG
	fi
}

fill_mosquitto_config() {

	echo "user root" >>$CONFIG

	# Use access control list
	if [ -n "$acl_file_path" ]; then
		echo "acl_file $acl_file_path" >>$CONFIG
	fi

	# Use TLS/SSL for connection
	if [ "$use_tls_ssl" == "1" ]; then
		if [ "$tls_type" == "cert" ]; then
			[ -n "$ca_file" ] && echo "cafile $ca_file" >>$CONFIG
			[ -n "$cert_file" ] && echo "certfile $cert_file" >>$CONFIG
			[ -n "$key_file" ] && echo "keyfile $key_file" >>$CONFIG

			if [ "$tls_version" != "all" ]; then
				echo "tls_version $tls_version" >>$CONFIG
			fi
			echo "require_certificate $require_certificate" >> $CONFIG
		elif [ "$tls_type" == "psk" ]; then
			echo "$identity:$psk" >$PSK_FILE
			echo "psk_file $PSK_FILE" >>$CONFIG
			echo "psk_hint $(mnf_info -s)" >>$CONFIG
		fi
	fi

	if [ "$persistence" == "1" ]; then
		echo "persistence true" >>$CONFIG
	fi

	if [ -n "$password_file" ]; then
		echo "password_file $password_file" >>$CONFIG
	fi

	if [ "$anonymous_access" == "1" ]; then
		echo "allow_anonymous true" >>$CONFIG
	else
		echo "allow_anonymous false" >>$CONFIG
	fi

	[ -n "$max_queued_messages" ] && echo "max_queued_messages $max_queued_messages" >> $CONFIG

	[ -n "$max_packet_size" ] && [ "$max_packet_size" -ne 0 ] && echo "max_packet_size $max_packet_size" >> $CONFIG

	config_foreach find_topics "topic"
	[ -z $found_topics ] && return 0
	# Connect to other broker if client is enabled
	config_foreach fill_mosquitto_bridge_config "bridge"
}

start_service() {
	echo "" >$CONFIG
	setup_config
	if [ "$enabled" == "1" ]; then
		[ "$custom_enabled" == "0" ] && fill_mosquitto_config

		procd_open_instance
		procd_set_param file /etc/config/mosquitto
		procd_set_param command $APP -c $CONFIG
		procd_set_param respawn ${respawn_threshold:-2} ${respawn_timeout:-10} ${respawn_retry:-3}
		procd_close_instance
	fi
}

service_triggers() {
	procd_add_reload_trigger "mosquitto"
}
