############################################################################
#
# Copyright (c) 2009-2011 Sandro Santilli <strk@kbt.io>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#############################################################################

MODULE_NAME=postgis_raster

POSTGIS_PGSQL_VERSION=@POSTGIS_PGSQL_VERSION@
MODULE_big=$(MODULE_NAME)-@POSTGIS_MAJOR_VERSION@
MODULEDIR=contrib/postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULEPATH    = $$libdir/$(MODULE_NAME)-@POSTGIS_MAJOR_VERSION@

ifeq (@LIBINCLUDEMINORVERSION@,yes)
MODULEPATH=$$libdir/$(MODULE_NAME)-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULE_big=$(MODULE_NAME)-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
endif

# Files to be copied to the contrib/ directory
DATA_built=rtpostgis.sql rtpostgis_upgrade.sql uninstall_rtpostgis.sql rtpostgis_legacy.sql
DATA=

# SQL preprocessor
SQLPP = @SQLPP@

# SQL objects (files requiring pre-processing)
SQL_OBJS=rtpostgis.sql rtpostgis_drop.sql rtpostgis_upgrade_cleanup.sql rtpostgis_legacy.sql

# Objects to build using PGXS
OBJS = \
	rtpostgis.o \
	rtpg_internal.o \
	rtpg_legacy.o \
	rtpg_spatial_relationship.o \
	rtpg_mapalgebra.o \
	rtpg_utility.o \
	rtpg_inout.o \
	rtpg_wkb.o \
	rtpg_geometry.o \
	rtpg_raster_properties.o \
	rtpg_band_properties.o \
	rtpg_pixel.o \
	rtpg_create.o \
	rtpg_gdal.o \
	rtpg_statistics.o

# Libraries to link into the module (proj, geos)
#
# Note: we specify liblwgeom.a directly in SHLIB_LINK rather than using
# -L... -l options to prevent issues with some platforms trying to link
# to an existing liblwgeom.so in the PostgreSQL $libdir supplied by an
# older version of PostGIS, rather than with the static liblwgeom.a
# supplied with newer versions of PostGIS
#
LIBLWGEOM_LDFLAGS = @top_builddir@/liblwgeom/.libs/liblwgeom.a
LIBLWGEOM_CFLAGS = -I@top_srcdir@/liblwgeom -I@top_builddir@/liblwgeom
LIBPGCOMMON_CFLAGS = -I@top_srcdir@/libpgcommon
LIBPGCOMMON_LDFLAGS = @top_builddir@/libpgcommon/libpgcommon.a
LIBGDAL_CFLAGS=@LIBGDAL_CFLAGS@
LIBGDAL_LDFLAGS=@LIBGDAL_LDFLAGS@
LIBPROJ_CFLAGS=@PROJ_CPPFLAGS@

CC = @CC@
PG_CPPFLAGS += \
	$(LIBLWGEOM_CFLAGS) \
	@CPPFLAGS@ \
	@CFLAGS@ \
	$(LIBGDAL_CFLAGS) \
	$(LIBPGCOMMON_CFLAGS) \
	$(LIBPROJ_CFLAGS) \
	-I@srcdir@/../rt_core \
	-I@builddir@ \
	-I@builddir@/.. \
	-I@top_builddir@
SHLIB_LINK_F = @builddir@/../rt_core/librtcore.a $(LIBLWGEOM_LDFLAGS) $(LIBPGCOMMON_LDFLAGS) $(LIBGDAL_LDFLAGS) @SHLIB_LINK@

# Extra files to remove during 'make clean'
EXTRA_CLEAN=$(SQL_OBJS) $(DATA_built) rtpostgis_upgrade.sql.in

# PGXS information
PG_CONFIG := @PG_CONFIG@
PGXS := @PGXS@
# NO_TEMP_INSTALL is a workaround for a 9.5dev bug. See:
# http://www.postgresql.org/message-id/CAB7nPqTsR5o3g-fBi6jbsVdhfPiLFWQ_0cGU5=94Rv_8W3qvFA@mail.gmail.com
NO_TEMP_INSTALL=yes
include $(PGXS)

VPATH = @srcdir@

# Utility rule for use from outside
sql_objs: $(DATA_built)

# Set PERL _after_ the include of PGXS
PERL=@PERL@

# This is to workaround a bug in PGXS 8.4 win32 link line,
# see http://trac.osgeo.org/postgis/ticket/1158#comment:57
SHLIB_LINK := $(SHLIB_LINK_F) $(SHLIB_LINK)

# If REGRESS=1 passed as a parameter, change the default install paths
# so that no prefix is included. This allows us to relocate to a temporary
# directory for regression testing.
ifeq ($(REGRESS),1)
        bindir=/bin
        pkglibdir=/lib
        datadir=/share
        datamoduledir=contrib/postgis
endif

# Borrow the $libdir substitution from PGXS but customise by running the preprocessor
# and adding the version number
# replace @extschema@. with nothing, this is only used as placeholder for extension install
#
# TODO: create an intermediate product with @extschema@ in place, to reduce rules duplications
#       in the extension/ folder
#
%.sql: %.sql.in
	$(SQLPP) -I@top_builddir@/postgis/ $< > $@.tmp
	grep -v '^#' $@.tmp | \
	$(PERL) -lpe "s'MODULE_PATHNAME'\$(MODULEPATH)'g;s'@extschema@\.''g" > $@
	rm -f $@.tmp

../../liblwgeom/.libs/liblwgeom.a:
	$(MAKE) -C ../../liblwgeom

../rt_core/librtcore.a:
	$(MAKE) -C ../rt_core

../../libpgcommon/libpgcommon.a:
	$(MAKE) -C ../../libpgcommon

# Objects dependencies
# NOTE: the .a deps are really only link-time dependencies, but at this
#       time I dunno how to wncode those
$(OBJS): ../../liblwgeom/.libs/liblwgeom.a ../rt_core/librtcore.a ../../libpgcommon/libpgcommon.a ../../postgis_config.h ../../postgis_revision.h

# SQL objects deps here
$(SQL_OBJS): ../../postgis/sqldefines.h ../../postgis_revision.h

#this is redundant but trying to fold in with the  other just hangs
rtpostgis_upgrade.sql.in: rtpostgis.sql ../../utils/create_upgrade.pl
	$(PERL) @top_srcdir@/utils/create_upgrade.pl $< > $@

rtpostgis_upgrade.sql: ../../postgis/common_before_upgrade.sql rtpostgis_upgrade_cleanup.sql rtpostgis_upgrade.sql.in rtpostgis_drop.sql ../../postgis/common_after_upgrade.sql
	echo 'BEGIN;' > $@
	cat $^ | sed -e 's/^BEGIN;$$//' -e 's/^COMMIT;$$//' >> $@
	echo 'COMMIT;' >> $@

uninstall_rtpostgis.sql: rtpostgis.sql ../../utils/create_uninstall.pl
	$(PERL) @top_srcdir@/utils/create_uninstall.pl $< $(POSTGIS_PGSQL_VERSION) > $@

distclean: clean
	rm -f Makefile
