#!/usr/bin/perl -w

while(<>) {
	chomp;
	if (/^\tsed -i( -e)? (['"])([^'"]+)['"]\s(.*)/) {
		my ($quote, $file) = ($2, $4);
		if ($quote eq "'") {
			print "\ted -s $file <<-\\EOF\n";
		} else {
			print "\ted -s $file <<-EOF\n";
		}
		do {
			my $expr = $3;
			if ($expr =~ /^\//) {
				print "\tg$expr\n";
			} else {
				print "\t,$expr\n";
			}
			chomp($_ = <>);
		} while (/^\tsed -i( -e)? ($quote)([^'"]+)$quote\s\Q$file\E/);
		print "\tw\n";
		print "\tEOF\n";
		redo;
	} elsif (/^sed -i( -e)? (['"])([^'"]+)['"]\s(.*)/) {
		my ($quote, $expr, $file) = ($2, $3, $4);
		print "ed -s $file <<-EOF\n";
		if ($expr =~ /^\//) {
			print "\tg$expr\n";
		} else {
			print "\t,$expr\n";
		}
		print "\tw\n";
		print "\tEOF\n";
	} else {
		print $_, "\n";
	}
}
