#!/usr/bin/perl
#
# Generate the errcodes-table.sgml file from errcodes.txt
# Copyright (c) 2000-2013, PostgreSQL Global Development Group
use warnings;
use strict;
print
  "\n";
open my $errcodes, $ARGV[0] or die;
while (<$errcodes>)
{
	chomp;
	# Skip comments
	next if /^#/;
	next if /^\s*$/;
	# Emit section headers
	if (/^Section:/)
	{
		# Remove the Section: string
		s/^Section: //;
		# Escape dashes for SGML
		s/-/—/;
		# Wrap PostgreSQL in 
		s/PostgreSQL/PostgreSQL<\/>/g;
		print "\n\n";
		print "\n";
		print "";
		print "$_>\n";
		print "
\n";
		next;
	}
	die unless /^([^\s]{5})\s+([EWS])\s+([^\s]+)(?:\s+)?([^\s]+)?/;
	(my $sqlstate, my $type, my $errcode_macro, my $condition_name) =
	  ($1, $2, $3, $4);
	# Skip lines without PL/pgSQL condition names
	next unless defined($condition_name);
	print "\n";
	print "\n";
	print "$sqlstate\n";
	print "$condition_name\n";
	print "
\n";
}
close $errcodes;