#!/usr/local/bin/bash

function compilable_check {
	/bin/mv -f result result.bak
	/bin/touch result
	for f in `cat result.bak`; do
		nm $f >& /dev/null
		if [ "$?" = "0" ]; then
			echo $f >> result
		fi
	done
	/bin/rm -f result.bak
}

function search_directories () {
	echo "/lib /usr/lib /usr/local/lib /usr/share/lib /usr/opt/lib"
	echo "/usr/X11/lib /usr/local/X11/lib /usr/share/X11/lib"
	mount|cut -d" " -f3
}

/bin/rm -f result
/bin/touch result

if [ -x "`which locate`" ]; then
	echo -n "Locating ${1}: " > /dev/tty
	locate lib${1}.a > result
	if [ "$?" = "0" ]; then
		compilable_check
		echo > /dev/tty
		awk '{print "    " $0}' result > /dev/tty
	else
		echo "not found" > /dev/tty
	fi
	exit 0
fi

list=`search_directories`

echo "Searching in the following directories: " > /dev/tty
for dir in $list; do
	echo "\t$dir"
done
child=no
for dir in $list; do
	echo -n "search in $dir [Y/n] " > /dev/tty
	read yes < /dev/tty
	if [ "$yes" = "" -or "$yes" = "y" -or "$yes" = "yes" ]; then
		echo $dir " started" > /dev/tty
		find $dir -depth -mount -type f -name lib${1}.a -print 2>&- >> result &
		child=yes
	fi
done

if [ $child = yes ]; then
	echo "waiting" > /dev/tty
	wait
	compilable_check
	echo "done" > /dev/tty
fi
