--- Makefile.orig	2007-12-14 04:27:13.000000000 +0800
+++ Makefile	2009-09-06 05:40:38.000000000 +0800
@@ -1,7 +1,7 @@
 
-#LINKOPT	=	/usr/local/lib/libdbmalloc.a 
-#LINKOPT = 	/usr/local/lib/cpl.a
-CFLAGS	=	-g -Wall
+#LINKOPT	=	%%PREFIX%%/lib/libdbmalloc.a
+#LINKOPT = 	%%PREFIX%%/lib/cpl.a
+CFLAGS	+=	-g -Wall
 #CFLAGS  =       -g -DUSE_CPL
 INSTALL = /usr/bin/install
 LD = /usr/bin/ld
@@ -9,7 +9,33 @@
 
 default:	all
 
-all:	shpcreate shpadd shpdump shprewind dbfcreate dbfadd dbfdump shptest
+TOOLS=	shpcreate shpadd shpdump shprewind dbfcreate dbfadd dbfdump shptest \
+	shputils shptreedump
+
+CONTRIBTOOLS=	
+
+#all:	lib shpcreate shpadd shpdump shprewind dbfcreate dbfadd dbfdump \
+#	shptest shputils shptreedump
+
+all:	lib tools contrib_tools
+
+install:	lib_install tools_install contrib_install
+
+tools:
+.for _TOOL in $(TOOLS)
+	$(CC) $(CFLAGS) -I. $(_TOOL).c -o $(_TOOL) .libs/libshp.a
+.endfor
+
+contrib_tools:
+	(cd contrib; ${MAKE} %%PROJ4%% all; cd ..)
+
+tools_install:
+.for _TOOL in ${TOOLS}
+	%%INSTALL%% -c -m 755 ${_TOOL} %%PREFIX%%/bin/${_TOOL}
+.endfor
+
+contrib_install:
+	(cd contrib; ${MAKE} %%PROJ4%% install; cd ..)
 
 shpopen.o:	shpopen.c shapefil.h
 	$(CC) $(CFLAGS) -c shpopen.c
@@ -100,51 +126,19 @@
 
 
 # -----------------------------------------------------------------------------
-# The following is contributed by Jan-Oliver Wagner, and should allow for
-# creating shared libraries on most platforms with gcc, and libtool installed.
+# A truly insane amount of hackery to make things actually work with
+# libtool-2.2.x -- you'd not believe the garbage that was here before.
 
-SHPLIB_VERSION=1.2.9
-LIBSHP_VERSION=1.0.1 # still once to be changed manually (see for 1:1:0), sorry
+SHPLIB_VERSION=1.2.10
+LIBSHP_VERSION=1 # still once to be changed manually (see for 1:1:0), sorry
 
 lib:
-	/bin/sh ./libtool --mode=compile gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1  -I. -I. -I/usr/local/include    -g -O2 -c shpopen.c
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c  -fPIC -DPIC shpopen.c -o .libs/shpopen.lo
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c shpopen.c -o shpopen.o >/dev/null 2>&1
-	mv -f .libs/shpopen.lo shpopen.lo
-	/bin/sh ./libtool --mode=compile gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1  -I. -I. -I/usr/local/include    -g -O2 -c shptree.c
-	rm -f .libs/shptree.lo
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c  -fPIC -DPIC shptree.c -o .libs/shptree.lo
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c shptree.c -o shptree.o >/dev/null 2>&1
-	mv -f .libs/shptree.lo shptree.lo
-	/bin/sh ./libtool --mode=compile gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1  -I. -I. -I/usr/local/include    -g -O2 -c dbfopen.c
-	rm -f .libs/dbfopen.lo
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c  -fPIC -DPIC dbfopen.c -o .libs/dbfopen.lo
-	gcc -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I. -I/usr/local/include -g -O2 -c dbfopen.c -o dbfopen.o >/dev/null 2>&1
-	mv -f .libs/dbfopen.lo dbfopen.lo
-	/bin/sh ./libtool --mode=link gcc  -g -O2  -o libshp.la -rpath /usr/local/lib -version-info 1:1:0 shpopen.lo shptree.lo dbfopen.lo  
-	rm -fr .libs/libshp.la .libs/libshp.* .libs/libshp.*
-	rm -fr .libs/libshp.lax
-	mkdir .libs/libshp.lax
-	$(LD) -G -h libshp.so.1 -o .libs/libshp.so.$(LIBSHP_VERSION)  shpopen.lo shptree.lo dbfopen.lo  -lc
-
-	(cd .libs && rm -f libshp.so.1 && ln -s libshp.so.$(LIBSHP_VERSION) libshp.so.1)
-	(cd .libs && rm -f libshp.so && ln -s libshp.so.$(LIBSHP_VERSION) libshp.so)
-	ar cru .libs/libshp.a  shpopen.o shptree.o dbfopen.o 
-	ranlib .libs/libshp.a
-	rm -fr .libs/libshp.lax
-	(cd .libs && rm -f libshp.la && ln -s ../libshp.la libshp.la)
+	%%LIBTOOL%% --mode=compile --tag=CXX $(CC) -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I%%PREFIX%%/include $(CFLAGS) -c shpopen.c
+	%%LIBTOOL%% --mode=compile --tag=CXX $(CC) -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I%%PREFIX%%/include $(CFLAGS) -c shptree.c
+	%%LIBTOOL%% --mode=compile --tag=CXX $(CC) -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I%%PREFIX%%/include $(CFLAGS) -c dbfopen.c
+	%%LIBTOOL%% --mode=compile --tag=CXX $(CC) -DPACKAGE=\"libshp\" -DVERSION=\"$(SHPLIB_VERSION)\" -DSTDC_HEADERS=1 -I. -I%%PREFIX%%/include $(CFLAGS) -c safileio.c
+	%%LIBTOOL%% --mode=link --tag=CXX $(CC) $(CFLAGS) -o libshp.la -rpath %%PREFIX%%/lib -version-info 1:1:0 shpopen.lo shptree.lo dbfopen.lo safileio.lo
 
 lib_install:
-	cp .libs/libshp.la .libs/libshp.lai
-	/bin/sh ./mkinstalldirs /usr/local/lib
-	/bin/sh ./libtool  --mode=install $(INSTALL) -c libshp.la /usr/local/lib/libshp.la
-	$(INSTALL) -c .libs/libshp.so.$(LIBSHP_VERSION) /usr/local/lib/libshp.so.$(LIBSHP_VERSION)
-	(cd /usr/local/lib && rm -f libshp.so.1 && ln -s libshp.so.$(LIBSHP_VERSION) libshp.so.1)
-	(cd /usr/local/lib && rm -f libshp.so && ln -s libshp.so.$(LIBSHP_VERSION) libshp.so)
-	chmod +x /usr/local/lib/libshp.so.$(LIBSHP_VERSION)
-	$(INSTALL) -c .libs/libshp.la /usr/local/lib/libshp.la
-	$(INSTALL) -c .libs/libshp.a /usr/local/lib/libshp.a
-	ranlib /usr/local/lib/libshp.a
-	chmod 644 /usr/local/lib/libshp.a
-	/bin/sh ./mkinstalldirs /usr/local/include/libshp
-	$(INSTALL) -c -m 644 shapefil.h /usr/local/include/libshp/shapefil.h
+	%%LIBTOOL%% --mode=install %%INSTALL%% -c libshp.la %%PREFIX%%/lib/libshp.la
+	%%INSTALL%% -c -m 644 shapefil.h %%PREFIX%%/include/shapefil.h
