#!/bin/bash
usage='search-sym SYMBOL'

if [ $# -ne 1 ];then
  echo "invalid arguments!"
  echo $usage
  exit 1
fi

sym=$1
egrep_pattern="^[0-9a-f]*[[:space:]]*[^U[:space:]].*$sym"
static_lib_pattern='*.a *.o'
shared_lib_pattern='*.so*'

function search_file() # filename nm-opt
{
  local f=$1
  shift
  local nmopt=$@
  local res
  if [ -f $f ];then
    res=`nm $nmopt $f 2>&1 | grep $sym`
    if [ -n "$res" ];then
      res=`printf -- "$res\n" | grep -E $egrep_pattern`
      if [ -n "$res" ];then
        echo "$f:"
        echo "$res"
      fi
    fi
  fi
}

echo "symbol $sym is included in..."
for f in $static_lib_pattern;do
  search_file $f -f bsd -C
done
for f in $shared_lib_pattern;do
  search_file $f -f bsd -C -D
done
