#!/bin/sh

# A quick script to search a statically linked executable, to see if
# it contains any of the given object files.

# Original version by Dion Mendel, 2002
# Modified to run under Cygwin (and hopefully other unix or
# unix-like systems) by Will Dyke, 2002.

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 [ -z $binary ] ; then
   usage
fi

if [ -z $dir ] ; then
   usage
fi

if [ ! -f $binary ] ; then
   usage
fi

if [ ! -d $dir ] ; then
   usage
fi

if [ 0 -eq `find $dir -name '*.o' | wc -l` ] ; then
  if [ 0 -ne `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


