#!/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
# 09/10/92 Boone      Solved the problem of inserting the current path
#                     into a deliberately empty selector.  This was
#                     causing odd behavior on telnet items, and also
#                     helped ts to lose some items.
# 01/22/93 Boone      Added capability to index subtrees
# End Modifications
#
# Version 1.6
#
# 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);
		if ((substr($fields[1], 0, 1) ne "8") &&
 		    (substr($fields[1], 0, 1) ne "T"))
		{
			if (length($fields[1]) > 1)
			{
				$fields[1] = substr($fields[1], 0, 1) .
					$prefix . substr($pathnm, 1) .
					substr($fields[1], 1);
			}
		}
		print LOG join("\t", @fields), "\r\n";
		undef(@fields);
	}
	return;
}

# Main routine

$[ = 0;
if ($#ARGV < 0)
{
	print STDERR "usage: tb path-to-index gopher-root-path\n";
	exit(1);
}
open(LOG, ">.ts/.tsdata") || die "Unable to open .tsdata\n";
chdir($ARGV[0]);
if ($ARGV[1])
{
	if ($ARGV[1] eq substr($ARGV[0], 0, length($ARGV[1])))
	{
		$prefix = substr($ARGV[0], length($ARGV[1]));
	}
	else
	{
		print STDERR "$0: path-to-index not under gopher-root-path\n";
		exit(2);
	}
}
else
{
	$prefix = "";
}
&dive(".");
close(LOG);
exit(0);
