#!/sbin/openrc-run

name="Zigbee2MQTT"

: ${command_user:="zigbee2mqtt"}
: ${start_wait=100}  # milliseconds
: ${retry="TERM/10/KILL/5"}

command="/usr/bin/zigbee2mqtt"
command_background="yes"
pidfile="/run/$RC_SVCNAME.pid"

start_stop_daemon_args="--wait $start_wait $start_stop_daemon_args"
# The leading space is to avoid fallback to $start_stop_daemon_args when this
# is empty (supervise-daemon doesn't support --wait).
supervise_daemon_args=" $supervise_daemon_args"

export ZIGBEE2MQTT_DATA="${datadir:="/var/lib/zigbee2mqtt"}"
export Z2M_ONBOARD_URL="${onboard_url:="http://127.0.0.1:8080"}"

depend() {
	need dev localmount
	use mosquitto
}

start_pre() {
	if ! [ -f /var/lib/zigbee2mqtt/configuration.yaml ]; then
		# Change the default log_output in the config file that will be
		# generated during onboarding.
		export ZIGBEE2MQTT_CONFIG_ADVANCED_LOG_OUTPUT='["console", "syslog"]'
	fi
	return 0
}

start_post() {
	if ! [ -f /var/lib/zigbee2mqtt/configuration.yaml ]; then
		ewarn "Open $onboard_url to configure Zigbee2MQTT."
		ewarn "You can change this URL in /etc/conf.d/zigbee2mqtt if needed."
	fi
	return 0
}
