include @srcdir@/../upgradeable_versions.mk

EXTENSION    = postgis_tiger_geocoder
EXTVERSION    = @POSTGIS_LIB_VERSION@
MINORVERSION  = 2011.@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
GREP = @GREP@
PERL = @PERL@

MICRO_NUMBER  = $(shell echo $(EXTVERSION) | \
						$(PERL) -pe 's/\d.\d.(\d+)[a-zA-Z]*\d*/$1/'

PREREL_NUMBER = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/\d+[a-zA-Z]+(\d+)/\1/'

MICRO_PREV    = $(shell if test "$(MICRO_NUMBER)x" != "x"; then expr $(MICRO_NUMBER) - 1; fi)
PREREL_PREV   = $(shell if test "$(PREREL_NUMBER)x" != "x"; then expr $(PREREL_NUMBER) - 1; fi)

PREREL_PREFIX = $(shell echo $(EXTVERSION) | \
                        $(PERL) -pe 's/\d\.\d\.(.*)/\1/' | \
                        $(GREP) "[a-zA-Z]" | \
                        $(PERL) -pe 's/(\d+[a-zA-Z]+)\d*/\1/'

DATA_built = \
	$(EXTENSION).control \
	sql/$(EXTENSION)--$(EXTVERSION).sql \
	sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql \
	$(NULL)

REGRESS = test-normalize_address test-upgrade
REGRESS_OPTS = --load-extension=fuzzystrmatch --load-extension=postgis --load-extension=$(EXTENSION)

SQL_BITS     = $(wildcard sql_bits/*.sql)
EXTRA_CLEAN = sql expected ${SQL_BITS}

#test address standardizer if also built
ifeq (@ADDRESS_STANDARDIZER@,address_standardizer)
	REGRESS +=  test-pagc_normalize_address
	REGRESS_OPTS += --load-extension=address_standardizer
endif

all: sql/$(EXTENSION)_pre.sql sql/$(EXTENSION)--$(EXTVERSION).sql sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql sql/$(EXTENSION)--ANY--$(EXTVERSION).sql sql/test-normalize_address.sql sql/test-pagc_normalize_address.sql expected/test-normalize_address.out expected/test-pagc_normalize_address.out sql/test-upgrade.sql expected/test-upgrade.out


.PHONY: expected
expected:
	mkdir -p $@

sql:
	mkdir -p $@

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql | sql
	cp $< $@

expected/test-normalize_address.out: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/normalize_address_regress | expected
	cat $^ > $@

expected/test-pagc_normalize_address.out: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/pagc_normalize_address_regress | expected
	cat $^ > $@

## adding \t .. to force output to be the same as what get in regular regress in tiger folder
sql/test-normalize_address.sql: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/normalize_address_regress.sql | sql
	cat $^ > $@

sql/test-upgrade.sql: | sql
	echo 'ALTER EXTENSION ${EXTENSION} UPDATE TO "ANY"; ALTER EXTENSION ${EXTENSION} UPDATE TO "$(EXTVERSION)"' > $@

%.control: %.control.in Makefile
	cat $< \
		| $(PERL) -lpe "s'@EXTVERSION@'$(EXTVERSION)'g" \
		> $@

expected/test-upgrade.out: sql/test-upgrade.sql | expected
	cp $< $@

sql/test-pagc_normalize_address.sql: sql_bits/test_tuples_only_unaligned.sql.in ../../extras/tiger_geocoder/regress/pagc_normalize_address_regress.sql | sql
	cat $^ > $@

sql/$(EXTENSION)_pre.sql: sql/tiger_geocoder.sql  sql_bits/mark_editable_objects.sql.in | sql
	cat $^ > $@

sql/$(EXTENSION).sql: sql/$(EXTENSION)_pre.sql ../../utils/create_or_replace_to_create.pl
	cat $< \
		| $(PERL) @top_srcdir@/utils/create_or_replace_to_create.pl sql/$(EXTENSION)_pre.sql \
		> $@

sql/$(EXTENSION)--ANY--$(EXTVERSION).sql: ../postgis_extension_helper.sql sql/tiger_geocoder_upgrade_minor.sql sql_bits/mark_editable_objects.sql.in | sql
	cat $^ > $@
	echo "SELECT postgis_extension_drop_if_exists('${EXTENSION}', 'DROP SCHEMA tiger_data');" >> $@
	cat @srcdir@/../postgis_extension_helper_uninstall.sql  >> $@

sql/$(EXTENSION)--$(EXTVERSION)next--$(EXTVERSION).sql: sql/$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql | sql
	cp $< $@

#strip BEGIN/COMMIT since these are not allowed in extensions
#strip CREATE SCHEMA since we force extension
# to create schema by setting schema to tiger_geocoder in control
#also remove tiger_data from extension if it is part of it
sql/tiger_geocoder_minor.sql.in: ../../extras/tiger_geocoder/utility/set_search_path.sql \
  sql_bits/upgrade_before.sql.in \
  	../../extras/tiger_geocoder/geocode_settings.sql \
  ../../extras/tiger_geocoder/tiger_loader_2024.sql \
	../../extras/tiger_geocoder/utility/utmzone.sql \
	../../extras/tiger_geocoder/utility/cull_null.sql \
	../../extras/tiger_geocoder/utility/nullable_levenshtein.sql \
	../../extras/tiger_geocoder/utility/levenshtein_ignore_case.sql \
	../../extras/tiger_geocoder/normalize/end_soundex.sql \
	../../extras/tiger_geocoder/normalize/count_words.sql \
	../../extras/tiger_geocoder/normalize/state_extract.sql \
	../../extras/tiger_geocoder/normalize/get_last_words.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract.sql \
	../../extras/tiger_geocoder/normalize/normalize_address.sql \
	../../extras/tiger_geocoder/normalize/pprint_addy.sql \
	../../extras/tiger_geocoder/pagc_normalize/pagc_tables.sql \
  ../../extras/tiger_geocoder/pagc_normalize/pagc_normalize_address.sql \
	../../extras/tiger_geocoder/geocode/other_helper_functions.sql \
	../../extras/tiger_geocoder/geocode/rate_attributes.sql \
	../../extras/tiger_geocoder/geocode/includes_address.sql \
	../../extras/tiger_geocoder/geocode/interpolate_from_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_location.sql \
	../../extras/tiger_geocoder/geocode/geocode_intersection.sql \
	../../extras/tiger_geocoder/geocode/geocode.sql \
	../../extras/tiger_geocoder/geocode/reverse_geocode.sql \
	../../extras/tiger_geocoder/geocode/census_tracts_functions.sql \
	| sql
	cat $^ > $@

#also remove tiger_data from extension if it is part of it so data gets backed up
sql/tiger_geocoder.sql.in: sql_bits/norm_addy_create.sql.in \
	../../extras/tiger_geocoder/utility/set_search_path.sql \
	../../extras/tiger_geocoder/geocode_settings.sql \
	../../extras/tiger_geocoder/tables/lookup_tables_2011.sql \
	../../extras/tiger_geocoder/tiger_loader_2024.sql \
	../../extras/tiger_geocoder/utility/set_search_path.sql \
	../../extras/tiger_geocoder/utility/utmzone.sql \
	../../extras/tiger_geocoder/utility/cull_null.sql \
	../../extras/tiger_geocoder/utility/nullable_levenshtein.sql \
	../../extras/tiger_geocoder/utility/levenshtein_ignore_case.sql \
	../../extras/tiger_geocoder/normalize/end_soundex.sql \
	../../extras/tiger_geocoder/normalize/count_words.sql \
	../../extras/tiger_geocoder/normalize/state_extract.sql \
	../../extras/tiger_geocoder/normalize/get_last_words.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_countysub_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_exact.sql \
	../../extras/tiger_geocoder/normalize/location_extract_place_fuzzy.sql \
	../../extras/tiger_geocoder/normalize/location_extract.sql \
	../../extras/tiger_geocoder/normalize/normalize_address.sql \
	../../extras/tiger_geocoder/normalize/pprint_addy.sql \
	../../extras/tiger_geocoder/pagc_normalize/pagc_tables.sql \
    ../../extras/tiger_geocoder/pagc_normalize/pagc_normalize_address.sql \
	../../extras/tiger_geocoder/geocode/other_helper_functions.sql \
	../../extras/tiger_geocoder/geocode/rate_attributes.sql \
	../../extras/tiger_geocoder/geocode/includes_address.sql \
	../../extras/tiger_geocoder/geocode/interpolate_from_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_address.sql \
	../../extras/tiger_geocoder/geocode/geocode_location.sql \
	../../extras/tiger_geocoder/geocode/geocode_intersection.sql \
	../../extras/tiger_geocoder/geocode/geocode.sql \
	../../extras/tiger_geocoder/geocode/reverse_geocode.sql \
	../../extras/tiger_geocoder/geocode/census_tracts_functions.sql \
	../../extras/tiger_geocoder/topology/tiger_topology_loader.sql \
	../postgis_extension_helper.sql \
	sql/add_search_path.sql | sql
	cat $^ > $@
	echo "SELECT postgis_extension_drop_if_exists('${EXTENSION}', 'DROP SCHEMA tiger_data');" >> $@
	cat @srcdir@/../postgis_extension_helper_uninstall.sql  >> $@

sql/tiger_geocoder.sql: sql/tiger_geocoder.sql.in
	sed -e 's/BEGIN;//g' -e 's/COMMIT;//g' -e '/^CREATE SCHEMA/d;'  $< > $@
	$(PERL) -pe 's/BEGIN\;//g ; s/COMMIT\;//g' $< > $@

sql/add_search_path.sql: sql_bits/add_search_path.sql.in | sql
	cp $< $@

#hardcode for now
#TODO: generate via create_unpackaged.pl
sql/$(EXTENSION)--unpackaged--$(EXTVERSION).sql: sql_bits/tiger_geocoder--unpackaged.sql.in | sql
	cp $< $@

#upgrade script should have everything but table, schema, type creation/alter
#NOTE: we assume all object definitions end in ;
#first expression deletes all non-removable objects defined on same line
#second deletes all non-removable defined on multiple lines
#  the end of the body of object we assume ends in ;
#aggregates are special
#they can be dropped but we need to remove
#them from the extension first
sql/tiger_geocoder_upgrade_minor.sql:  sql/tiger_geocoder_minor.sql.in | sql
	sed -e 's/BEGIN;//g' -e 's/COMMIT;//g' \
	 	 $< > $@

#only extension files
EXTRA_CLEAN += $(wildcard expected/*--*.out)

include @srcdir@/../upgrade-paths-rules.mk

distclean: clean
	rm Makefile

PG_CONFIG := @PG_CONFIG@
PGXS := @PGXS@
include $(PGXS)
PERL=@PERL@
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
ifneq (@MKDIR_P@,)
	MKDIR_P = @MKDIR_P@
endif

VPATH = @srcdir@

top_srcdir = @top_srcdir@
abs_topbuilddir=$(abspath $(top_builddir))
