#!/bin/sh
# the kernel should mount devtmpfs stuff by itself so all this does is
# load the modules that apply to this hardware and fuck off so the
# device manager can pick up
#
# coldplugging doesn't happen here, mdevd-coldplug tells the kernel to reemit
# uevents, SYS/setup starts the mdevd service after this has run and the
# mdevd-coldplug oneshot waits for mdevd to be ready
. /usr/lib/nitro/rc.lib
case $1 in
up)
    log "loading modules for present devices..."
    find /sys/devices -name modalias -type f -exec cat {} + 2>/dev/null |
        sort -u | xargs -r modprobe -abq 2>/dev/null || :

    for f in /etc/modules-load.d/*.conf; do
        [ -f "$f" ] || continue
        while read -r mod _; do
            case $mod in ''|\#*) continue ;; esac
            printf '%s\n' "$mod"
        done < "$f"
    done | xargs -r modprobe -aq 2>/dev/null || :

    # mdevd reads the netlink itself so a hotplug helper left
    # behind by an initramfs needs to be cleaned up
    if [ -w /proc/sys/kernel/hotplug ]; then
        printf '\n' > /proc/sys/kernel/hotplug 2>/dev/null || :
    fi
    ;;
esac
exit 0
