#!/usr/bin/make -f

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
  CFLAGS = -g -Wall
  DEBUG = 1
else
  CFLAGS = -O1 -pedantic
endif


build-stamp:
	dh_testdir
	./configure --prefix=/usr --mandir=/usr/share/man \
	  --with-ssl \
	  --with-ipv6 \
	  --with-perl \
	  --without-tcl \
	  --program-transform-name='s/epic/epic4/' \
	  --libexecdir=/usr/lib/epic4 --program-prefix=$(CURDIR)/debian/epic4
	$(MAKE) CFLAGS="$(CFLAGS)"
	touch build-stamp


build:	patch build-stamp

patch-stamp:
	@for patchfile in debian/patches/*.patch; do \
	  patchname=`basename "$$patchfile" .patch`; \
	  echo -n "Applying $$patchname patch... "; \
	  patch -s -p1 < $$patchfile \
	    && (echo "succeed!"; touch patch-stamp-$$patchname) \
	    || (echo "failed!"; failed="yes"); \
	done; \
	test -z $$failed && touch patch-stamp

patch: patch-stamp


unpatch:
	-@for patchfile in `ls -1 debian/patches/*.patch | sort -nr`; do \
	  patchname=`basename "$$patchfile" .patch`; \
	  test -f patch-stamp-$$patchname \
	    && (patch -s -p1 -R < $$patchfile; rm patch-stamp-$$patchname; echo "The $$patchname patch has been removed.") \
	    || echo "Failed removing the $$patchname patch."; \
	done

clean: unpatch
	dh_testdir
	rm -f patch-stamp build-stamp install-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean


install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k -pepic4
	dh_installdirs -pepic4
	$(MAKE) install


install:	build install-stamp


binary-indep:
# Nothing to see, move along...


binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs -a doc/* -XCVS -Xdoc/epic.1
	dh_installmenu -a
ifndef DEBUG
	dh_strip -a
endif
	install -m 644 $(CURDIR)/debian/local \
		       $(CURDIR)/debian/epic4/usr/share/epic4/script/local
	dh_installchangelogs -a
	dh_installexamples -a
	install -m 644 debian/epic4.irc $(CURDIR)/debian/epic4/etc/epic4
	install -m 644 $(CURDIR)/debian/local \
		       $(CURDIR)/debian/epic4/usr/share/epic4/script/local
	echo "irc.debian.org:6667" > $(CURDIR)/debian/epic4/etc/epic4/servers
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_installdeb -a
	dh_md5sums -a
	dh_builddeb -a


binary:		binary-indep binary-arch


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