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

# Create the dir2pkgname database. This takes a while, so provide a
# progress report in the way of a single dot sent to the terminal as
# each category is begun. The progress report goes to "/dev/tty", not
# standard error, so it'll be sent to "/dev/null" when this script is
# run via "crontab" or otherwise unattended.
#
# The function takes one argument:
#
#     Argument #1: the name of the output file.
#
create_dir2pkgname () {
	cd $PKGSRCDIR
	echo "Running make to build database... this takes a while..." >> /dev/tty
	make show-var PKG_MISC_TARGETS=show-var VARNAME=PKGNAME \
	| awk -v dir2pkgname_idx="$1" '
	/^===> [^\/][^\/]*$/ {			# category
		if (FileOpen)			# not first one
			fflush(dir2pkgname_idx)
		else				# first one
			FileOpen = "true"
		printf "." >> "/dev/tty"
	}
	/^===> [^\/][^\/]*\// {			# category/package
		dir = $2
		tabs = ""
		numtabs = 5 - int(length(dir) / 8)
		while (numtabs--)
			tabs = tabs "\t"
		do getline pkgname
			while (pkgname ~ /^=/)	# IGNORE set
		print dir tabs pkgname > dir2pkgname_idx
	}
	END {
		printf "%s", ORS >> "/dev/tty"
	}'
}
