#!/bin/sh

TCEDIR="/etc/sysconfig/tcedir"
SCEDIR="/etc/sysconfig/tcedir/sce"
KERELVERSION=`uname -r`

[ -d /tmp/sce-modulecheck ] || mkdir -p /tmp/sce-modulecheck

cd "$SCEDIR"

if [ ! "$NOLOCK" == "TRUE" ]; then
	if [ -f /tmp/.scelock ]; then
		LOCK=`cat /tmp/.scelock`
		if /bb/ps -o pid | sed -e 's/^[ \t]*//'| grep "^$LOCK$" | /bb/grep -v grep > /dev/null 2>&1; then
			echo "${YELLOW}Warning:${NORMAL} Another SCE utility is presently in use, exiting.."
			exit 1
		fi
	fi
	
	echo "$$" > /tmp/.scelock
fi

> /tmp/scemodules


for I in `ls *.sce`; do
	SCENAME=`basename "$I" .sce`
	echo "Checking for kernel modules in $I..."
	sudo mount "$I" /tmp/sce-modulecheck > /dev/null 2>&1
	if [ -d /tmp/sce-modulecheck/lib/modules/*tinycore* ] || [ -d /tmp/sce-modulecheck/usr/local/lib/modules/*tinycore* ]; then
		echo "$SCENAME:" >> /tmp/scemodules
	else
		sudo umount /tmp/sce-modulecheck
		continue
	fi
	if find /tmp/sce-modulecheck/lib/modules/*tinycore* -name *.ko* > /dev/null 2>&1; then
		echo " `find /tmp/sce-modulecheck/lib/modules/ -maxdepth 1 | tail -n1 | sed 's:/tmp/sce-modulecheck::g'`" >> /tmp/scemodules
	fi
	if find /tmp/sce-modulecheck/usr/local/lib/modules/*tinycore* -name *.ko* > /dev/null 2>&1; then
		echo  " `find /tmp/sce-modulecheck/usr/local/lib/modules/ -maxdepth 1 | tail -n1 | sed 's:/tmp/sce-modulecheck::g'`" >> /tmp/scemodules
	fi
	echo " " >> /tmp/scemodules
	echo " " >> /tmp/scemodules
	sudo umount /tmp/sce-modulecheck > /dev/null 2>&1
	sleep 3
done

echo " "
echo "Check for kernel modules in SCEs completed."
echo "Below contain kernel modules with the module paths listed below."
echo "Results saved in /tmp/scemodules."
echo " "
cat  /tmp/scemodules
echo " "



