#! /bin/sh
#
# Copyright (c) 2001-2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# see if a run of modules.dep is needed and call depmod in this case
#
# options:
#    -r       use "uname -r" no matter what other kernel
#             might be installed in /boot
#    -v VERSION    use VERSION
# 
# will always fall back to the version from "uname -r" if something is missing

# search a directory tree recursively
#
find() {
    local dir=$1 glob=$2 file

    for file in $dir/*; do
	[ -d "$file" -a ! -L "$file" ] \
	    && find "$file" "$glob"
	eval "\
	    case \"${file##*/}\" in
	    $glob)  echo \"$file\" ;;
	    esac"
    done
}

# arguments
USE_RUNNING="false"
KERNEL_VERSION=""
if test "x$1" = "x-r" ; then
    USE_RUNNING="true"
fi

if test "x$1" = "x-v" ; then
    if test "x$2" != "x" ; then
	KERNEL_VERSION="$2"
    fi
fi

# find the kernel we need to run depmod for and it's modules
if test "$USE_RUNNING" = "false" -a "$KERNEL_VERSION" = "" ; then
    case `uname -m` in
	ppc)  KERNEL_FILENAME="vmlinux" ;;
	s390) KERNEL_FILENAME="image" ;;
	um) KERNEL_FILENAME="linux" ;;
	*)    KERNEL_FILENAME="vmlinuz" ;;
    esac
    KERNEL_VERSION=`/sbin/get_kernel_version /boot/$KERNEL_FILENAME 2>/dev/null`
fi

if test -n "$KERNEL_VERSION" -a -d /lib/modules/$KERNEL_VERSION -a -f /boot/System.map-$KERNEL_VERSION ; then
	MODULES_DIR=/lib/modules/$KERNEL_VERSION
	DEPMOD_PARAMS="-a -F /boot/System.map-$KERNEL_VERSION $KERNEL_VERSION"
else
	MODULES_DIR=/lib/modules/`uname -r`
	DEPMOD_PARAMS="-a"
fi

# now see if any modules are newer than the modules.dep file
# modprobe.conf no longer matters here, since it defines no additional load paths
if test -x /sbin/depmod -a -d $MODULES_DIR ; then
    for dir in $MODULES_DIR \
	       ${MODULES_DIR%%-*}-override-${MODULES_DIR##*-}; do
	# This is run at boot.  We can't rely on the existance of /usr .
	if test -x /usr/bin/find; then
	    if test -n "`/usr/bin/find $dir -newer $MODULES_DIR/modules.dep \( -name '*.o' -o -name '*.ko' \) 2>/dev/null`"; then
		rm -f $MODULES_DIR/modules.dep
	    fi
	else
	    for module in $(find $dir '*.ko|*.o'); do
		if test $module -nt $MODULES_DIR/modules.dep; then
		    rm -f $MODULES_DIR/modules.dep
		    break
		fi
	    done
	fi
    done
    if test ! -s $MODULES_DIR/modules.dep ; then
	rm -f $MODULES_DIR/modules.dep
    fi
    if test ! -e $MODULES_DIR/modules.dep ; then
        /sbin/depmod $DEPMOD_PARAMS > /dev/null 2>&1
    fi
fi

