#!/usr/bin/perl -w
#
#NAME
#  tunedirs2 - sort files into 2-byte subdirectories
#
#SYNOPSIS
#  tunedirs2 [file]..
#
#DESCRIPTION
#  The first 2 chars of each file name are converted to  lower  case,
#  the  directory by that name is created, and the file is moved into
#  the directory.
#
#  The idea is to take a directory with too many files and  apportion
#  them  into  lots  of  subdirectories.   This  is practical on many
#  systems, because there is usually a quadratic  term  in  directory
#  search times, and it becomes large for thousands of files.

#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
($P = $0) =~ s".*/"";	# Our name, minus directories
$V = $ENV{"V_$P"} || 1;	# Verbose/trace/debug level
$exitstat = 0;			# Set to nonzero for failure

for $f (@ARGV) {
	if (($c1,$c2) = ($f =~ /^(.)(.)/)) {
		$d = lc("$c1$c2");
		mkdir($d) unless -d $d;
		if (link($f,"$d/$f")) {
			print "-> $d/$f\n" if $V>1;
			unlink($f);
		} else {
			print STDERR "### Can't link $f -> $d/$f [$!]\n" if $V>0;
		}
	} else {
		print STDERR "$P: Ignored \"$f\"\n" if $V>0;
	}
}



exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
