#!/usr/bin/perl
#
#   tmpfiles [files] [size] [prefix]
#
# Make a bunch of temp files.  The command-line args are:
#   files  is the number of files to create.
#   size   is the desired size of each file.
#   prefix is the prefix (directory and/or string) to each file name.
# If files > 99, we will create directories with the prefix, and put
# 100 files in each directory.
#
$files  = $ARGV[0] || 1000;
$size   = $ARGV[1] || 8192;
$prefix = $ARGV[2] || "tmp";
$L = length($files);
#
# Formats:  fmt1 creates file names for < 100 files; fmt2 and fmtd 
# create file names and directory names for > 99 files.
$fmt1 = '%0' . $L . 'd';
$fmt2 = '%0' . ($L-2) . 'd/%0' . $L . 'd';
$fmtd = '%0' . ($L-2) . 'd';
#
# Build a string of 99-byte lines + a newline, exactly $size bytes long.
if ($size > 0) {
	while (length($x) < $size) {
		$x .= "....:....1....:....2....:....3....:....4....:....5....:....6....:....7....:....8....:....9....:....\n";
	}
	substr($x,$size-1) = "\n";
} else {
	$x = '';
}
#
for ($n = 0; $n < $files; $n++ ) {
	if ($L > 2) {
		$hi = $n / 100;	# Directory number.
		$lo = $n % 100;	# File within directory.
		$name = $prefix . sprintf($fmt2,%prefix,$hi,$n);
		mkdir($prefix . sprintf($fmtd,%prefix,$hi),0777)
			if ($lo == 0);
	} else {
		$name = $prefix . sprintf($fmt1,%prefix,$n);
	}
	open(F,">$name")
		|| print "Can't write \"$name\" [$!]\n";
	$y = $x;
	substr($y,0,length($name)) = $name;
	print F $y;
	close(F);
}
