#!/usr/bin/make -f
#export DH_VERBOSE=1

  include /usr/share/dpatch/dpatch.make

  export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
  export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

  ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --build $(DEB_HOST_GNU_TYPE)
  else
    confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
   endif
     

  CFLAGS = -Wall -g

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

configure: patch configure-stamp
configure-stamp: 
	dh_testdir
	
	# Add here commands to configure the package.
	mkdir $(CURDIR)/debian/build && cd $(CURDIR)/debian/build && cmake ../../ -DCOUNT_ME=OFF -DCMAKE_SKIP_RPATH=ON -DLIB_INSTALL_DIR=/lib
	
	touch configure-stamp

build: build-stamp

build-stamp: configure
	dh_testdir

	# Add here commands to compile the package.
	cd $(CURDIR)/debian/build && $(MAKE)
	
	touch build-stamp

clean:	cleanunpatched unpatch
cleanunpatched:
	dh_testdir
	dh_testroot
	dh_clean
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -rf $(CURDIR)/debian/build

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

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

	#Install lintian override file
	#install -m 644 debian/initng.lintian-override $(CURDIR)/debian/initng/usr/share/lintian/overrides/initng
        
# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installman
	dh_install --sourcedir=debian/tmp --list-missing
	#Fix until we have a manpage
	#ln -sf /usr/share/man/man8/initng.8.gz $(CURDIR)/debian/initng/usr/share/man/man8/shutdown_script.8.gz
	#ln -sf /usr/share/man/man8/initng.8.gz $(CURDIR)/debian/initng/usr/share/man/man8/initng-segfault.8.gz
	#ln -sf /usr/share/man/man8/killall5.8.gz $(CURDIR)/debian/initng/usr/share/man/man8/killalli5.8.gz
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l :debian/tmp/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure \
	patch unpatch
