#!/bin/sh

# A quick script to search a statically linked executable, to see if
# it contains any of the given object files.

usage() {
   echo "Usage: $0 <binary> <dir>"
   echo Searches the given binary file for object files found in dir
   exit 1
}

binary=$1
dir=$2

if [ x == x$binary ] ; then
   usage
fi

if [ x == x$dir ] ; then
   usage
fi

if [ ! -f $binary ] ; then
   usage
fi

if [ ! -d $dir ] ; then
   usage
fi

if [ 0 == `find $dir -name '*.o' | wc -l` ] ; then
  if [ 0 != `find $dir -name '*.a' | wc -l` ] ; then
     echo Unpack the archive[s] in $dir then rerun
     exit 1
  else
     echo No object files found in $dir
     exit 0
  fi
fi

# Do the work

tmpfile=/tmp/ss.$$
rm -f $tmpfile
touch $tmpfile

for obj in $dir/*.o ; do
   bin/elfgrep -t $obj $binary >> $tmpfile
done

perl bin/elfgrep_fixup $tmpfile

rm -f $tmpfile
