#!/bin/bash
#
#set -x
#pidof vdr
#if [ "$?" != "0" ] ; then
#   echo "VDR laeuft nicht"
#   exit 1
#fi

ALL_DVBMOD="/tmp/~adm"
UNUSED_MOD="/tmp/~unused"
LIB_DIR="/lib/modules/"$(uname -r)"/kernel/drivers/media/dvb"
find $LIB_DIR -name *.ko | sed -e "s/.*\///" | cut -f 1 -d "." > $ALL_DVBMOD
cp /_config/bin/dvbmod-krnl /_config/bin/insmod.sh
lsmod | grep "  0 $" | cut -f 1 -d " " > $UNUSED_MOD
ADM=$(cat $ALL_DVBMOD)
CHANGED=1
while [ "$CHANGED" != "0" ] ; do
   CHANGED=0
   for i in $ADM ; do
      MN=$(echo $i | tr "-" "_")
      if [ "$(grep "^$MN$" $UNUSED_MOD)" != "" ] ; then
         echo "<$i> is unused - unloading"
         sed -i /_config/bin/insmod.sh -e "s/modprobe .*$i/# modprobe $i/" \
             -e "s/ $i / /" -e "s/ $i.ko / /" -e "s/\t$i /\t /" -e "s/\t$i.ko /\t /"
         rmmod $i
         CHANGED=1
      fi
   done
   lsmod | grep "  0 $" | cut -f 1 -d " " > $UNUSED_MOD
done


