use strict;

my @protected_labels = (
'devicenormative:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Interrupt Suppression',
'devicenormative:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Notification Suppression',
'devicenormative:General Initialization And Device Operation / Device Operation / Supplying Buffers to The Device / Virtqueue Notification Suppression',
'devicenormative:Virtio Transport Options / Virtio Over PCI Bus / PCI-specific Initialization And Device Operation / Virtqueue Interrupts From The Device',
'drivernormative:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Interrupt Suppression',
'drivernormative:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Notification Suppression',
'drivernormative:General Initialization And Device Operation / Device Operation / Supplying Buffers to The Device / Notifying The Device',
'drivernormative:General Initialization And Device Operation / Device Operation / Supplying Buffers to The Device / Updating idx',
'sec:Basic Facilities of a Virtio Device / Virtqueues / Virtqueue Interrupt Suppression',
'sec:General Initialization And Device Operation / Device Operation / Receiving Used Buffers From The Device',
'sec:Virtio Transport Options / Virtio Over PCI Bus / PCI-specific Initialization And Device Operation / Notifying The Device',
'sec:Virtio Transport Options / Virtio Over PCI Bus / PCI-specific Initialization And Device Operation / Virtqueue Interrupts From The Device',
'itm:General Initialization And Device Operation / Device Operation / Supplying Buffers to The Device / Place Buffers',
'itm:General Initialization And Device Operation / Device Operation / Supplying Buffers to The Device / Place Index',
);
my $lstlisting=0;
my $add=undef;

while (<>) {
	my $line = $_;
	if (m/%DIFDELCMD\s+<\s+\\begin\{lstlisting\}/) {
		$lstlisting=1;
		$add=0;
		$line =~s/(\\begin\{lstlisting\})/{\\lstset{escapechar=\\\$} $1/;
	}
	if (m/\\DIFaddbegin\s*\\begin\{lstlisting\}/) {
		$lstlisting=1;
		$add=1;
		$line =~s/(\\begin\{lstlisting\})/{\\lstset{escapechar=\\\$} $1/;
	}
	if ($lstlisting) {
		$line =~ s/%DIFDELCMD\s+< //;
		if (not $line =~ m/\\(?:begin|end)\{lstlisting\}/) {
			$line =~ s/([#&{} ])/\\$1/g;
			if ($add) {
				$line =~ s/(.*)/\$\\DIFdel\{$1\}\$/;
			} else {
				$line =~ s/(.*)/\$\\DIFadd\{$1\}\$/;
			}
		}
		#print "%FIXED BY RULE 1\n";
	}
	if ($line =~ m/%DIFDELCMD\s+<\s+\\label\{([^}]*)\}/) {
		my $label = $1;
		foreach my $l (@protected_labels) {
			if ($l eq  $label) {
				$line =~ s/(%DIFDELCMD\s+<\s+)\\label\{([^}]*)\}/\\label\{$label\}$1/;
			}
		}
	}
	if ($line =~ m/%DIFDELCMD\s+<\s+\\((driver|device)normative)\{[^{]*\{[^{]*\{([^}\\]*)\}/) {
		my $label = "$1:$3";
		foreach my $l (@protected_labels) {
			if ($l eq $label) {
				$line =~ s/(%DIFDELCMD\s+<\s+)\\((driver|device)normative)\{[^{]*\{[^{]*\{([^}\\]*)\}/\\label\{$label\}$1/;
			}
		}
	}

	# Too many \color directives (generated by DIFdel/addbegin/end)
	# confuse xetex, producing errors:
	# WARNING ** Color stack overflow. Just ignore.
	# and resulting in corrupted color in output.
	# As a work-around, detect cases where it's safe, and replace \color with
	# \textcolor.
	# As a result, number of \color directives goes does sufficiently
	# enough to avoid the overflow error.

	if ($line =~ m/\\drivernormative|\\devicenormative/) {
		#3rd argument in normative statements is a label. Don't put it in diffs.
		$line =~ s/(normative\{[^{]*\{[^{]*\{[^{\\]*)\\DIFdelbegin \\DIFdel\{([^}]*)\}\\DIFdelend/$1$2/;
		$line =~ s/(normative\{[^{]*\{[^{]*\{[^{\\]*)\\DIFaddbegin \\DIFadd\{([^}]*)\}\\DIFaddend/$1$2/;
		$line =~ s/(normative\{[^{]*\{[^{]*\{[^{\\]*)\\DIFdel\{([^}]*)\}/$1$2/;
		$line =~ s/(normative\{[^{]*\{[^{]*\{[^{\\]*)\\DIFadd\{([^}]*)\}/$1$2/;
	} elsif ($line =~ m/section|paragraph/) {
		$line =~ s/\\DIFdelbegin \\DIFdel\{([^}]*)\}\\DIFdelend/\\DIFdel{$1}/;
		$line =~ s/\\DIFaddbegin \\DIFadd\{([^}]*)\}\\DIFaddend/\\DIFadd{$1}/;
	} else {
		$line =~ s/\\DIFdelbegin \\DIFdel\{([^}]*)\}\\DIFdelend/\\DIFdeltext{$1}/;
		$line =~ s/\\DIFaddbegin \\DIFadd\{([^}]*)\}\\DIFaddend/\\DIFaddtext{$1}/;
	}

	print $line;
	if ($lstlisting) {
		if (m/\\end\{lstlisting\}/) {
			print "}\n";
			$lstlisting=0;
			$add=undef;
		}
	}
}
