#!/bin/bash
set -e

# This script installs, enables, and restarts the auracast-server and auracast-frontend services
# Requires sudo privileges

# Ensure static link local is activated (for direct laptop connection)
# Enable link-local for all wired ethernet connections
while IFS=: read -r name type; do
    if [[ "$type" == *"ethernet"* ]]; then
        echo "Configuring connection: $name"
        # link-local: always enabled so direct-connect (no DHCP) works immediately
        sudo nmcli connection modify "$name" ipv4.link-local enabled 2>/dev/null || echo "Failed to modify $name"
        # may-fail=yes: do NOT tear down the connection when DHCP times out.
        # Without this, NM declares ip-config-unavailable after the 45s DHCP timeout
        # and enters a reconnect loop that causes ~1.5 min outages every ~45 seconds.
        sudo nmcli connection modify "$name" ipv4.may-fail yes 2>/dev/null || echo "Failed to set may-fail on $name"
        # Infinite DHCP timeout: NM keeps retrying DHCP in the background but never
        # declares ip-config-unavailable. This prevents the 45s reconnect loop that
        # kills the link-local address in direct-connect (no DHCP server) scenarios.
        sudo nmcli connection modify "$name" ipv4.dhcp-timeout infinity 2>/dev/null || echo "Failed to set dhcp-timeout on $name"
        sudo nmcli connection up "$name" 2>/dev/null || echo "Failed to bring up $name"
    fi
done < <(nmcli -t -f NAME,TYPE connection show)


# Remove stale avahi hosts pin — this file overrides per-interface advertisement
# and causes mDNS to always resolve to eth0's IP regardless of which interface
# the query arrived on, breaking eth1 mDNS entirely.
sudo rm -f /etc/avahi/hosts
sudo systemctl restart avahi-daemon

# Ensure Loopback is loaded with a fixed name and index 
# Needed for dante
# TODO image when we create the next image this should be part of it
echo "options snd-aloop index=6 id=Loopback pcm_substreams=6" | sudo tee /etc/modprobe.d/snd-aloop.conf
echo snd-aloop | sudo tee /etc/modules-load.d/snd-aloop.conf



# Install NetworkManager dispatcher script for link-local / Avahi management
sudo cp /home/caster/bumble-auracast/src/service/10-link-local-mgmt /etc/NetworkManager/dispatcher.d/10-link-local-mgmt
sudo chown root:root /etc/NetworkManager/dispatcher.d/10-link-local-mgmt
sudo chmod 755 /etc/NetworkManager/dispatcher.d/10-link-local-mgmt

# Copy system service file for DEP
sudo cp /home/caster/bumble-auracast/src/service/dep.service /etc/systemd/system/dep.service

# Copy system service file for frontend
sudo cp /home/caster/bumble-auracast/src/service/auracast-frontend.service /etc/systemd/system/auracast-frontend.service

# Copy user service file for backend
mkdir -p /home/caster/.config/systemd/user
cp /home/caster/bumble-auracast/src/service/auracast-server.service /home/caster/.config/systemd/user/auracast-server.service

# Reload systemd for frontend and dep
sudo systemctl daemon-reload
# Reload user systemd for server
systemctl --user daemon-reload

# Enable DEP to start on boot (system)
sudo systemctl enable dep.service
# Enable frontend to start on boot (system)
sudo systemctl enable auracast-frontend.service
# Enable server to start on boot (user)
systemctl --user enable auracast-server.service

# Restart all
sudo systemctl restart dep.service

sudo systemctl restart auracast-frontend.service
systemctl --user restart auracast-server.service

#print status
sudo systemctl status dep.service --no-pager
sudo systemctl status auracast-frontend.service --no-pager
systemctl --user status auracast-server.service --no-pager
