#!/usr/bin/with-contenv bashio
# ==============================================================================
# Start Genie service
# ==============================================================================
export THINGENGINE_BASE_URL

# Set the Ingress URL as Genie base URL for correct handling
THINGENGINE_BASE_URL=$(bashio::addon.ingress_entry)

mkdir -p /data/almond-server
if [ ! -f /data/almond-server/prefs.db ]; then
    cp /etc/genie/default-prefs.db /data/almond-server/prefs.db
fi

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

# Register Home Assistant with Genie
./register &

# Start Genie
cd /opt/genie-server || bashio::exit.nok "Failed to change directory to Genie"
exec node dist/main.js
