#!/bin/sh
# Only runs in native /etc and PROFINET nodes.

if initctl cond get boot/profinet; then
    initctl enable snmpd
    initctl disable lldpd
fi

# Factory default: all switch ports in VLAN 1 of br0
# need to check for 'length > 0' because ip command
# outputs empty json objects for non-port group ifs
ports=$(ip -json link show group port | jq -r '.[].ifname | select(length > 0)' | tr "\n" " ")
[ -f /etc/network/interfaces.d/br0 ] || [ -z "$ports" ] && exit 0

touch /etc/network/interfaces.d/br0
for port in $ports; do
    cat <<-EOF >>/etc/network/interfaces.d/br0
		iface $port
		    bridge-access 1
		    post-up ip link set $port group port
	EOF
done
cat <<-EOF >>/etc/network/interfaces.d/br0

	auto br0
	iface br0
	    bridge-ports $ports
	    bridge-vlan-aware yes
	    bridge-stp on
	    bridge-vids 1

	auto vlan1
	iface vlan1 inet dhcp
	    vlan-id 1
	    vlan-raw-device br0
	    post-up ip link set vlan1 group iface
EOF
ip link set vlan1 group iface
