#!/usr/bin/perl
#
#  @(#)mkfacesindex 1.3 91/11/19
#
#  Copyright (c) Steve Kinzler - April 1991.
#
#  Permission is given to distribute these sources, as long as the
#  copyright messages are not removed, and no monies are exchanged.
#
#  No responsibility is taken for any errors on inaccuracies inherent
#  either to the comments or the code of this program, but if reported
#  to me, then an attempt will be made to fix them.

# Uncomment this line if you are running a version of faces earlier
# than version v1.6.0:
#$olddb = 1;

$FACEDIR = '/usr/local/faces';

# mkfacesindex - make a .index file at the root of a faces database that
#		 facesall can use more speedily instead of tabulating the
#		 database itself
# usage: mkfacesindex [ bitmap_directory ]
#	default directory is $FACEDIR above
# Steve Kinzler, kinzler@cs.indiana.edu, April/November 1991

if ($dir = $ARGV[0]) {
	$dir = $FACEDIR . '/' . $dir unless $dir =~ /^\// || -d $dir;
	$FACEDIR = $dir;
}
chdir $FACEDIR || die "$0: Cannot chdir $FACEDIR ($!)";

open(INDEX, "> .index") || die "$0: Cannot open $FACEDIR/.index ($!)";

if ($olddb) {
	foreach $dir (`ls`) {
		chop $dir;
		if (-d $dir && ! -l $dir) {
			foreach (`ls $dir`) {
				print INDEX "$dir/$_";
			}
		}
	}
	close INDEX;
	exit;
}

open(FIND, 'find . -type f \( -name face.xbm -o -name 48x48x1 -o \
			      -name sun.icon -o -name face.ps \) -print |');

while (<FIND>) {
	s,^\./+,,;
	s,/+[^/]*$,,;
	/^(.*)\/+([^\/]+)$/;
	$aindom = $1; $user = $2;
	$domain = join('.', reverse(split(/\/+/, $aindom)));
	$index{"$domain/$user"}++;
}

close FIND;

for (sort keys %index) {
	print INDEX "$_\n";
}

close INDEX;
