#!/usr/bin/perl

use Getopt::Std;

sub usage {
    print <<EOF;
replaceNL.pl : replace new lines in strings by replacement string given in argument (or remove it if no -r)
option :
       -q : quote mark (\' by default)
       -r : replacement string
       -h : this
EOF
}

getopts('hq:r:',\%opts);

if ($opts{h}) {
	usage();
	exit(0);
}

$replacment = '';
$replacment = $opts{r} if defined $opts{r};

$quote = '\'';
$quote = $opts{q} if defined $opts{q};

if (length $quote ne 1) {
	usage();
	print "Quote must be one character long\n";
	exit(1);
}

$c = '';
$inquote = 0;
while ( (read STDIN, $c, 1) == 1) {
	if ($c eq $quote) {
		$inquote ^= 1;
		print $c;
	} elsif ($c eq "\n" and $inquote) {
		print $replacment;
	} else {
		print $c;
	}
}


