#!/bin/sh
# whatssing.sh
# version 1.0, 04/01/2019
# script to find packages that have a dependency on entered library, on a Slackware-based distro
# script by Paul Sherman <psherman2001@gmail.com>
# c2019, GPL3

LFILE=$HOME/WHATSUSINGLIST.TXT
rm -f $LFILE 2>/dev/null
FINALENTRY=""
DIALOG=Xdialog
PKGDIR='/var/lib/pkgtools/packages'
rm -rf /tmp/99.txt 2>/dev/null

echo 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QAAAAAAAD5Q7t/AAAACXBI
WXMAAC4jAAAuIwF4pT92AAAAB3RJTUUH4wQCBDEw5AAyBQAAABl0RVh0Q29tbWVudABDcmVhdGVk
IHdpdGggR0lNUFeBDhcAAAIESURBVDjLnZLfS1MBGIaf82NuY+Y505xDp+KPZCMKL4rEAkmEyEEo
FFFQeBVBF3UhQV0Uari6yH+goIJuCqmL6KKgLqK68Q8I1NSZLGxOM51nnp2zrwspAqehz+0LD9/L
9yr8w8j9p7K3qhIjaCL2Ous5m7PnuhS24W+YGHkm+44eork5QoVfxbUdZqbnyaZSxOPtW0pUgP6b
D6X0cBdGrAHH52FV0bB8XvwtdSi1jbx+9Vm2Euh9F2+LE2nFNYOkF8ENwJpnI1x1YN4fIuBb2rKC
npMSPsxEsMIgdWDboGvgFsB1Ib0CumXwcvS99J7u3FRFd0TlyyQsO3kyUY1wSMV1IGsJmgaaR6HS
1YiIXfyC9V9pmqszGNUhTANC5VBWCmuWwux3GJ8D05vB9ueKC+zsCjX2GOHaGAejEKuFYABWclBe
AV6xqMkkudDXU/QT6pt3DxRSY+jjbwkH88RMaPVAbA9UebJEFj+i53P/30H3yStSvz/KkbYD1IWC
pH4sMj0xy/LCEma4gbL558QDL2gasJSigj/09vSLz1+Cz+vj0eNbCsDlUyfk2hk/TW31fBt9QuON
n9uucxOD7UjyXoUUUkOSm7wuU8OGsFMSHaokE6YU5gY3JAlz55LhDlWSd00ppIbEnbgqX+8YouxG
cr47iN7SyaepenbFwDFdLsWPC8BvGVbBzVaml+MAAAAASUVORK5CYII=
' | base64 -d > /tmp/icon.png

function CLOSE_EXIT() 
{ 
    echo "Close and exit..." 
    kill $(ps aux | grep 'yad' | awk '{print $2}') 2>/dev/null 
	pkill -f $0
} ; export -f CLOSE_EXIT 

start_process () {
	for f in `find /usr/lib64 -name "*.so*" -type f`; do
		process_file $f $1
	done
	for f in `find /usr/bin -type f`; do
		process_file $f $1
	done
	for f in `find /usr/sbin -type f`; do
		process_file $f $1
	done
	for f in `find /usr/local/sbin -type f`; do
		process_file $f $1
	done
	for f in `find /usr/local/bin -type f`; do
		process_file $f $1
	done
	for f in `find /usr/libexec -type f`; do
		process_file $f $1
	done
}


process_file () {

	if [[ -d $1 ]]; then
		return 0
	fi
	if [ -z "$1" ]; then
		return 0
	fi
	if [ ! -f "$1" ]; then
		return 0
	fi
	# filetype testig...
	DTYPE=`file "$1" 2>/dev/null`
	if [[ "$DTYPE" == "*Perl script*" ]]; then
		return 0
	fi
	if [[ "$DTYPE" == "*ASCII*" ]]; then
		return 0
	fi
	if [[ "$DTYPE" == "*image*" ]]; then
		return 0
	fi
	if [[ "$DTYPE" == "*XML*" ]]; then
		return 0
	fi
	# test if this is a symolic link, get link target if so...
	ISLINK=`file "$1" | cut -d\  -f2`
	if [ "$ISLINK" == "symbolic" ] ; then
		xx=`readlink -f "$1" 2>/dev/null`
		input="$xx"
	else
		input="$1"
	fi

	# for shared libs, ldd often reports symlinks to different versions,
	# so we have to chop off libs after the .so

	if [[ $2 == *.so.* ]]; then
		FINALENTRY=${2%\.so\.*}.so
	else
		FINALENTRY=$2
	fi

	retpath="${input:1}"
	ISUSING=`ldd "$1"  2>/dev/null | grep "$FINALENTRY"` 
	if [ ! -z "$ISUSING" ]; then
		grep -R "$retpath$" $PKGDIR | cut -d:  -f1 | rev | cut -d'/' -f 1 | rev >> /tmp/99.txt
	fi

} #end of  "process_file" function


ENTRY=$(zenity --entry --title="Find Dependents" --class=findinstalled \
--text="`printf "Enter a shared library name below:\nwhole path is not necessary, nor are version numbers."`" \
--window-icon=/usr/share/pixmaps/findinstalled.xpm --width=400 --entry-text "" 2>/dev/null)
if [ ! "$ENTRY" ]; then
	exit 0
fi

# get just the filename, not the whole path
ENTRYDAT=`echo $ENTRY | rev | cut -d'/' -f 1 | rev`

yad --info --width=450 --title="Searching" --window-icon="/tmp/icon.png" --text-align center --undecorated  --center --button="EXIT:bash -c CLOSE_EXIT"  --text="\n<b><big>\nSearching programs that depend upon:\n \"$ENTRYDAT\"\n\n</big></b>A list will be displayed upon completion.\n\nThis can take quite a while.\nClicking \"Exit\" will stop the process." &
sleep 1
	 
start_process $ENTRYDAT

kill $(ps aux | grep 'yad' | awk '{print $2}') 2>/dev/null
sort /tmp/99.txt | uniq > $LFILE
sed -i "1s;^;Packages that depend upon \"$ENTRYDAT\"\n \n;" $LFILE
#cp $LFILE $HOME/Desktop/$FINALENTRY-dependents.txt
scite $LFILE&
exit 0
