#!/bin/bash

# Generate modules.{networking, block, drm, modesetting}
# lists for further usage
# Adapted from Fedora
#
# Ozan Caglayan (C) 2010

collect_modules_list()
{
  sed -r -n -e "s/^([^ ]+) \\.?($2)\$/\\1/p" $TMP_MODUNDEF |
  LC_ALL=C sort -u > modules.$1
}

MOD_DIR=$1
TMP_MODNAMES=`mktemp`
TMP_MODUNDEF=`mktemp`

find $MOD_DIR -name "*.ko" -type f > $TMP_MODNAMES

cat $TMP_MODNAMES | xargs nm -upA |
    sed -n 's,^.*/\([^/]*\.ko\):  *U \(.*\)$,\1 \2,p' > $TMP_MODUNDEF

# Start collecting module lists
# based on undefined symbols

cd $MOD_DIR

collect_modules_list networking \
    'register_netdev|ieee80211_register_hw|usbnet_probe|phy_driver_register'
collect_modules_list block \
    'ata_scsi_ioctl|scsi_add_host|scsi_add_host_with_dma|blk_init_queue|register_mtd_blktrans|scsi_esp_register|scsi_register_device_handler'
collect_modules_list drm \
    'drm_open|drm_init'
collect_modules_list modesetting \
    'drm_crtc_init'

# Clean temporary files
rm -f $TMP_MODNAMES $TMP_MODUNDEF
