#!/bin/sh
#
# $Id: ldcheck,v 1.2 2003/11/15 05:40:05 kevinwilliams Exp $
#
# Copyright (C) 2003 Kevin Dale Williams
#
#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
# 
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

VERSION=0.95

# global settings
IFS=:

if [ -f /etc/gentoo-release ]; then
	DIST="gentoo"
	if [ ! -f /usr/bin/qpkg ]; then
		echo "On Gentoo, this script requires the 'gentoolkit' package."
	fi
fi
if [ -f /etc/mandrake-release ]; then
	DIST="mandrake"
fi
if [ -f /etc/SuSE-release ]; then
	DIST="suse"
fi
if [ -f /etc/redhat-release ]; then
	DIST="redhat"
fi
if [ -f /etc/debian_version ]; then
	DIST="debian"
fi

# parse command-line args
while [ $# -gt 0 ]; do
	case "$1" in
		-O | --no-opt)
			SKIPOPT=yes
			shift;;
		-v | --verbose)
			VERBOSE=yes
			shift;;
	    -vv | --very-verbose)
		    VERYVERBOSE=yes
            shift;;
	    -q | --quiet)
		    QUIET=yes
            shift;;
		--version)
			echo $"
ldcheck $VERSION
Copyright (C) 2003 Kevin Dale Williams
This is free software - see the source for copying conditions.
There is no warranty for any use or purpose.
"
			exit 0;;
		--help)
			echo $"
Usage: ldcheck [OPTION]

    -O, --no-opt        skip checking references in '/opt' which may
                        have dynamic configurations

    -q, --quiet         display only the packages affected

    -v, --verbose       display the packages affected as well as
                        the files in the package with missing
                        dependancies
    
    -vv, --very-verbose print the entire 'ldd' output for each file
                        that has missing dependancies

    --help              print this help and exit

    --version           print version information and exit
"
			exit 0;;
		-*)
			echo >&2 $"Unrecognized option" "\`$1'"
			echo >&2 $"Try 'ldcheck --help' for more information."
			exit 1;;
		*)
			break;;
	esac
done

# run 'ldd' against the file and check the output
find_bad_dependencies()
{
	lddout=`ldd $1 2>/dev/null`
	baddeps=`echo $lddout | grep found`
	if [ "X$baddeps" != "X" ]; then
		case "$DIST" in
			gentoo)
				pkg=`qpkg -nc -f $1 | sed -r 's/.*\/(.*)/\1/'`;;
			redhat | suse | mandrake)
				pkg=`rpm -qf $1`;;
			debian)
				pkg=`apt-file search $1`;;
			*)
				pkg="unknown";;
		esac
		if [ "X$pkg" = "X" ]; then
			pkg="unknown"
		fi
		mkdir -p ~/tmp/ldcheck/$pkg
		filemorph=`echo $1 | tr "\/" "%"`
		echo "$lddout" >~/tmp/ldcheck/$pkg/$filemorph.ldd
	fi
}

# check executable files in directory
test_executables()
{
	for myfile in `find $1 -type f | tr "\n" "\:"`
	do
		if [ -r $myfile ]; then
			if [ -x $myfile ]; then
				find_bad_dependencies $myfile
			fi
		fi
	done
}

################
## main code
################
for D in $PATH
do
	if [ -d $D ]; then
		OPTFOUND=no
		if [ $SKIPOPT ]; then
			OPTPATH=`echo $D | grep /opt`
			if [ ! "X$OPTPATH" = "X" ]; then
				OPTFOUND=yes
			fi
		fi
		if [ $OPTFOUND = no ]; then
			test_executables $D
		fi
	fi
done

for L in `cat /etc/ld.so.conf | tr "\n" "\:"`
do
	if [ -d $L ]; then
		OPTFOUND=no
		if [ $SKIPOPT ]; then
			OPTPATH=`echo $L | grep /opt`
			if [ ! "X$OPTPATH" = "X" ]; then
				OPTFOUND=yes
			fi
		fi
		if [ $OPTFOUND = no ]; then
			test_executables $L
		fi
	fi
done

for P in `find ~/tmp/ldcheck/* -type d | tr "\n" "\:"`
do
    echo ""
    P2=`echo "$P" | sed -r 's/.*\/(.*)$/\1/'`
    echo -e "\E[01;031m$P2\E[00m"
    for F in `find $P -type f | tr "\n" "\:"`
    do
        F2=`echo "$F" | sed -r 's/.*\/(.*)$/\1/' | tr "%" "\/"`
        if [ ! $QUIET ]; then
            echo "  $F2" | sed -r 's/^(.*)\.ldd$/\1/'
            if [ $VERBOSE ]; then
                cat $F | grep found | sed -r 's/^(.*)\ =>\ not\ found$/\1/'
            fi
            if [ $VERYVERBOSE ]; then
                cat $F
            fi
        fi
    done
done
echo ""

# cleanup
rm -rf ~/tmp/ldcheck