# **********************************************************************
# * $Id: Makefile.in
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.refractions.net
# * Copyright 2008 Mark Cave-Ayland
# *
# * This is free software; you can redistribute and/or modify it under
# * the terms of the GNU General Public Licence. See the COPYING file.
# *
# **********************************************************************

CC=gcc
CFLAGS=-g -O2  -fno-common -DPIC  -Wall -Wmissing-prototypes

# Filenames with extension as determined by the OS
PGSQL2SHP=pgsql2shp
SHP2PGSQL=shp2pgsql
SHP2PGSQL-GUI=shp2pgsql-gui
SHP2PGSQL-CLI=shp2pgsql-cli

# PostgreSQL frontend CPPFLAGS and LDFLAGS (for compiling and linking with libpq)
PGSQL_FE_CPPFLAGS=-I/usr/local/pgsql/8.3/include
PGSQL_FE_LDFLAGS=-L/usr/local/pgsql/8.3/lib -lpq

# iconv flags
ICONV_LDFLAGS=-liconv

# liblwgeom
LIBLWGEOM=../liblwgeom/liblwgeom.a

# GTK includes and libraries
GTK_CFLAGS = 
GTK_LIBS = 

# PGXS information
#
# Note that PGXS currently doesn't handle building FE executables, but we need
# the INSTALL and DESTDIR variables so we can get the correct install paths. 
# Hence we include the PGXS Makefile here, but ensure that we override the
# 'all' and 'install' targets with the ones we really want to use below.
PG_CONFIG = /usr/local/pgsql/8.3/bin/pg_config
PGXS := /usr/local/pgsql/8.3/lib/pgxs/src/makefiles/pgxs.mk
include $(PGXS)


# The real parts of the Makefile
all: $(SHP2PGSQL) $(PGSQL2SHP) 

gui: $(SHP2PGSQL-GUI) $(SHP2PGSQL-CLI)

# liblwgeom.a dependency to allow "make install" in 
# the loader/ subdirectory to work  
$(LIBLWGEOM):
	make -C ../liblwgeom

pgsql2shp.o: pgsql2shp.c
	$(CC) $(CFLAGS) $(PGSQL_FE_CPPFLAGS) -c $<

$(PGSQL2SHP): shpopen.o dbfopen.o getopt.o pgsql2shp.o $(LIBLWGEOM) 
	$(CC) $(CFLAGS) $^ $(ICONV_LDFLAGS) $(PGSQL_FE_LDFLAGS) -lm -o $@ 

$(SHP2PGSQL): shpopen.o dbfopen.o getopt.o  shp2pgsql.o $(LIBLWGEOM) 
	$(CC) $(CFLAGS) $^ $(ICONV_LDFLAGS) -lm -o $@ 

shp2pgsql-core-gui.o: shp2pgsql-core.c
	$(CC) $(CFLAGS) -DPGUI -c -o $@ $^

shp2pgsql-gui.o: shp2pgsql-gui.c
	$(CC) $(PGSQL_FE_CPPFLAGS) $(CFLAGS) $(GTK_CFLAGS) -o $@ -c shp2pgsql-gui.c

$(SHP2PGSQL-GUI): stringbuffer.o shpopen.o dbfopen.o shp2pgsql-core-gui.o shp2pgsql-gui.o $(LIBLWGEOM) 
	$(CC) $(CFLAGS) $(GTK_LIBS) $(ICONV_LDFLAGS) $(PGSQL_FE_LDFLAGS) -lm $^ -o $@

$(SHP2PGSQL-CLI): stringbuffer.o shpopen.o dbfopen.o getopt.o shp2pgsql-core.o shp2pgsql-cli.o $(LIBLWGEOM) 
	$(CC) $(CFLAGS) $(ICONV_LDFLAGS) -lm $^ -o $@

install: all
	@mkdir -p $(DESTDIR)$(bindir)
	$(INSTALL) $(PGSQL2SHP) $(DESTDIR)$(bindir)
	$(INSTALL) $(SHP2PGSQL) $(DESTDIR)$(bindir)

uninstall:
	@rm -f $(DESTDIR)$(bindir)/$(PGSQL2SHP)
	@rm -f $(DESTDIR)$(bindir)/$(SHP2PGSQL)

clean:
	@rm -f *.o $(SHP2PGSQL) $(PGSQL2SHP) 

