#!/bin/bash
# sd.sh
# created and copyrighted: 17 Mar 11
#
# author: Evsiz (evsiz@this-voice.org)
# The author releases this file under the GNU GPL
# license. See GNU's COPYING file for specifics.
#


# consts
LANG=C
export LANG
LC_ALL=C
export LC_ALL


# code
version () {
	echo "sd.sh slackware package dependencies ver. 17mar11"
	exit
	}

help () {
	echo " sd.sh slackware package dependencies
sd.sh -?   this help
sd.sh -v   show version
sd.sh /var/log/packages/somepkg  display pkg dependencies
      will work with multiple /var/log/package files or wildcards"
	exit
      }

main () {
	echo $1
    LIST=`awk '/\/$/{next}
            /(sbin\/.*|bin\/.*|libexec\/.*|lib\/.*\.so(\..*)?)$/{print}' $1`
	if [ "$LIST" == "" ]; then
		echo "no libs or bins in pkg"
		return
		fi
	echo "missing:"
	for i in $LIST; do
		i=`echo $i | sed 's_lib/incoming_lib_'`
		if [ -x /$i ]; then
#       		echo "#0 "$i #DEBUG
			ldd /$i | awk '$0 ~ /not found/ { print "\t" $1 }'
		    LIBS=$(ldd /$i | awk '/not found/{next}
                    {print $1;E=1}
                    END{exit E ? 0 : 1}') || continue # no lib found here.
			for lib in $LIBS; do
				if [ "$lib" == "not" ]; then continue; fi
#				echo "#1 "$lib #DEBUG
			    grep -l $lib /var/log/packages/* >> tmp$$ || \
			    if [ "$lib" != "linux-gate.so.1" ]; then
					file=`locate -n 1 $lib`
					file=`ls -l $file | cut -d" " -f10`
#       				echo "#2 "$file #DEBUG
					grep -l $file /var/log/packages/* 2>/dev/null >> tmp$$
					fi
			    done
		fi
		done
	echo "requires:"
	if [ -f tmp$$ ]; then
		sort tmp$$ | uniq | sed 's_/var/log/packages/_\t_'
		rm tmp$$
	fi
	}

case "$1" in

 --version|-v) version  ;;

 --help|-h|--usage|-?) help ;;

 *) if [ "$*" == "" ]; then
		help
	else
	    LC_ALL=en_US.ISO-8859-1
	    for n in $*; do
	        main $n
	        done
	fi
;;
esac
