#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# 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
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

configure:
	libtoolize -c -f
	autoreconf

config.status: configure
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --htmldir=\$${prefix}/share/doc/libcgicc-doc/html --disable-demos


build: build-stamp
build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp example.tar.gz

	# Clean up after the build process.
	# Run config.status --recheck + write, as upstream tarball includes Makefiles built for cygwin which don't work for us
	[ ! -f Makefile ] || (./config.status --recheck && ./config.status && $(MAKE) distclean)
	
	find . -name Makefile.in -delete
	rm -rf configure cgicc/config.h.in support/ltmain.sh m4 aclocal.m4

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i --sourcedir=debian/tmp
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i 
	dh_installexamples -i -XMakefile
	dh_link -i
	dh_compress -i
	dh_fixperms -i 
	find $(CURDIR)/debian/libcgicc-doc -name '*.png' -o -name '*.svg' | xargs chmod 644
	find $(CURDIR)/debian/libcgicc-doc -name .deps | xargs rm -r
	dh_installdeb -i 
	dh_gencontrol -i 
	dh_md5sums -i 
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=debian/tmp
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a -plibcgicc5-dev debian/cgicc-config.1
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a 
	dh_md5sums -a
	dh_builddeb -a

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