0x3E8
  B   ˆl–Ým_J•4Ÿº‚ „ ¸n2Ú˜´o�þDÑ%›,�¶ô­l�ÏÂ…`mÁÁ_ˆI|¥�+V[(ÁÀ ?÷     #!/usr/bin/perl

#
# PostGIS - Spatial Types for PostgreSQL
# http://postgis.net
#
# Copyright (C) 2011 OpenGeo.org
# Copyright (C) 2009 Paul Ramsey <pramsey@cleverelephant.ca>
#
# This is free software; you can redistribute and/or modify it under
# the terms of the GNU General Public Licence. See the COPYING file.
#
#---------------------------------------------------------------------
#
# This script is aimed at restoring postgis data
# from a dumpfile produced by pg_dump -Fc
#
# Basically it will restore all but things known to belong
# to postgis. Will also convert some old known constructs
# into new ones.
#
# Tested on:
#
#    pg-8.4.9/pgis-1.4.3    => pg-8.4.9/pgis-2.0.0SVN
#    pg-8.4.9/pgis-2.0.0SVN => pg-8.4.9/pgis-2.0.0SVN
#    pg-8.4.9/pgis-2.0.0SVN => pg-9.1.2/pgis-2.0.0SVN
#    pg-9.1b3/pgis-1.5.3    => pg-9.1.1/pgis-2.0.0SVN
#
#---------------------------------------------------------------------

use warnings;
use strict;

my $me = $0;

my $usage = qq{
Usage:	$me [-v] [-L TOC] [-s schema] <dumpfile>
        Restore a custom dump (pg_dump -Fc) of a PostGIS-enabled database.
        First dump the old database: pg_dump -Fc MYDB > MYDB.dmp
        Then create a new database: createdb NEWDB
        Then install PostGIS in the new database:
           psql -f postgis/postgis.sql NEWDB
        Also install PostGIS topology and raster, if you were using them:
           psql -f topology/topology.sql NEWDB
           psql -f raster/rtpostgis.sql NEWDB
        Finally, pass the dump to this script and feed output to psql:
           $me MYDB.dmp | psql NEWDB
        The -v switch writes detailed report on stderr.
        Use -L to provide a TOC rather than extracting it from the dump.
        Use -s if you installed PostGIS in a custom schema.

};

my $DEBUG = 0;
my $POSTGIS_SCHEMA;
my $POSTGIS_TOC;

# NOTE: the SRID limits here are being discussed:
# http://lists.osgeo.org/pipermail/postgis-devel/2012-February/018440.html
my $SRID_MAXIMUM = 999999;
my $SRID_USER_MAXIMUM = 998999;

while (@ARGV && $ARGV[0] =~ /^-/) {
  my $arg = shift(@ARGV);
  if ( $arg eq '-v' ) {
    $DEBUG = 1;
  }
  elsif ( $arg eq '-s' ) {
    $POSTGIS_SCHEMA = shift(@ARGV);
  }
  elsif ( $arg eq '-L' ) {
    $POSTGIS_TOC = shift(@ARGV);
  }
  elsif ( $arg eq '--' ) {
    last;
  }
  else {
    print STDERR "Unknown switch " . $arg;
    die $usage;
  }
}

die $usage if (@ARGV < 1);

my $dumpfile = $ARGV[0];
my $manifest = $dumpfile;
$manifest =~ s/\/$//; # strip trailing slash
$manifest .= ".lst";
my $hasTopology = 0;

die "$me:\tUnable to find 'pg_dump' on the path.\n" if ! `pg_dump --version`;
die "$me:\tUnable to find 'pg_restore' on the path.\n" if ! `pg_restore --version`;
die "$me:\tUnable to open dump file '$dumpfile'.\n" if ! -r $dumpfile;

print STDERR "Converting $dumpfile to ASCII on stdout...\n";

STDOUT->autoflush(1);

######################################################################
# Load the signatures of things to skip.
#

print STDERR "  Reading list of functions to ignore...\n";

my %skip = ();
while(my $l = <DATA>) {
  chop($l);
  print STDERR "DATA: $l\n" if $DEBUG;
  $l =~ s/\s//g;
  $skip{$l} = 1;
}

######################################################################
# Write a new manifest for the dump file, skipping the things that
# are part of PostGIS
#

if(!defined($POSTGIS_TOC)) {
  print STDERR "  Writing manifest of things to read from dump file...\n";

  open( DUMP, "pg_restore -f - -l $dumpfile |" ) || die "$me:\tCannot open dump file '$dumpfile'\n";
} else {
  open( DUMP, '<' . $POSTGIS_TOC) || die "$me:\tCannot open TOC file '$POSTGIS_TOC'\n";
}
open( MANIFEST, ">$manifest" ) || die "$me:\tCannot open manifest file '$manifest'\n";
while( my $l = <DUMP> ) {

  next if $l =~ /^\;/;
  my $sigHR = linesignature($l);
  my $sig = $sigHR; $sig =~ s/\s//g;
  $hasTopology = 1 if $sig eq 'SCHEMAtopology';

	if ( not defined ($POSTGIS_SCHEMA) )
	{
		if ( $l =~ / TABLE DATA ([^ ]*) spatial_ref_sys / )
		{
			$POSTGIS_SCHEMA