#!/bin/sh
#
# pkggrep v1.11 by Stephen Kennedy <steve@sdk-software.com>, 1995-2000.
# Part of the `morepkgtools` package.
# Copyright S D K Software, but freely distributable.
#
# Nothing fancy here, just saves a bit of a time (I seem to be forever
# grepping /var/log/packages/*) and gives a nice dialog display.

if [ $# = 0 ] ; then
	echo "Usage: pkggrep [grep options] expression" 1>&2
	exit 1
elif [ "$1" = "-help" ] ; then
	echo "Usage: pkggrep [grep options] expression"
	exit 0
fi

TMP=${TMP:=/tmp}

if [ ! -d "$TMP" ]; then
	echo "\$TMP (\"$TMP\") is NOT a directory" 1>&2
	exit 1
fi

ADM_DIR=${ADM_DIR:="`echo "$TARGET_DIR/var/log" | tr -s '/'`"}
PACKAGES=${PACKAGES:="`echo "$ADM_DIR/packages" | tr -s '/'`"}

if [ ! -d "$PACKAGES" ] ; then
	cat <<EOF 1>&2
The PACKAGES variable is set to "$PACKAGES", which is not a directory.
Maybe you are using an old version of Slackware? If you are, type
'export ADM_DIR=/var/adm' and try running 'pkggrep' again.
EOF
	exit 1
fi

touch "$TMP/pkggrep.$$" && chmod 600 "$TMP/pkggrep.$$" && grep "$@" "$PACKAGES"/* >> "$TMP/pkggrep.$$" && dialog --title "pkggrep $* - results" --textbox "$TMP/pkggrep.$$" 22 74 2>/dev/null

rm -f "$TMP/pkggrep.$$" 2>/dev/null
