#!/bin/sh
#
# $Id: ldcheck,v 1.1 2003/10/28 06:01:53 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.9

# 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;;
		--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
    -v, --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 -f $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
		echo "File '$1' in package '$pkg' has bad dependencies:"
		if [ $VERBOSE ]; then
			echo "$lddout"
		else
			echo "$baddeps"
		fi
	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
				echo "$OPTPATH"
				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
				echo $OPTPATH
				OPTFOUND=yes
			fi
		fi
		if [ $OPTFOUND = no ]; then
			test_executables $L
		fi
	fi
done
