#!/bin/sh
#
# culldeps
#
# Filter redundant dependencies.
#
# Emit all the dependencies on the standard input to the standard
# output EXCEPT the dependencies which can be derived from other
# dependencies by the transitive rule. For example, omit both A C
# and A D from
#
# 	A B
# 	B C
# 	C D
# 	A C
# 	A D
#
# because A C can be derived from A B and B C by transitivity,
# and A D can be derived from A B, B C, C D by transitivity.
#

SCRATCH=$(mktemp -d /var/tmp/$0.XXXXXX)
NEXTLEFTOVERS=$SCRATCH/leftovers0
LASTJOIN=$SCRATCH/join0
NEXTJOIN=$SCRATCH/join1
TAB="	"

sort -k 1 > $LASTJOIN

LEFTOVERS=$LASTJOIN

while [ $(wc -l $LASTJOIN | awk '{ print $1; }') -ne 0 ]; do

	#
	# From dependencies X-requires-Y in $LEFTOVERS and Y-requires-Z in
	# $LASTJOIN, produce dependencies X-requires-Z and write them to
	# $NEXTJOIN.
	#
	sort -k 2 < $LEFTOVERS | join -1 2 -2 1 -o '1.1 2.2' - $LASTJOIN | \
	    sort -u > $NEXTJOIN
	if [ ${DEBUG:-0} -gt 0 ]; then
		echo "### filtered ###" 1>&2
		join -t "$TAB" $NEXTJOIN $LEFTOVERS | sort 1>&2
		echo "###" 1>&2
	fi

	#
	# Filter out of $LEFTOVERS all of the dependencies X-requires-Z, which
	# were produced in the previous step. Write the new leftovers to
	# $NEXTLEFTOVERS.
	#
	join -v 2 -t "$TAB" $NEXTJOIN $LEFTOVERS | sort -u > $NEXTLEFTOVERS

	#
	# Swap output files before repeating. 
	#
	LASTJOIN=$NEXTJOIN
	if [ $(basename $NEXTJOIN) = join0 ]; then
		NEXTJOIN=$SCRATCH/join1
	else
		NEXTJOIN=$SCRATCH/join0
	fi
	LEFTOVERS=$NEXTLEFTOVERS
	if [ $(basename $NEXTLEFTOVERS) = leftovers0 ]; then
		NEXTLEFTOVERS=$SCRATCH/leftovers1
	else
		NEXTLEFTOVERS=$SCRATCH/leftovers0
	fi
done

#
# Output all of the dependencies that were not culled and clean up.
#
cat $LEFTOVERS
rm -r $SCRATCH
