#!/bin/bash
set -e

echo "Reloading BCM kernel modules..."

#depmod -a

# List of devices that must exist
devices=(
    "/dev/linux-kernel-bde c 127 0"
    "/dev/linux-user-bde   c 126 0"
    "/dev/linux-bcm-knet   c 122 0"
    "/dev/linux-bcm-net    c 123 0"
    "/dev/linux-knet-cb    c 121 0"
)

# Checking each device
for dev in "${devices[@]}"; do
    path=$(echo "$dev" | awk '{print $1}')
    major=$(echo "$dev" | awk '{print $3}')
    minor=$(echo "$dev" | awk '{print $4}')

    if [ ! -e "$path" ]; then
        echo "Creating device node: $path"
        /bin/mknod "$path" c "$major" "$minor"
    else
        echo "Device node already exists: $path"
    fi
done


# unload derivers 
rmmod linux-user-bde linux-bcm-knet linux-kernel-bde linux-knet-cb || true

# Downloading new driver instances
insmod /lib/modules/$(uname -r)/linux-kernel-bde.ko dmasize=64M maxpayload=256 debug=1 dma_debug=1 usemsi=1 dmaalloc=0
insmod /lib/modules/$(uname -r)/linux-bcm-knet.ko use_rx_skb=1 rx_buffer_size=9238 debug=0x5020 default_mtu=9100
insmod /lib/modules/$(uname -r)/linux-user-bde.ko
insmod /lib/modules/$(uname -r)/linux-knet-cb.ko debug=7 

echo "BCM kernel modules reloaded."
