#!/bin/bash

###############################################################################
# Script : list_loaded_kmod_dirs
# Purpose: List directory names housing the loaded Linux Kernel modules.
#          This list is used (in part) to identify the required modules for the
#          Slackware Installer and OS InitRD.
# Date...: 02-Jan-2024
# Author : Stuart Winter <mozes@slackware.com>
###############################################################################
# Ideas for usage:
# Boot the Slackware OS on each supported Hardware Model.
# $ DESTDIR=/tmp/kmod-copy
# $ kmoddirlist=slackwareaarch64-current/source/k/sources/kmod-dirlist
# $ mkdir -p $DESTDIR
# $ scripts/filter-kmod-dirs > /tmp/kmoddirlist
# Merge /tmp/kmoddirlist with $kmoddirlist
# Copy module directories from the lists:
# $ cd /lib/modules/6.1.69-armv8/kernel
# $ egrep -v '^#|^$' ${kmoddirlist} | tar pvvcf - -T- | tar -C${DESTDIR} -pxf -
# Slim down the modules:
# $ cd $DESTDIR
# $ . slackwareaarch64-current/source/k/scripts/slim-kmods-initrd
# Check total size of module repository.
###############################################################################

while read -r mod; do
     dirname $( /sbin/modinfo "$mod" | awk -F: '/^filename/{print $2}' | sed 's|/lib/modules/[0-9]\+\.[0-9]\+\.[0-9]\+-[a-zA-Z0-9]\+/kernel/||' )
#     echo $( /sbin/modinfo "$mod" | awk -F: '/^filename/{print $2}' | sed 's|/lib/modules/[0-9]\+\.[0-9]\+\.[0-9]\+-[a-zA-Z0-9]\+/kernel/||' )
done < <( awk '{print $1}' /proc/modules ) | sort | uniq

# To do:
# filter these from the list:
# sound
# bluetooth
