#!/usr/bin/perl
#   mfl [$len]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This little program reads in a list of file names, and coughs  them #
# up  formatted  as  make  likes  to see them.  The result will be in #
# alphabetical order, with a tab in col 1, as many file names as will #
# fit  into  $len  bytes,  and  a backslash on all but the last line. #
# Blank lines are ignored.  If the first non-blank line is  indented, #
# its  first "word" will be proceeded by a blank line and produced on #
# a line by itself.  This can be used from vi to rewrite a file  list #
# in a neater form.                                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$len = $ARGV[0] || 75;

# Read in the data and collect the file names.
for $line (<STDIN>) {
	$line =~ s/\s*\\+\s*/ /g;
	if ($line[0]) {			# Was previous line empty?
		&par() if ($name || %name);	# If so, put out chunk.
		$name = shift(@line);		# 
		print "\n" if $blankline;	#
		$blankline = 0;	#
	}
	foreach $fld (@line) {
		next if !$fld;
		next if $fld eq "\\";
		$name{$fld} ++;
	}
	@line = split(/\s+/,$line);
	$blankline++ if !$line;
}
&par() if ($name || %name);
print "\n" if $blankline;
exit 0;

sub par {
	$line = '';
	# If there was an initial name, produce it on a separate line.
	if ($name) {
		print "$name";
		print " \\\n" if %name;
	}
	# Run thru the file names and produce all but the last line.
	if (%name) {
		for $key (sort keys %name) {
			if (length($line) + length($key) > $len) {
				print "\t$line\\\n";
				$line = '';
			}
			$line .= "$key ";
		}
	# The above guarantees at least one file name on the last line.
	print "\t$line\n";
	}
	$name = '';
	%name = ();
}
