#!/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

# This is the debhelper compatability version to use.
export DH_COMPAT=2

# where to build the package
MIRROR   = switch.dl.sourceforge.net
NTOPSRC  = ntop-3.0pre2.tgz
builddir = packages/debian.official/BUILD
srcdir   = $(shell dirname $(builddir))


configure: $(builddir)/configure-stamp-ntop

$(builddir)/configure-stamp-ntop:

	# check if the source distribution tarball is there. Download if not.
	@if [ ! -f $(srcdir)/$(NTOPSRC) ]; then \
           (cd $(srcdir) && wget http://$(MIRROR)/sourceforge/ntop/$(NTOPSRC)); \
           (cd $(srcdir) && tar xvfz $(NTOPSRC)); \
         fi

	# Link the 'build' directory with a well-known name.
	@if [ ! -f $(builddir) ]; then \
           (cd $(srcdir) && ln -s `echo $(NTOPSRC) | sed -e 's/.tgz//'` `basename $(builddir)`); \
         fi

	# Linking the 'debian' direcory.
	# debhelper requires the 'debian' directory under the sources to successful work.
	@if [ ! -f debian ]; then \
          ln -s packages/debian.official debian; \
         fi

	dh_testdir
	cd $(builddir) && ./configure --prefix=/usr --libdir=/usr/lib/ntop \
		--sysconfdir=/etc --localstatedir=/var/lib --bindir=/usr/sbin \
		--mandir=/usr/share/man --enable-tcpwrap \
		--with-zlib-lib=/usr/lib --with-zlib-include=/usr/include \
		--with-libpng-lib=/usr/lib --with-libpng-include=/usr/include
#		--with-gd-lib=/usr/lib --with-gd-include=/usr/include

	touch $(builddir)/configure-stamp-ntop

build: $(builddir)/configure-stamp-ntop $(builddir)/build-stamp-ntop

$(builddir)/build-stamp-ntop:
	dh_testdir

	cd $(builddir) && $(MAKE)

	touch $(builddir)/build-stamp-ntop

clean:
	dh_testdir
	dh_testroot
	rm -f $(builddir)/build-stamp* $(builddir)/configure-stamp*

	# Add here commands to clean up after the build process.
	-$(MAKE) -C $(builddir) distclean

	-rm $(builddir)/config.status $(builddir)/config.cache $(builddir)/config.log
	-find . -type d -name ".deps" | xargs rm -Rf
	# remove the 'debian' link at the top level
	-rm -f debian

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd $(builddir) && $(MAKE) install install-data-as DESTDIR=$(CURDIR)/debian/ntop

	cp $(srcdir)/protocol.list $(CURDIR)/debian/ntop/etc/ntop
	gunzip $(CURDIR)/debian/ntop/etc/ntop/*.gz

	# Added things for logcheck ignoring.
	cp $(srcdir)/logcheck $(CURDIR)/debian/ntop/etc/logcheck/ignore.d.server/ntop
	cp $(srcdir)/logcheck $(CURDIR)/debian/ntop/etc/logcheck/ignore.d.workstation/ntop
	cp $(srcdir)/logcheck.paranoid $(CURDIR)/debian/ntop/etc/logcheck/ignore.d.paranoid/ntop
	# Added logrotate file.
	cp $(srcdir)/logrotate $(CURDIR)/debian/ntop/etc/logrotate.d/ntop

	cp $(builddir)/ChangeLog $(CURDIR)/debian/ntop/usr/share/doc/ntop/changelog

# docs for dh_installdirs
DOCS=$(builddir)/docs/FAQ $(builddir)/docs/HACKING $(builddir)/docs/KNOWN_BUGS \
	$(builddir)/docs/README $(builddir)/docs/TODO

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf -n
	dh_installdocs $(DOCS)
	dh_installexamples
	dh_installmenu
	dh_installinit -n
	dh_installcron
	dh_installmanpages png.5 zlib.3 libpng.3 libpngpf.3 ; \
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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