#/usr/local/bin/perl
# tb, boone, 07/27/92
# Create an index of all files in a tree
# Copyright (C) 1992, Trustees of Michigan State University
#
# Modifications:
# 07/27/92 Boone      Initial coding
# End Modifications
#
# Description:
# Tb treewalks the current directory, generating Gopher- style
# directories of each one.  The resulting directories are edited for
# completeness (full pathname) and appended to a large listing file.
# This file can be searched with the companion program ts.  Tb uses the
# gopherls program (a symbolic link to gopherd) to generate the basic
# listing.  There are two possible problems with gopherls:
#
#   1. gopherls versions earlier than 1.02 try to write to stdin.  This
#      should be fixed in 1.02.  To fix this in earlier versions, locate
#      the following code in gopherd.c near line 255:
#
#	listdir(0, "/");
#
#      and change it to:
#
#	listdir(fileno(stdout), "/");
#
#   2. gopherls versions earlier than 1.02 set the hostname to a null string.
#      This results in unusable output from tb.  To correct this problem,
#      locate the following code in gopherd.c near line 255:
#
#	if (RunLS) {
#		Zehostname ="";
#		Caching = FALSE;
#
#      and change it to:
#
#	if (RunLS) {
#		Zehostname = GetDNSname(DOMAIN_NAME);
#		Caching = FALSE;
# End Description

sub dive
{
	local($dir) = @_;
	local($i, $temp);
	&listit($dir);
	opendir(F, $dir) || die "Unable to open $dir\n";
	local(@flist) = readdir(F);
	close(F);
	foreach $i (@flist)
	{
		next if ($i =~ /^\./);
		$temp = "$dir/$i";
		if (-d $temp)
		{
			&dive($temp);
		}
	}
	return;
}

sub listit
{
	local($pathnm) = @_;
	local($j);
	$pathnmsub = $pathnm;
	$pathnmsub =~ s/ /\\ /g;
	open(LS, "/usr/local/etc/gopherls $pathnmsub |");
	local(@lines) = <LS>;
	close(LS);
	foreach $j (@lines)
	{
		chop($j); chop($j);
		next if ($j =~ /^\.$/);
		local(@fields) = split(/\t/, $j);
		$fields[1] = substr($fields[1], 0, 1) . $pathnm . 
					substr($fields[1], 1);
		print LOG join("\t", @fields), "\r\n";
		undef(@fields);
	}
	return;
}

open(LOG, ">.tsdata") || die "Unable to open .tsdata\n";
&dive(".");
close(LOG);
exit(0);
