# This is a rather crude way to strip tsearch stuff from a PostgreSQL 8.1
# dump.
#
# Thomas Munro <munro@ip9.org>

import re
import sys

functions = ( "gtsvector_in",
              "gtsvector_out",
              "tsquery_in",
              "tsquery_out",
              "_get_parser_from_curcfg",
              "concat",
              "dex_[a-z_]+",
              "dex_lexize",
              "exectsq",
              "exectsq",
              "get_covers",
              "gtsvector_[a-z_]+",
              "headline",
              "length",
              "lexize",
              "parse",
              "prsd_[a-z_]+",
              "querytree",
              "rank",
              "rank_cd",
              "reset_tsearch",
              "rexectsq",
              "set_curcfg",
              "set_curdict",
              "set_curprs",
              "setweight",
              "show_curcfg",
              "snb_[a-z_]+",
              "spell_[a-z_]+",
              "stat",
              "strip",
              "syn_[a-z_]+",
              "to_tsquery",
              "to_tsvector",
              "token_type",
              "ts_debug",
              "tsearch2",
              "tsvector_[a-z_]+" )

types = ( "gtsvector", "tsquery", "tsvector", "statinfo", "tokenout",
          "tokentype", "tsdebug" )

tables = ( "pg_ts_[a-z_]+", )

ddl_patterns = [ "^CREATE FUNCTION %s\\(" % x for x in functions ] + \
               [ "^ALTER FUNCTION public\\.%s\\(" % x for x in functions ] + \
               [ "^CREATE TYPE %s " % x for x in types ] + \
               [ "^ALTER TYPE public\\.%s " % x for x in types ] + \
               [ "^CREATE TABLE %s " % x for x in tables ] + \
               [ "^ALTER TABLE public\\.%s " % x for x in tables ] + \
               [ "^ALTER TABLE ONLY %s$" % x for x in tables ] + \
               [ "^REVOKE .* ON TABLE %s " % x for x in tables ] + \
               [ "^GRANT .* ON TABLE %s " % x for x in tables ] + \
               [ "^CREATE OPERATOR ", "^ALTER OPERATOR " ]
ddl_compiled = map(re.compile, ddl_patterns)
end_ddl_pattern = re.compile(".*;")

data_patterns = [ "^COPY %s " % x for x in tables ]
data_compiled = map(re.compile, data_patterns)
end_data_pattern = re.compile("^\\\\.$")

skip_ddl = False
skip_data = False
for line in sys.stdin.xreadlines():
    if skip_ddl:
        sys.stdout.write("--- [skipped DDL] ")
        sys.stdout.write(line)
        if end_ddl_pattern.match(line):
            skip_ddl = False
    elif skip_data:
        sys.stdout.write("--- [skipped data] ")
        sys.stdout.write(line)
        if end_data_pattern.match(line):
            skip_data = False
    else:
        for pattern in ddl_compiled:
            if pattern.match(line):
                skip_ddl = True
                break
        if skip_ddl:
            sys.stdout.write("--- [skipped DDL] ")
            sys.stdout.write(line)
            if end_ddl_pattern.match(line):
                skip = False
        else:
            for pattern in data_compiled:
               if pattern.match(line):
                  skip_data = True
                  break
            if skip_data:
               sys.stdout.write("--- [skipped data] ")
               sys.stdout.write(line)
            else:
               sys.stdout.write(line)

