#!/usr/bin/perl -w

# Extracts the top- and high-priority OL Dump records.

use warnings 'FATAL', 'all';
# Make every warning fatal.

use strict;
# Require strict checking of variable references, etc.

use utf8;
# Make Perl interpret the script and standard files as UTF-8 rather than bytes.

open IN, '<:utf8', 'ol_dump_2012-04-30.txt';
# Open the input file for reading.

open PRI, '<:utf8', 'prisub.txt';
# Open the priority-subject file for reading.

open TOP, '>:utf8', 'booktop.txt';
# Create or truncate the top-priority output file and open it for writing.

open HIGH, '>:utf8', 'bookhigh.txt';
# Create or truncate the high-priority output file and open it for writing.

my (%pri, $pri, @seg, $seg, $whole);

while (<PRI>) {
# For each line of the priority-subject file:

	chomp;
	# Delete its trailing newline.

	@seg = (split /\t/);
	# Identify its segments.

	$pri{$seg[1]} = $seg[0];
	# Add it to the table of priority subjects, with a value identifying its priority.

}

while (<IN>) {
# For each line of the input file:

	$whole = $_;
	# Identify a copy of it.

	if (s/^.+?, "subjects": \["/"/) {
	# Delete the part before the subject list. If anything was deleted:

		if (s/"\].+$/"/) {
		# Delete the part after the subject list before the trailing newline. If anything was deleted:

			chomp;
			# Delete the trailing newline.

			if ((s/^"|"$//g) == 2) {
			# Delete the leading and trailing quotation marks. If both were deleted:

				$pri = '';
				# Initialize the line’s priority as low.

				@seg = (split /", "/);
				# Identify a list of the subjects.

				foreach $seg (@seg) {
				# For each of them:

					$seg =~ s/\.$//;
					# Delete any trailing period.

					$seg =~ s/ *-- *| +- +/ -- /g;
					# Collapse any multiple spaces around single- or double-hyphen-minus element
					# delimiters, and convert any space-enclosed single hyphen-minuses to space-
					# enclosed double hyphen-minuses.

					$seg =~ s/\. \[from old catalog\]$//;
					# Delete any trailing “. [from old catalog]” from it.

					if (exists $pri{$seg}) {
					# If it is top- or high-priority:

						if ($pri{$seg} eq 't') {
						# If it is top-priority:

							print TOP $whole;
							# Output its line to the top-priority file.

							last;
							# Stop processing its line.

						}

						elsif ($pri eq '') {
							# Otherwise, if it is the first high-priority subject in the line:

							$pri = 'h';
							# Identify the line as at least high-priority.

						}

					}

				}

				if ($pri eq 'h') {
				# If the line is high-priority:

					print HIGH $whole;
					# Output it to the high-priority file.

				}

			}

		}

	}

}

close IN;
# Close the input file.

close PRI;
# Close the priority-subject file.

close TOP;
# Close the output file.

close HIGH;
# Close the output file.
