#!/bin/sh
# findinstalled
# version 3.0, 10/19/2018
# script to find files installed by specific packages on a Slackware-based distro
# ...grepping around in /var/lib/pkgtools/packages :)
# script by Paul Sherman <psherman2001@gmail.com>
# c2005-2018, GPL3

LFILE=$HOME/FIND_INSTALLED_OUTPUT.TXT
rm -f $LFILE 2>/dev/null
DIALOG=Xdialog




if [[ -d $1 ]]; then
	$DIALOG --rc-file /usr/share/pixmaps/colorboxDOH.rc  --icon /usr/share/pixmaps/doh.xpm  --title "That\'s a directory" \
	--msgbox "\nThis script finds the package that installed a FILE.\n\nNot for use on directories.\n" 0 0
	exit 0
fi
if [ ! -z "$1" ]; then
	if [ ! -f "$1" ]; then
		exit 0
	fi
	input=$(basename $1)
else


	input=$(zenity --entry --title="Check if a file was installed" --class=findinstalled \
	--text="`printf "...and what package installed it.\n\nEnter the filename below:"`" \
	--window-icon=/usr/share/pixmaps/findinstalled.xpm --width=400 --entry-text "")
	
	
	
	if [ ! "$input" ]; then
		exit 0
	fi
fi




if [ ${#input} -lt "3" ]; then
		$DIALOG --rc-file /usr/share/pixmaps/colorboxDOH.rc  --icon /usr/share/pixmaps/doh.xpm  --title "What was that?" \
			--msgbox "\nSorry but...                  \n\nYou have to enter a filename\nor the results will be meaningless.\n" 0 0
		$0 & # recall this script in the background
		exit 0
fi
yad --info --on-top --width=450 --title="Searching" --text-align center --skip-taskbar --undecorated  --center --no-buttons --text="<span><b><big><big>\n\nSearching installed programs that contain:\n\n \"$input\"</big></big></b></span>" &
sleep 1
grep -R "$input" /var/lib/pkgtools/packages | sed -e 's/\/var\/lib\/pkgtools\/packages\//PACKAGE= /' -e 's/:/   FILE= \//' > $LFILE 
numlines=`wc -l <$LFILE`
if [ $numlines -lt 1 ]; then
	echo "Done searching packages." > $LFILE
	echo "" >> $LFILE
	echo "No results found." >> $LFILE
	echo "It appears that" >> $LFILE
	echo "\"$input\"" >> $LFILE
	echo "was not directly installed by a package." >> $LFILE
fi
kill $(ps aux | grep 'yad' | awk '{print $2}') 2>/dev/null
scite $LFILE&
exit 0
