#!/usr/bin/make -f
# -*- makefile -*-
# Made with the aid of dh_make, by Craig Small.
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Also some stuff taken from debmake scripts, by Christoph Lameter.
# And from the X build system by Adam Heath.

B=$(SOURCE_DIR)/apache_1.3.9
C=$(SOURCE_DIR)/apache-contrib-1.0.7
L=$(SOURCE_DIR)/auth_ldap-1.4.0


CONFARGS =      --target=apache --with-layout=Debian \
		--enable-suexec --suexec-caller=www-data \
		--suexec-docroot=/var/www --includedir=/$(inc) \
		--suexec-logfile=/var/log/apache/suexec.log \
		--without-confadjust --without-execstrip \
		--enable-shared=max --enable-rule=SHARED_CHAIN \
		--enable-module=most --enable-module=status \
		--enable-module=log_referer --enable-module=log_agent \
		--enable-module=auth_db $(EXTRA_CONFARGS) \
		--activate-module=src/modules/extra/mod_macro.c
CONFCMD = cd $(B) && ./configure $(CONFARGS)

include debian/scripts/vars
SHELL=/bin/bash
BUILDINFO=make dpkg-dev debian-policy debhelper
IN_TARGETS=$(patsubst %,debian/%,docs apache-dev.docs \
	apache-doc.docs apache-common.examples examples)

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

FHS=$(shell test -f /usr/share/doc/debhelper/copyright && echo FHS)
ifeq ($(FHS),FHS)
doc=usr/share/doc/apache
man=usr/share/man
else
doc=usr/doc/apache
man=usr/man
endif

lib=usr/lib/apache/1.3
inc=usr/include/apache-1.3


build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	@echo -e "\n\n*** Compiling daemon and modules\n"	
	cd $(B) && $(MAKE)
	@echo -e "\n--- modules: engelschall apache-contrib ---\n"
	cd $(C) && $(MAKE) APXS=$(CURDIR)/debian/local-apxs
	@echo -e "\n--- modules: netgods debian-contrib ---\n"
	cd contrib && $(MAKE) -k APXS=$(CURDIR)/debian/local-apxs
	@echo -e "\n--- modules: ldap suite ---\n"
	-cd $(L) && $(MAKE) APXS=$(CURDIR)/debian/local-apxs \
		INCLUDES=-I/usr/include/openldap OPENLDAP=-DWITH_OPENLDAP \
		FRONTPAGE=-DAUTH_LDAP_FRONTPAGE_HACK

#	-cd $(L) && $(MAKE) APXS=$(CURDIR)/debian/local-apxs \
#		INCLUDES=-I/usr/include/openldap2 \
#		LIBS="/usr/lib/liblber-openldap2.a /usr/lib/libopenldap2.a" \
#		OPENLDAP=-DWITH_OPENLDAP \
#		FRONTPAGE=-DAUTH_LDAP_FRONTPAGE_HACK

	@echo -e "\n--- sanity: module manifest check ---\n"
	find $(B)/ $(C)/ $(L)/ contrib/ -mindepth 2 \
		-name "*.so" -exec install -m 644 {} $(B) \;
	install -m 644 contrib/mod_autoindex/*.so $(B)
	@set -e; echo; cd $(B) && cat $(CURDIR)/debian/module-manifest \
		| xargs -i ls -l --no-group {}; echo
	touch build-stamp

source.make: $(STAMP_DIR)/source.make
$(STAMP_DIR)/source.make:
	$(MAKE) -f debian/sys-build.mk source.make

configure-stamp: $(STAMP_DIR)/source.make
	@echo -e "\n\n*** Commencing laborious build of Debian Apache \
		1.3 $(FHS)\n"

	@echo -e "\n--- sanity: source code check ---\n"
	test -f /usr/include/mysql.h -o /usr/include/mysql/mysql.h
	test -f /usr/include/postgresql/postgres.h
	set -e ; for foo in debian/*inst* debian/*ostrm debian/*rerm ; \
		do echo -n "$$foo " ; sh -n $$foo ; done ; echo

#	These modules either have no licence or ownership statement,
#	or are seriously non-free.
	test ! -d $(C)/mod_cache
	test ! -d $(C)/mod_lock
	test ! -d $(C)/mod_cvs

	@echo -e "\n--- patch: EAPI support ---\n"
	cp build-tree/pkg.eapi/*.h $(B)/src/include
	cp build-tree/pkg.eapi/*.c $(B)/src/ap

	@echo -e "\n--- configure: APACI and APXS ---\n"
	cp $(C)/mod_macro/mod_macro.c $(B)/src/modules/extra

	$(CONFCMD)

	chmod +x debian/ubersed
	(debian/ubersed $(B)/src/apaci; \
		cat debian/apaci.append) > debian/apaci
	install debian/apaci $(B)/src/apaci

	cd $(B)/src/support && $(MAKE) apxs
	set -e; INCL=$$(echo $$(pwd)/$(B)/src/include); \
	 SBIN=$$(echo $$(pwd)/$(B)/src); sed \
	   	-e "s;^#!/.*;#!/usr/bin/perl;" \
		-e "s;\@prefix\@;/usr;" \
		-e "s;\@sbindir\@;$$SBIN;" \
		-e "s;\@libexecdir\@;/usr/lib/apache/1.3;" \
		-e "s;\@includedir\@;$$INCL;" \
		-e "s;\@sysconfdir\@;/etc/apache;" \
		-e "s;/httpd;/apache;g" \
		-e "s;-lm;-lc -lm;" \
		-e "s;-ldbm -ldb;-ldb;" \
		-e "s;-DTARGET;-DEAPI -DTARGET;" \
		-e "s;\$$CFG_TARGET.conf;httpd.conf;g" \
		$(B)/src/support/apxs > debian/local-apxs; \
	set -e; INCL=$$(echo $$(pwd)/$(B)/src/include); \
	 SBIN=$$(echo $$(pwd)/$(B)/src); sed \
		-e "s;$$INCL;/usr/include/apache-1.3;" \
		-e "s;$$SBIN;/usr/sbin;" \
		debian/local-apxs > debian/apxs
	chmod +x debian/{apxs,local-apxs}
	cp -a $(B)/src/os/unix/{*.h,*inline.c} \
		 $(B)/src/include
#	cd $(L) && patch -p1 < $(CURDIR)/debian/openldap.patch
	cp -v debian/suexec-1.3.9-debian.c \
		build-tree/apache_1.3.9/src/support/suexec.c
	touch configure-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf build-stamp install-stamp configure-stamp
	rm -f debian/apaci debian/Buildinfo.Debian
	rm -f debian/{*.gif,*.jpg,*.gz,local-apxs,apxs} o debian/o
	rm -rf debian/debian $(IN_TARGETS) contrib/*/*o
	chmod +x debian/{*post*,*preinst*,*prerm*,cron.daily,modchk}
	chmod +x debian/{ubersed,apacheconfig,sys-build.mk,scripts/*.*}
	$(MAKE) -f debian/sys-build.mk source.clean
	rm -rf debian.diff debian/stampdir
	dh_clean

install: install-stamp
install-stamp: build-stamp $(IN_TARGETS)
	@echo -e "\n\n*** So Far So Good\n"
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	cd debian && uudecode logos.uue && tar -zxf logos.tar.gz
	mkdir -p debian/tmp/$(doc)

	@echo -e "\n--- install: apache ---\n"
	mkdir -p debian/tmp/$(man)/man8
	chmod +x debian/ubersed
	install $(B)/src/apache \
		debian/apacheconfig debian/tmp/usr/sbin
	debian/ubersed < $(B)/src/support/apachectl \
		>>  debian/tmp/usr/sbin/apachectl
	chmod +x debian/tmp/usr/sbin/apachectl
	debian/ubersed < $(B)/src/support/httpd.8 \
		> debian/tmp/$(man)/man8/apache.8
	debian/ubersed < $(B)/src/support/apachectl.8 \
		> debian/tmp/$(man)/man8/apachectl.8
	cp -a debian/apacheconfig.8 debian/tmp/$(man)/man8

	@echo -e "\n--- install: apache-common ---\n"

	mkdir -p debian/apache-common/{$(lib),usr/share/apache/icons}
	mkdir -p debian/apache-common/usr/{bin,sbin}
	cp -a info/* $$(find $(B) contrib/ -maxdepth 1 -name "*.so") \
		debian/apache-common/$(lib)
	cp -a $(B)/icons/*.gif debian/*.gif debian/*.jpg debian/debian \
		debian/apache-common/usr/share/apache/icons
	mkdir -p debian/apache-common/$(doc)-common
	cp -a $(B)/icons/README \
		debian/apache-common/$(doc)-common/README.icons
	cp -a $(B)/src/support/suexec.{c,h} \
		$(B)/src/include/ap_config.h \
		debian/apache-common/$(doc)-common/
	mkdir -p debian/apache-common/$(man)/man{1,8}

	install $(B)/src/support/suexec \
		debian/apache-common/usr/lib/apache/suexec
	install $(B)/src/support/{logresolve,rotatelogs,ab} \
		debian/apache-common/usr/sbin
	debian/ubersed < $(B)/src/support/suexec.8 \
		> debian/apache-common/$(man)/man8/suexec.8
	debian/ubersed < $(B)/src/support/logresolve.8 \
		> debian/apache-common/$(man)/man8/logresolve.8
	debian/ubersed < $(B)/src/support/rotatelogs.8 \
		> debian/apache-common/$(man)/man8/rotatelogs.8
	debian/ubersed < $(B)/src/support/ab.8 \
		> debian/apache-common/$(man)/man8/ab.8

	install $(B)/src/support/{htpasswd,htdigest,dbmmanage} \
		debian/apache-common/usr/bin
	debian/ubersed < $(B)/src/support/htpasswd.1 \
		> debian/apache-common/$(man)/man1/htpasswd.1
	debian/ubersed < $(B)/src/support/htdigest.1 \
		> debian/apache-common/$(man)/man1/htdigest.1
	debian/ubersed < $(B)/src/support/dbmmanage.1 \
		> debian/apache-common/$(man)/man1/dbmmanage.1

	for foo in auth_cookie auth_external lock put roaming ; \
		do echo "++ $(C)/mod_$$foo/README" ; \
		cp -a $(C)/mod_$$foo/README \
		debian/apache-common/$(doc)-common/README.mod_$$foo \
		; done
	for foo in $(C)/*/*html $(L)/auth_ldap.html ; \
		do echo "++ $$foo" ; cp -a $$foo \
		debian/apache-common/$(doc)-common/ ; done
	rm -f $$(find debian/apache-common -name "*fastcgi*" -o -name "*~")
	@echo -e "\n--- sanity: module .info check ---\n"
	chmod +x debian/modchk && debian/modchk

	@echo -e "\n--- install: apache-dev ---\n"
	mkdir -p debian/apache-dev/$(man)/man{1,8} \
		debian/apache-dev/usr/{bin,sbin}
	debian/ubersed < $(B)/src/support/apxs.8 \
		> debian/apache-dev/$(man)/man1/apxs.1
	install debian/apxs debian/apache-dev/usr/bin/apxs
	mkdir -p debian/apache-dev/$(inc)
	cp -a $(B)/src/include/* debian/apache-dev/$(inc)
	mkdir -p debian/apache-dev/$(doc)-dev
	install debian/apaci debian/apache-dev/$(doc)-dev/apaci
	install $(B)/src/apache debian/apache-dev/usr/sbin/apache.dbg
	debian/ubersed < $(B)/src/support/httpd.8 \
		> debian/apache-dev/$(man)/man8/apache.dbg.8

	@echo -e "\n--- install: apache-doc ---\n"

	mkdir -p debian/apache-doc/$(doc)/manual
	cp -a $(B)/src/README debian/apache-doc/$(doc)/README.src
	cp -a $(B)/htdocs/manual/* debian/apache-doc/$(doc)/manual
	debian/ubersed < $(B)/htdocs/index.html \
		> debian/apache-doc/$(doc)/manual.html
	ln -s /usr/share/apache/icons debian/apache-doc/$(doc)/icons

	@echo -e "\n--- install: pointless debmakeism ---\n"
	echo; (date -u; echo; dpkg -l gcc "libc6*" binutils ldso $(BUILDINFO) \
		| awk '$$1 == "ii" { printf("%s-%s\n", $$2, $$3) }'; echo; \
		uname -a) | tee debian/buildinfo.Debian; echo
	dh_installdocs -A debian/buildinfo.Debian
	touch install-stamp

# Build architecture-independent files here.
binary-indep: build install
	@echo -e "\n\n*** Architecture Indepdendent (apache-doc)\n"
	dh_testversion 2.0.21
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installcron -i
	dh_installchangelogs -i $(B)/src/CHANGES
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_suidregister -i
	dh_installdeb -i
	dh_gencontrol -i -u-isp
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-pre-arch: build install
	@echo -e "\n\n*** Architecture Specific (apache, dev, common)\n"
	dh_testversion 2.0.21
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installinit -a
	dh_installcron -a
	dh_installchangelogs -a $(B)/src/CHANGES
	dh_strip -a --exclude=dbg
	dh_link -a
#	This avoids a lintian warning by breaking policy -- fun, eh?	
	dh_compress -a --exclude=log_server_status \
		--exclude=httpd.conf --exclude=access.conf --exclude=srm.conf
	dh_fixperms -a
	chmod 755 debian/apache-dev/$(doc)-dev/apaci
	dh_suidregister -a
	dh_installdeb -a
	dh_shlibdeps -a debian/tmp/usr/sbin/apache

binary-arch: binary-pre-arch
	dh_gencontrol -a -u-isp
	dh_md5sums -a
	dh_builddeb -a
	@echo -e "\nw00 w00\n"

source diff:                                                                  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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

$(IN_TARGETS): % : %.in debian/scripts/vars
	sed 's|$$(BUILD_TREE)|$(B)|g' $@.in > $@

reset:
	rm -rf build-stamp build-tree configure-stamp debian.diff
	$(MAKE) -f debian/sys-build.mk source.clean
	$(MAKE) -f debian/sys-build.mk source.build
