#!/usr/bin/with-contenv bashio
# ==============================================================================
# Start OpenZwave service for zwave radio
# ==============================================================================
OZW_CONFIG=/data/ozw/config
OZW_NETWORK_KEY=$(bashio::config 'network_key')
OZW_DEVICE=$(bashio::config 'device')
bashio::config.has_value 'instance' \
    && OZW_INSTANCE="$(bashio::config 'instance')" || OZW_INSTANCE=1

MQTT_PASSWORD="$(bashio::jq /data/auth.json ".ozw_password")"

# Wait until mosqitto is up and running
bashio::log.info "Starting OpenZwave..."
bashio::net.wait_for 1883

# Send out discovery information to Home Assistant
./discovery &

# Run daemon
export OZW_NETWORK_KEY
export MQTT_PASSWORD
exec ozwdaemon \
    -s "${OZW_DEVICE}" \
    --config-dir "${OZW_CONFIG}" \
    --user-dir "${OZW_CONFIG}" \
    --mqtt-server 127.0.0.1 \
    --mqtt-port 1883 \
    --mqtt-username ozw \
    --mqtt-instance "${OZW_INSTANCE}" \
    --stop-on-failure
