#!/bin/bash
#
# by Phil2k@gmail.com
#  v0.5

paths=("/bin" "/usr/bin" "/usr/sbin" "/sbin" "/usr/local/bin" "/usr/local/sbin" "/lib" "/usr/lib" "/usr/local/lib" "/var/vpopmail/bin" "/var/vpopmail/lib" "/var/qmail/bin" "/var/qmail/plugins" "/home/vpopmail/bin" "/home/vpopmail/lib" "/opt")

if [ -f /etc/gentoo-release ]; then
 pkg_dep_cmd="equery -q -C b"
elif [ -f /etc/debian_version ]; then
 pkg_dep_cmd="dpkg -S"
elif [ -f /etc/redhat-release ]; then
 pkg_dep_cmd="rpm -qf"
fi
tmp_file=/tmp/check_lib_dependences_$$.tmp
> $tmp_file
echo "Look for broken library dependences in paths: "${paths[*]}", and their packages ..."
for path in ${paths[*]}; do
 echo "search path: $path"
 find $path -type f|while read file; do
  if file -L $file|grep -q -w ELF; then
   not_found=`ldd $file 2>/dev/null|grep "not found"|awk '{print $1}'|sort|uniq|tr '\n' ' '`
   if [ -n "$not_found" ]; then
    echo "broken lib. dep. found in $file "'('"$not_found"')'
    $pkg_dep_cmd $file|cut -f1|tr -d ":" >> $tmp_file
   fi
  fi
 done
done

echo "-------"
out="check_lib_dependences.out"
mv -f $tmp_file $out
echo "Packages that contains files with broken library deps., can be found in file: $out"
echo "-------"
sort $out|sort|uniq|sed -r 's/^(.*)-[0-9].*/\1/'