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

# The ./configure invocation from hell - tweak CFLAGS here.

CONFLAGS =	-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O1

EXTRA_CONFARGS =

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=auth_digest --enable-module=log_referer \
		--enable-module=log_agent --enable-module=auth_db \
		$(EXTRA_CONFARGS) \
		--activate-module=src/modules/extra/mod_macro.c

# CAVEAT COMPILOR
#
#    $(B) version also hardcoded in debian/scripts/vars and debian/control
#    Change these for new upstream version.

B=$(SOURCE_DIR)/apache_1.3.26
C=$(SOURCE_DIR)/apache-contrib-1.0.8

######################################################################

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
	rm -f $(C)/mod_random/mod_random.so # in separate package
	rm -f $(C)/mod_layout/mod_layout.so # in separate package

	@echo -e "\n--- modules: netgods debian-contrib ---\n"
	cd contrib && $(MAKE) -k APXS=$(CURDIR)/debian/local-apxs

	@echo -e "\n--- modules: build-tree mod_auth_cache ---\n"
	cd build-tree/mod_auth_cache* \
		&& $(CURDIR)/debian/local-apxs -c mod_auth_cache.c
	@echo -e "\n--- modules: build-tree mod_throttle ---\n"
	cd build-tree/mod_throttle* \
		&& $(CURDIR)/debian/local-apxs -c mod_throttle.c

	@echo -e "\n--- sanity: module manifest check ---\n"
	find build-tree/ contrib/ \
		-name "*.so" -exec install -m 644 {} $(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
#	false

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/bin/uudecode
	test -f /usr/include/db.h

	set -e ; for foo in debian/*inst* debian/*ostrm debian/*rerm ; \
		do echo -n "$$foo " ; sh -n $$foo ; done ; echo

	test ! -d $(C)/mod_cvs # mod_cvs in apache-contrib is non-free

	@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
	patch -d $(B) -p0 < build-tree/pkg.eapi/eapi.patch

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

	cd $(B) && CFLAGS="$(CONFLAGS)" ./configure $(CONFARGS)

	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;;" \
		-e "s;-lndbm -ldb;;" \
		-e "s;-DTARGET;-DEAPI -DTARGET;" \
		-e "s;CFG_SYSCONFDIR/\$$CFG_TARGET;CFG_SYSCONFDIR/httpd;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
	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*,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
	install $(B)/src/apache \
		debian/apacheconfig debian/tmp/usr/sbin
	cp -a debian/apacheconfig.8 debian/tmp/$(man)/man8

	mkdir -p debian/tmp/etc/logrotate.d
	cp -a debian/logrotate debian/tmp/etc/logrotate.d/apache

	chmod +x debian/ubersed
	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

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

	mkdir -p debian/apache-common/{$(lib),usr/share/apache/icons/small}
	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,png} debian/*.gif debian/*.jpg debian/debian \
		debian/apache-common/usr/share/apache/icons
	cp -a $(B)/icons/small/*.{gif,png} \
		debian/apache-common/usr/share/apache/icons/small
	mkdir -p debian/apache-common/$(doc)-common
	cp -a $(B)/icons/README \
		debian/apache-common/$(doc)-common/README.icons
	cp -a $(B)/icons/small/README.txt \
		debian/apache-common/$(doc)-common/README.small.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 -m 4755 $(B)/src/support/suexec \
		debian/apache-common/usr/lib/apache/suexec
	install -D -m644 debian/apache-common.lintian \
		debian/apache-common/usr/share/lintian/overrides/apache-common
	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

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

	for mod in $$(cd $(C) && sh -c "find * -type d -maxdepth 0") ; \
		do if [ -f $(C)/$$mod/README ] ; \
		then echo "++ $(C)/$$mod/README" ; cp -a $(C)/$$mod/README \
		debian/apache-common/$(doc)-common/README.$$mod \
		; fi ; done
	cp -v $(C)/*/*.html debian/apache-common/$(doc)-common/
	mkdir debian/apache-common/$(doc)-common/mod_auth_cache && \
		cp build-tree/mod_auth_cache*/{ChangeLog,INSTALL,README,TODO} \
		debian/apache-common/$(doc)-common/mod_auth_cache/
	mkdir debian/apache-common/$(doc)-common/mod_throttle && \
		cp -a build-tree/mod_throttle*/{Img,*html} \
		debian/apache-common/$(doc)-common/mod_throttle/
	cd debian/apache-common/$(doc)-common/mod_throttle/ \
		&& mv index.shtml index.html

	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 -m644 debian/apaci debian/apache-dev/$(doc)-dev/apaci
	install $(B)/src/apache debian/apache-dev/usr/sbin/apache.dbg
	install -D -m644 debian/apache-dev.lintian \
		debian/apache-dev/usr/share/lintian/overrides/apache-dev
	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
	rm -f debian/apache-doc/$(doc)/manual/LICENSE
	debian/ubersed < $(B)/htdocs/index.html.en \
		> debian/apache-doc/$(doc)/manual.html
	ln -s ../../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_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 --exclude=suexec
	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_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 --exclude=suexec
	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.make

patch:
	-cd $(B) && $(MAKE) clean
#	-find $(B) -name "*.orig" -exec rm {} \;
	$(MAKE) -f debian/sys-build.mk make-diff
