#!/bin/echo Source me: .
#
# Copyright (c) 2002-2004 by Frederick Bruckman
# All rights reserved.

# Create the dir2dependencies database.
#
# We're using "make" here, rather than trying to parse the "Makefiles"
# directly, in the hope that this will prove to require less continuing
# maintenance. This is clearly where future improvements will lie. Any
# candidate replacement for this function or it's partner,
# "create_dir2pkgname", should consistently produce the same output.
# 
# The function takes one argument:
#
#     Argument #1: the name of the output file.
#
create_dir2dependencies () {
	cd $PKGSRCDIR
	make show-depends-dirs PKG_MISC_TARGETS=show-depends-dirs \
	| awk -v dir2dependencies_idx="$1" '
	/^===> [^\/][^\/]*$/ {			# category
		if (FileOpen)			# not first category
			fflush(dir2dependencies_idx)
		else				# first category
			FileOpen = "true"
	}
	/^===> [^\/][^\/]*\// {			# category/package
		dir = $2
		tabs = ""
		numtabs = 5 - int(length(dir) / 8)
		while (numtabs--)
			tabs = tabs "\t"
		do getline
			while ($0 ~ /^=/)	# IGNORE set
		for (i=1; i<=NF; i++)
			if (i == 1)
				printf "%s%s%s", dir, tabs, $1 > dir2dependencies_idx
			else
				printf "%s%s", OFS, $i > dir2dependencies_idx
		if (NF > 0)
			printf "%s", ORS > dir2dependencies_idx
	}'
}
