#!/usr/bin/make -f

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

PKG_APACHE2        := libapache2-mod-dacs
PKG_DACS           := dacs
PKG_DACS_EXAMPLES  := dacs-examples
PKG_LIBDACS        := $(shell grep -E '^Package: libdacs[0-9]+' debian/control |\
                             awk '{print $$2}')
PKG_LIBDACS_DEV    := libdacs-dev

#CFLAGS    =
#LDFLAGS   =
#CPPFLAGS  =
#CONFFLAGS =
#CONFENV   =
#
#CFLAGS = -Wall -g
#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#        CFLAGS += -O0
#else
#        CFLAGS += -O2
#endif
#CFLAGS   += $(shell apr-config --cflags)
#CPPFLAGS += $(shell apr-config --cppflags)
#
#
##LDFLAGS
#LDFLAGS +=  -Wl,-z,defs
#
#CONFENV += LDFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS)"


##let's help configure to figure out our host/build system
#DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
#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




ext_lib := $(CURDIR)/ext_lib
ext_lib_mkdir = $(shell mkdir -p $(shell dirname $(ext_lib)/$(strip $(1))))
ext_lib_link =  $(shell : $(call ext_lib_mkdir, $(2)); \
                        [ -L $(ext_lib)/$(strip $(2)) ] || \
                            ln -s $(1) $(ext_lib)/$(strip $(2)) \
                 )


LIB_version_current := $(shell awk -F '=' '/^LIBCURRENT=/{print $$2}' src/configure.ac)
LIB_version_revision := $(shell awk -F '=' '/^LIBREVISION=/{print $$2}' src/configure.ac)
LIB_version_age := $(shell awk -F '=' '/^LIBAGE=/{print $$2}' src/configure.ac)
LIB_soname := $(shell expr $(LIB_version_current) - $(LIB_version_age))
LIB_version := $(LIB_soname).$(LIB_version_age).$(LIB_version_revision)


configure: configure-stamp
configure-stamp:
	dh_testdir

	if [ ! "libdacs$(LIB_soname)" = "$(PKG_LIBDACS)" ]; then \
	   echo "Soname bump in debian/control missing; new soname: $(LIB_soname)" ;\
	   exit 1 ;\
	fi

ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	rm -f src/conftools/config.sub
	cp -f /usr/share/misc/config.sub src/conftools/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	rm -f src/conftools/config.guess
	cp -f /usr/share/misc/config.guess src/conftools/config.guess
endif


	#Linking external libraries and programs
	# Apache
	$(call ext_lib_link, /usr/bin/apxs2, apache/bin/apxs)
	$(call ext_lib_link, /usr/share/apache2/build, apache/build)
	$(call ext_lib_link, /usr/include/apr-1.0, apache/include/apr-1)
	$(call ext_lib_mkdir, apache/conf/httpd.conf)
	touch $(ext_lib)/apache/conf/httpd.conf

#	  $(CONFENV) ./configure
	set -e ;\
	  cd src ;\
	  unset LDFLAGS CFLAGS ;\
	  ./configure \
	    --prefix=/usr \
	    --localstatedir=/var \
	    --disable-prefix-check \
	    --sysconfdir=/etc/dacs \
	    --includedir=/usr/include/dacs \
	    --disable-bdb \
	    --enable-access-tokens \
	    --enable-cert-auth \
	    --disable-gdbm \
	    --enable-unix-auth \
	    --enable-apache-auth \
	    --enable-native-auth \
	    --enable-ldap-auth \
	    --enable-cas-auth \
	    --enable-token-auth \
	    --enable-pam-auth \
	    --enable-user-info \
	    --enable-unix-roles \
	    --enable-local-roles \
	    --with-htdocs=/usr/share/dacs \
	    --with-federations-root=/etc/dacs/federations \
	    --with-dacs-log=/var/log/dacs/error_log \
	    --with-ssl=/usr \
	    --with-expat=/usr \
	    --with-cgi-bin=/usr/lib/cgi-bin/dacs \
	    --with-apxs=/usr/bin/apxs2 \
	    --with-apache=$(ext_lib)/apache \
	    $(CONFFLAGS)

	touch $@

build-indep:
	#nothing to do here.

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

	echo "root" > src/.ownername
	echo "www-data" > src/.groupname
	unset LDFLAGS CFLAGS && $(MAKE) -C $(CURDIR)/src
	unset LDFLAGS CFLAGS && $(MAKE) -C $(CURDIR)/apache tag

	ln -s /usr/share/xml/docbook/stylesheet/nwalsh $(CURDIR)/man/docbook-xsl
	$(MAKE) -B -C $(CURDIR)/man mansrc html index

	touch $@


install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	$(MAKE) -C $(CURDIR)/src install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C $(CURDIR)/apache install DESTDIR=$(CURDIR)/debian/tmp

	# misc stuff is installed via dh_installdoc and similar tools
	rm -rf $(CURDIR)/debian/tmp/usr/www/misc
	#remove broken links
	rm $(CURDIR)/debian/tmp/usr/www/man/css
	rm $(CURDIR)/debian/tmp/usr/www/man/misc
	#fix examples directory name
	mkdir -p $(CURDIR)/debian/tmp/usr/share/doc/dacs-examples
	mv $(CURDIR)/debian/tmp/usr/www $(CURDIR)/debian/tmp/usr/share/doc/dacs-examples/dacs
	#remove empty dirs
	rmdir $(CURDIR)/debian/tmp/usr/lib/cgi-bin/dacs-native
	rmdir $(CURDIR)/debian/tmp/usr/tmp
	#remove preformatted manpages
	rm -rf $(CURDIR)/debian/tmp/usr/share/man/cat*
	#library tweaks
	chrpath -d $(CURDIR)/debian/tmp/usr/lib/libdacs.so
	find $(CURDIR)/debian/tmp/usr/lib/cgi-bin/dacs -type f -not -name dacs_error | xargs chrpath -d
	chrpath -d $(CURDIR)/debian/tmp/usr/bin/*
	chrpath -d $(CURDIR)/debian/tmp/usr/sbin/*
	rm $(CURDIR)/debian/tmp/usr/lib/libdacs.la
	#rename http to dacshttp
	mv $(CURDIR)/debian/tmp/usr/bin/http $(CURDIR)/debian/tmp/usr/bin/dacshttp
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/http.1 $(CURDIR)/debian/tmp/usr/share/man/man1/dacshttp.1

	dh_install --fail-missing

binary-indep: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i HISTORY
	dh_installdocs -i
	dh_compress -i
	find $(CURDIR)/debian/dacs-examples -type f | while read ffile; do \
	    if file $$ffile | grep -q executable; then \
	      chmod 755 $$ffile ;\
	    else \
	      chmod 644 $$ffile ;\
	    fi ;\
	done ;\
	dh_fixperms -i
	dh_gencontrol -i
	dh_installdeb -i
	dh_md5sums -i
	dh_builddeb -i


binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -s HISTORY
	dh_installdocs -s
	dh_strip -s
	dh_compress -s
	for pkg in $(PKG_APACHE2) $(PKG_DACS); do \
	  find $(CURDIR)/debian/$$pkg -type f | while read ffile; do \
	    if file $$ffile | grep -q executable; then \
	      chmod 755 $$ffile ;\
	    else \
	      chmod 644 $$ffile ;\
	    fi ;\
	  done ;\
	done
	dh_fixperms -s
	dh_makeshlibs -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_installdeb -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-arch binary-indep

clean:
	dh_testdir
	dh_testroot

	[ ! -f $(CURDIR)/src/Makefile ] || $(MAKE) -C $(CURDIR)/src distclean
	[ ! -f $(CURDIR)/apache/Makefile ] || $(MAKE) -C $(CURDIR)/apache distclean

ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	rm -f src/conftools/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	rm -f src/conftools/config.guess
endif

	rm -f man/docbook-xsl
	rm -f man/*.html
	rm -f man/*.man
	rm -f man/*.[0-9]

	rm -rf $(ext_lib)

	rm -rf html/handlers/dacs_pam_handler \
	   src/.build_notes \
	   src/.defaults \
	   src/config.log \
	   src/config.nice \
	   src/configure.lineno \
	   src/defs.mk \
	   src/defs.vars \
	   src/expr.loT \
	   src/libtool \
	   src/tests/Makefile \
	   tools/perl/DACScheck.pm

	rm -f src/.ownername src/.groupname

	rm -f configure-stamp build-stamp
	dh_clean

