#!/bin/sh
#
# simgrep -- search compressed Simtel.Net file list, and print findings
#   with directory names
#
# <rreiner@nexus.yorku.ca>
# Last revised Mon Feb  3 13:08:07 EST 1992
#

#############################################################################
# Customization section.
#
# The compressed file simibm.lst is stored in:
COMPFILE=/usr/phil/grads/phigs/lib/simibm.zoo
# The program it is to be uncompressed with:
COMPPROG=zoo
# The commandline switched needed to get COMPROG to extract a file to stdout:
COMPFLAGS=xp
# egrep:
EGREPPROG=egrep
# AWK:
AWKPROG=nawk
#
# End of customization section.
#############################################################################

MYNAME=`basename $0`

if [ $1z = z ]; then
	echo "$MYNAME: usage is  $MYNAME expresssion."
	echo "$MYNAME: note: searches are case-insensitive egrep regular expressions."
	exit 1
fi

#
# The AWK contortions at the end of the pipe do the following:
#
#   Decide whether to print each line only when the next is encountered; then,
#   if several "Directory PD" lines appear in sequence, keep only the last;
#   precede each change of directory with a newline; and print everything
#   that is not a "Directory PD" entry.
#
# The egrep and AWK stages of the pipe could be consolidated, but it is
#   simpler and faster this way.
#
$COMPPROG $COMPFLAGS $COMPFILE | $EGREPPROG -i "Directory PD"\|"$1" \
    | $AWKPROG '   { if (prevline == "") {
			prevline = $0
			next
		}
		if (prevline ~ "Directory PD") {
			if ($0 ~ "Directory PD") {
				prevline = $0
				next
			} else {
				print prevline
				prevline = $0
			}
		} else {
			print prevline
			prevline = $0
			if ($0 ~ "Directory PD")
				print ""
		}
	      }
	END   { if (! (prevline ~ "Directory PD"))
			print prevline
	      }'
