#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

#DEB_BUILD_OPTIONS=debug,noopt,nostrip

# 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)

upstreamversion=$(shell dpkg-parsechangelog | grep Version | head -1 | sed -e 's/Version: //g')

EXTRA_CMAKE_FLAGS=-DSKIP_BUILD_RPATH=ON -DSKIP_INSTALL_RPATH=ON -DWITH_PAM=ON -DWITH_SFV=ON
CMAKE_PREFIX = -DCMAKE_INSTALL_PREFIX="/usr"

include /usr/share/dpatch/dpatch.make

ifndef DESTDIR
DESTDIR=..
endif


CFLAGS = -Wall -fno-strict-aliasing

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

#config.status: patch-stamp configure
#	dh_testdir
#	# Add here commands to configure the package.
#	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --datadir=\$${prefix}/lib --localstatedir=/var --sysconfdir=/etc/wzdftpd --with-pam --with-zeroconf --enable-avahi

configure: configure-stamp

configure-stamp: patch-stamp
	dh_testdir
	# The first pass is used to create the configuration file
	$(MAKE) build/Makefile EXTRA_CMAKE_FLAGS="$(EXTRA_CMAKE_FLAGS)" PREFIX=/usr
	# save the files
	cp build/users.sample build/wzdftpd/wzd.cfg.sample .
	# fix the paths in the file ..
	sed -i -e 's+/usr/etc+/etc+g' \
	       -e 's+/usr/var+/var+g' \
	  wzd.cfg.sample
	$(MAKE) EXTRA_CMAKE_FLAGS="$(EXTRA_CMAKE_FLAGS)" CFLAGS="$(CFLAGS)" build/Makefile
	touch configure-stamp

#Architecture 
build: build-arch

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp

	# Add here commands to compile the arch part of the package.
	$(MAKE) EXTRA_CMAKE_FLAGS="$(EXTRA_CMAKE_FLAGS)" CFLAGS="$(CFLAGS)"
	touch build-arch-stamp

clean: clean-patched unpatch

clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp  #CONFIGURE-STAMP#

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || [ ! -f build/Makefile ] || $(MAKE) distclean
	dh_clean

install: install-arch

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	# Add here commands to install the arch part of the package into 
	# debian/tmp.
	$(MAKE) EXTRA_CMAKE_FLAGS="$(EXTRA_CMAKE_FLAGS)" PREFIX="$(CURDIR)/debian/tmp/usr" install
	# we don't need those files
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/wzdftpd/COPYING
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/wzdftpd/INSTALL

	# TODO since we install in standard dir, the following is becoming
	# useless ??

	install -m 755 -d debian/wzdftpd/usr/share/doc/wzdftpd/examples ;
	#install -m 644 wzdftpd/users.sample debian/wzdftpd/usr/share/doc/wzdftpd/examples ;\
	#install -m 644 wzdftpd/wzd.cfg.sample debian/wzdftpd/usr/share/doc/wzdftpd/examples ;\

	mv debian/tmp/usr/etc debian/tmp/

	install -m 755 -d debian/wzdftpd/usr/share/wzdftpd/files ;\
	mv wzd.cfg.sample debian/wzdftpd/usr/share/wzdftpd/files ;\
	mv users.sample debian/wzdftpd/usr/share/wzdftpd/files ;\
	rm debian/tmp/etc/wzdftpd/wzd.cfg.sample ;\
	rm debian/tmp/etc/wzdftpd/users.sample

	install -p -o root -g root -m 644 $(CURDIR)/debian/wzdftpd.overrides $(CURDIR)/debian/wzdftpd/usr/share/lintian/overrides/wzdftpd

	dh_install -s --list-missing --sourcedir=debian/tmp

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
#	dh_installdocs
	dh_installdocs -i debian/copyright
	dh_installexamples
#	dh_installmenu
	dh_installdebconf	
#	dh_installlogrotate	
#	dh_installpam
#	dh_installmime
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress -Xwzd.cfg.sample
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs -pwzdftpd -V 'wzdftpd (= $(upstreamversion))'
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb --destdir=$(DESTDIR)

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary: binary-arch
.PHONY: build clean binary-arch binary install install-arch patch unpatch
