#!/usr/bin/perl
# Copyright 2000-2003 Vlado Keselj www.cs.dal.ca/~vlado
#
# Justify text, left align, ragged right.

use strict;
use vars qw($VERSION);

$VERSION = sprintf "%d.%d", q$Revision: 1.2 $ =~ /(\d+)/g;

my $par='';
while (<>) {
    if ($par =~ /\n\s*\n$/) {
	foreach my $l ( &justify(72, $par) ) { print "$l\n" }
	print "\n";
	$par = '';
    }
    $par .= $_;
    $par =~ s/^\s+//;
}

if ($par) { foreach my $l ( &justify(72, $par) ) { print "$l\n" } }

sub justify($@) {
    my $width = shift;
    my ($p, $r, @ret);
    while (@_) { $p.= " ". shift }

    $p =~ s/^\s+//; $p =~ s/\s+/ /g; $p =~ s/\s*$/ /;
    while ($p) {
	$p =~ / /;
	$r = $`; $p = $';
	while ($p) {
	    $p =~/ /;
	    if (length("$r $`") <= $width) {
		$r .= " $`"; $p = $';
	    } else { last; }
	}
	push @ret, $r;
    }
    return @ret;
}

exit 0;

__END__
=head1 NAME

justify - Justify text, left align, ragged right.

=head1 SYNOPIS

   justify [input files]

=head1 DESCRIPTION

This script left-justifies the input text into 72-width rows.

=head1 SCRIPT CATEGORIES

Text

=head1 README

Justifies text, left align, ragged right.

=head1 COPYRIGHT

Copyright 2003 Vlado Keselj F<http://www.cs.dal.ca/~vlado>

This script is provided "as is" without expressed or implied warranty.
This is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

The latest version can be found at F<http://www.cs.dal.ca/~vlado/srcperl/>.

=cut