#############################################################################
#
# Copyright (c) 2011 Regents of the University of California
#   <bkpark@ucdavis.edu>
# Copyright (c) 2009-2022 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.
#
#############################################################################

top_builddir = @top_builddir@
builddir = @builddir@
top_srcdir = @top_srcdir@
srcdir = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@

VPATH = $(srcdir)

CC=@CC@
SHELL = @SHELL@
INSTALL = @INSTALL@
LIBTOOL = @LIBTOOL@

# Filenames with extension as determined by the OS
RASTER2PGSQL=raster2pgsql@EXESUFFIX@

# PostgreSQL executable directory
PGSQL_BINDIR=@PGSQL_BINDIR@

LIBLWGEOM_LDFLAGS = $(top_builddir)/liblwgeom/liblwgeom.la
LIBLWGEOM_CFLAGS = -I$(top_builddir)/liblwgeom -I$(top_srcdir)/liblwgeom
LIBGDAL_CFLAGS=@LIBGDAL_CFLAGS@
LIBGDAL_LDFLAGS=@LIBGDAL_LDFLAGS@
LIBGDAL_DEPLIBS_LDFLAGS=@LIBGDAL_DEPLIBS_LDFLAGS@
PROJ_CFLAGS=@PROJ_CPPFLAGS@
PROJ_LDFLAGS=@PROJ_LDFLAGS@
GEOS_CFLAGS=@GEOS_CPPFLAGS@
GEOS_LDFLAGS=@GEOS_LDFLAGS@

RTCORE_CFLAGS = -I$(srcdir)/../rt_core -I$(builddir)/.. -I$(builddir) -I$(top_builddir)
RTCORE_LDFLAGS = $(builddir)/../rt_core/librtcore.a

# GetText includes and libraries
GETTEXT_CFLAGS = @GETTEXT_CFLAGS@
GETTEXT_LDFLAGS = @GETTEXT_LDFLAGS@ @LIBINTL@

# iconv flags
ICONV_LDFLAGS=@ICONV_LDFLAGS@
ICONV_CFLAGS=@ICONV_CFLAGS@

CFLAGS = \
	@CFLAGS@ @PICFLAGS@ \
	$(RTCORE_CFLAGS) \
	$(LIBLWGEOM_CFLAGS) \
	$(PROJ_CFLAGS) \
	$(LIBGDAL_CFLAGS) \
	$(GEOS_CFLAGS) \
	$(GETTEXT_CFLAGS) \
	$(ICONV_CFLAGS)

LDFLAGS = \
	@LDFLAGS@ \
	$(LIBLWGEOM_LDFLAGS) \
	$(LIBGDAL_LDFLAGS) \
	$(LIBGDAL_DEPLIBS_LDFLAGS) \
	$(GEOS_LDFLAGS) \
	$(PROJ_LDFLAGS) \
	$(GETTEXT_LDFLAGS) \
	$(ICONV_LDFLAGS)

all: $(RASTER2PGSQL)

raster2pgsql.o: raster2pgsql.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $<

$(RASTER2PGSQL): ../rt_core/librtcore.a raster2pgsql.o
	$(LIBTOOL) --mode=link $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@

installdir:
	@mkdir -p $(DESTDIR)$(bindir)

install: installdir
	$(LIBTOOL) --mode=install $(INSTALL) $(RASTER2PGSQL) "$(DESTDIR)$(bindir)/$(RASTER2PGSQL)"

uninstall:
	$(LIBTOOL) --mode=uninstall $(RM) "$(DESTDIR)$(bindir)/$(RASTER2PGSQL)"

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

clean:
	rm -rf .libs
	rm -f *.o $(RASTER2PGSQL)

distclean: clean
	rm -f Makefile
