#!/bin/bash
#
# NetworkManager dispatcher script to update nftables configuration
# when network interfaces change.

# Check if the script is called with correct parameters
if [ $# -lt 2 ]; then
    echo "Error: missing arguments" >&2
    exit 1
fi

INTERFACE="$1"
ACTION="$2"

# Log the event
logger -i -t "update-box-ips" "Interface $INTERFACE: $ACTION"

# Only act on important network events
case "$ACTION" in
    "up"|"dhcp4-change"|"dhcp6-change"|"connectivity-change")
        # Small delay to ensure IP assignments are complete
        sleep 2
        
        # Run the script to update box IPs
        if [ -x /usr/share/trutz-network/update-box-ips.sh ]; then
            logger -i -t "update-box-ips" "Updating box IPs due to network change on $INTERFACE"
            /usr/share/trutz-network/update-box-ips.sh
        else
            logger -i -t "update-box-ips" "Error: Update script not found or not executable"
        fi
        ;;
esac

exit 0