#!/Perl

print "Hello, World...\n";
$in_dir = 'tmp4';
$out_dir = 'tmp5';
$in_file = 'tmp4/1-1-1-1.html';
load_file($in_file);
opendir TDIR, "$in_dir" or die "ERROR: No directory $in_dir ... $1\n";
@files = readdir TDIR;
closedir TDIR;
$dcnt = scalar @files;
print "Processing $dcnt files ...\n";
@dlines = ();
foreach $fil (@files) {
	if (($fil eq '.')||($fil eq '..')) {
		# skip dot and double dot
	} else {
		$ff = $in_dir . '/' . $fil;
		load_file($ff);
		$ffo = $out_dir . '/' . $fil;
		$ntxt = join("\n", @dlines);
		$ntxt .= "\n"; # add the last ...
		write2file( $ntxt, $ffo );
	}
}

sub load_file {
	my ($if) = shift;
	open INF, "<$if" or die "FAILED to open [$if] ... $! \n";
	@lines = <INF>;
	close INF;
	$cnt = scalar @lines;
	print "[$if] Got $cnt lines ... ";
	@dlines = ();
	foreach $line (@lines) {
		@lnarr = split(/\r/, $line);
		push(@dlines, @lnarr);
	}
	$cnt = scalar @dlines;
	print "Got $cnt lines ...\n";
}

sub write2file {
	my ($txt,$fil) = @_;
	open WOF, ">$fil" or die "ERROR: Unable to open $fil!!! $! \n";
	print WOF $txt;
	close WOF;
}
