#!/usr/bin/perl
######################################################################
##
##	denial ::: Jed Reynolds ::: Nov 23, '95
##
##	This is a hopefully useful perl script to scan a directory
##	tree of filenames, and create a database of them and
##	aribuituary 8.3 filenames to map to. Um...this won't be
##	the easiest thing I've programmed, but it won't be the 
##	hardest, either. I"m glad I'm motivated enough to learn
##	perl, otherwise I woulndn't be doing it /at all/. I'd
##	probably get around to doingit someday by hand :(
##
######################################################################

######################################################################
# 
# sub make_8
# 
# this subroutine takes a long filename, shortens it to 8.3,
# and scans through the database to see if there's another 8.3
# just like it, and tries again.
# 
######################################################################
sub make_8 {

    $counter = 0;		# frill file counter

    foreach $oldfile (keys %NAMES) {

	# 
	# create an 8.3 filename
	# 

	$_ = $oldfile;
	# s/\./_/g;
	($eight) = /^(\w{1,8})/;

	$_ = $oldfile;
	($three) = /\.(\w{3})\w*$/;
	
	# 
	# cat namebits together and store
	# 
	$newfile = $eight . "." . $three;


	# 
	# look for $newfile duplicates in NAMES
	# 
	$hits = 0;
	while ( ($thisfile, $thatfile) = each ( %NAMES)) {
	    if ( $newfile eq $thatfile ) {
		$hits ++;
	    }			# end of if
	    if ( $hits > 0 ) {
		# now time to rename file (again)
		$_ = $eight;
		($six) = /(\S{6})/;
		$eight = $six . $hits;
		$newfile = $eight . "." . $three;
		print LOGFILE "$hits Hits: $newfile.\n";
	    }
	}			# end of while


	$NAMES { $oldfile } = $newfile;
	$counter ++;
    }				# end of foreach $oldfile

    # 
    # now print this mapping to file
    # 
    while ( ($oldfile, $newfile) = each ( %NAMES )) {
	print MAPPING "$oldfile => $newfile\n";
    }				# end of while 

}				# end of make_8

######################################################################
#
# sub make_file_list
# 
# this scans a directory tree and compiles a list of filenames
# and enters them into a dbm
# 
######################################################################
sub make_file_list {

#    chdir("/home/hed/r/text");
    $basedirectory = `pwd`;
    chop( $basedirectory );

    print LOGFILE "STARTING OFF IN: $basedirectory\n";

    foreach ( `du` ) {
	chdir ($basedirectory);
	chop;
	s/.*\s+//i;
	s/$basedirectory//i;
	
#	# 
#	# skip any non-text directories
#	# 
#	if (!/.*text.*/) {
#	    print LOGFILE "Skipping non-TEXT dir.\n";
#	    next;
#	}
	
	print LOGFILE " ENTERING $_ \n";
	
	$workingdir = $_;
	
	chdir( $_ );
	
	# 
	# for each file
	# 
	while ( $anyfile = <*> ) {
	    next if ( -d $anyfile );
	    $tallyfiles++;	# incriment file counter
	    print LOGFILE "$anyfile, "; # record file
	    $NAMES { $anyfile } = "_blank_"; # create a blank entry
	}			# end of while each *
	print LOGFILE "\n";
    }				# end foreach du
}				# end of make_file_list

######################################################################
# 
# main script
# 
# opens dbm, closes dbm
# 
######################################################################

#
# open logfile, filename mappings, and dbm
#

open (LOGFILE, ">denial.log") || die "Can't open logfile.\n";
open (MAPPING, ">denial.map");

unlink("filelist.dir") || warn "Opening new database.\n";
unlink("filelist.pag");

dbmopen ( %NAMES, "filelist", 0644 ) || die "Can't create or open filelist\n";

do make_file_list();

do make_8();

print "Processed $tallyfiles files.\n";

dbmclose(%NAMES) || warn "Error closeing filelist\n";
close (LOGFILE);
close (MAPPING);

exit;
# eof

