#!/usr/bin/perl
#strip_nl, by Steve D. Sharples, May 2002.
#GPL

if ($ARGV[1] eq ""){
	print "strip_nl : strips single newlines (\\n) from the end of each line.\n";
	print "           Leaves multiple newlines in place (eg between paragraphs).\n";
	print "           Useful for exporting text files generated in (eg) pico\n";
	print "           when word wrap has been turned on. By Steve D. Sharples.\n\n";
        print "Usage :    strip_nl <input file> <output file>\n";
        die "\n";
}
if (-e "$ARGV[1]"){
        print "Output file \"$ARGV[1]\" exists. Overwrite? (y/n): ";
        $conf = <STDIN>;
        chop($conf);
        if ($conf ne 'y'){
                die "Ok then, quitting.\n";
        }
}
open IN, "$ARGV[0]";
open OUT, "> $ARGV[1]";

while(<IN>){

	$current=$_;
	chomp($current);
	
	if (substr($current,-1,1) eq " "){
		chop($current);
		}

	if ($current ne "") {
		chomp($so_far);
		if ($so_far eq ""){
			$so_far=$current;
			}
		else{
			$so_far=$so_far . " " . $current;
			}
		$so_far .= "\n";
		}
	else {
		$out=$so_far . "\n";
		print OUT $out;
		$so_far="";
		}
	}

print OUT $so_far;
