#!/usr/bin/perl -w

my $orig = shift;
my $delta = shift;

open ORIG, "< $orig" || die "couldn't open $orig\n";
open DELTA, "< $delta" || die "couldn't open $delta\n";

my $handler;
my $command;
my $count;

my $workline = 1;
sub write_to_line {
	my ($toline) = @_;
	while ($workline < $toline) {
		print scalar(<ORIG>);
		$workline++;
	}
}

sub skip_lines {
	my ($lines) = @_;
	while ($lines--) {
		$workline++;
		my $discard = <ORIG>;
	}
}

sub get_command {
	($opcode, $line, $count) = /(.)(\d+) (\d+)$/;
	if (defined($count)) {
		if ($opcode eq "d") {
			write_to_line($line);
			skip_lines($count);
		} elsif ($opcode eq "a") {
			write_to_line($line+1);
			$handler = \&add_data;
		} else {
			die "unexpected opcode!"
		}
	} else {
		die "bogus command: $_"
	}
}

sub add_data {
	print;
	unless (--$count) {
		$handler = \&get_command;
	}
}


$handler = \&get_command;

while (<DELTA>) {
	&{$handler}();
}

while (<ORIG>) {
	print;
}


