#!/usr/bin/perl

use strict;
use warnings;

my %oidcounts;

BEGIN
{
	@ARGV = (glob("pg_*.h"), glob("pg_*.dat"), qw(indexing.h toasting.h));
}

while (<>)
{
	next if /^CATALOG\(.*BKI_BOOTSTRAP/;
	next
	  unless /\boid *=> *'(\d+)'/
		  || /^CATALOG\([^,]*, *(\d+).*BKI_ROWTYPE_OID\((\d+),/
		  || /^CATALOG\([^,]*, *(\d+)/
		  || /^DECLARE_INDEX\([^,]*, *(\d+)/
		  || /^DECLARE_UNIQUE_INDEX\([^,]*, *(\d+)/
		  || /^DECLARE_TOAST\([^,]*, *(\d+), *(\d+)/;
	$oidcounts{$1}++;
	$oidcounts{$2}++ if $2;
}

if ($0 eq 'duplicate_oids')
{
	my $found = 0;

	foreach my $oid (sort { $a <=> $b } keys %oidcounts)
	{
		next unless $oidcounts{$oid} > 1;
		$found = 1;
		print "$oid\n";
	}

	exit $found;
}
elsif ($0 eq 'unused_oids')
{
	my $firstobj;
	open (my $handle,"../access/transam.h") || die "opening transam.h";
	while (<$handle>)
	{
		next unless /^#define\s+FirstBootstrapObjectId\s+(\d+)/;
		$firstobj = $1;
		last;
	}
	close($handle);
	my @sortedoids = sort { $a <=> $b } keys %oidcounts;
	my $last = 0;
	foreach my $oid (@sortedoids )
	{
		if ($oid > $last + 1)
		{
			if ($oid > $last + 2)
			{
				print $last + 1, " - ", $oid - 1, "\n";
			}
			else
			{
				print $last + 1,"\n";
			}
		}
		$last = $oid;
	}
	print $last + 1," - ", $firstobj - 1,"\n";
	exit 0;
}
