#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/quilt/quilt.make

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

config.status: configure
	dh_testdir
	#cp -f /usr/share/misc/config.sub /usr/share/misc/config.guess .
	./autogen.sh
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"

build: patch build-stamp
build-stamp:  config.status
	dh_testdir
	$(MAKE) ECHO=echo
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess
	dh_clean pngtest-static

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp ECHO=echo install

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	gcc -Wall -g -O2 -c -o pngtest.o pngtest.c
	gcc -o pngtest -Wall -g -O2 pngtest.o -L.libs -lpng12
	gcc -o pngtest-static -Wall -g -O2  pngtest.o .libs/libpng.a -lz -lm
	LD_LIBRARY_PATH=".libs:" ./pngtest
	./pngtest-static
endif

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_link
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_installexamples -plibpng12-dev example.c pngtest.c pngtest.png
	dh_install --sourcedir=debian/tmp
	dh_installman
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs --add-udeb=libpng12-0-udeb
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
