#!/usr/bin/make -f
# Sample debian.rules -*- makefile -*- for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified
#
# Modified to be a prototype for debmake by Christoph Lameter
# <clameter@debian.org>.

package=apache

arch=$(shell dpkg --print-architecture)
version=$(shell expr `pwd` : '.*-\([0-9.].*\)')
version_main=$(shell expr `pwd` : '.*-\([0-9]*.[0-9.]*\)')
version_main=1.3

SHELL=/bin/bash
INST=install -p
CC=cc -O2

# These should work now.
# ifeq ($(arch),sparc)
# DISABLEMODS=--disable-module=rewrite --disable-module=auth_dbm  \
# 	--disable-module=auth_db
# endif

# The module works now, right?
# ifeq ($(arch),alpha)
# DISABLEMODS=--disable-module=unique_id
# endif

build: stamp-configure stamp-apache stamp-common

stamp-apache: stamp-configure
	$(checkdir)
	@echo -e "\n--- establishing $(package) $(version_main) sanity ---\n"
	$(MAKE) -f debian/rules check-apache-scripts
	@echo -e "\n--- building $(package) ---\n"
	cd builddir && $(MAKE) LDFLAGS_SHLIB="-Bshareable"
	touch stamp-apache

stamp-common: stamp-apache
	mkdir -p debian/{dev/usr/man/man1,common/usr/man/man8}
	set -x; cd builddir/src.*/support; for m in *.8; do \
	    sed -e "s/httpd/apache/g" -e "s/HTTPD/APACHE/g" \
		-e "s?usr/local/apache/conf?etc/apache?g" \
		-e "s/1m/8/g" -e "s/apache.conf/httpd.conf/g" \
		-e "s?usr/local/apache/logs?var/log/apache?g" \
		-e "s?_log?.log?g" \
		-e "s?var/log/apache/httpd.pid?var/run/apache.pid?g" $$m \
		> ../../../debian/common/usr/man/man8/$$m; \
	done
	mv debian/common/usr/man/man8/apxs.8 debian/dev/usr/man/man1/apxs.1
	mv debian/common/usr/man/man8/httpd.8 debian
	mv debian/common/usr/man/man8/apachectl.8 debian
	@echo -e "\n--- building $(package) modules ---\n"
	INCL=$$(echo $$(pwd)/builddir/src.*/include); sed \
	   	-e "s;^#!/.*;#!/usr/bin/perl;" \
		-e "s;\@prefix\@;/usr;" \
		-e "s;\@sbindir\@;/usr/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;-DTARGET;-DEAPI -DTARGET;" \
		-e "s;\$$CFG_TARGET.conf;httpd.conf;" \
		builddir/src.*/support/apxs > debian/local-apxs; \
		ln -svf ../../src/os/unix/os.h $$INCL/os.h; \
		ln -svf ../../src/os/unix/os-inline.c $$INCL/os-inline.c
	chmod 755 debian/local-apxs
	cd debian-contrib && $(MAKE) APXS=$$(pwd)/../debian/local-apxs
	cd apache-contrib && $(MAKE) APXS=$$(pwd)/../debian/local-apxs
	INCL=$$(echo $$(pwd)/builddir/src.*/include); sed \
		-e "s;$$INCL;/usr/include/apache-1.3;" \
		debian/local-apxs > debian/apxs
	chmod 755 debian/apxs
	touch stamp-common

include debian/rules.common

stamp-configure: src/Configuration
	cp apache-contrib/mod_macro/mod_macro.c src/modules/extra
	./configure --enable-shared=max --enable-rule=SHARED_CHAIN \
		--enable-suexec --enable-module=status \
		--suexec-caller=www-data --enable-module=most \
		--enable-module=log_referer --enable-module=log_agent \
		--enable-module=auth_db --enable-module=auth_dbm \
		--prefix=/usr --libexecdir=/usr/lib/apache/1.3 \
		--sysconfdir=/etc/apache --localstatedir=/var/state/apache \
		--runtimedir=/var/run --logfiledir=/var/log/apache \
		--proxycachedir=/var/cache/apache --target=apache \
		--includedir=/usr/include/apache-1.3 \
		--shadow=$$(pwd)/builddir \
		--activate-module=src/modules/extra/mod_macro.c \
		--datadir=/var/www $(DISABLEMODS) --without-confadjust
	$(INST) debian/apaci builddir/src.*/apaci

	@echo -e "\n--- patching in $(package) EAPI support ---\n"
	cp pkg.eapi/*.h builddir/src.*/include
	cp pkg.eapi/*.c builddir/src.*/main
	cd builddir/src.*/ && patch -p1 < ../../pkg.eapi/eapi.patch
	sed "s/alloc.o buff.o/alloc.o buff.o ap_hook.o ap_ctx.o/" \
		builddir/src.*/main/Makefile > makefile-tmp
	mv makefile-tmp	builddir/src.*/main/Makefile 

	touch builddir/src.*/Configuration.apaci stamp-configure

check-apache-links:
	-(cd ../common && ls) | xargs -t -i sh -c \
		"diff -u ../common/{} debian/{}"

check-apache-scripts:
	if grep "trap SIG" debian/{*.p*,*.d*,p*,*html}; then false; fi
	if grep "/var/web" debian/{*.p*,*.d*,p*,*html} src/*.c; then false; fi
	if grep "/home/www" debian/{*.p*,*.d*,p*,*html} src/*.c; then false; fi
	if grep "Authorative" debian/*info src/*.c; then false; fi
	bash -n debian/cron.daily
	bash -n debian/init
	bash -n debian/preinst
	bash -n debian/postinst
	bash -n debian/prerm
	bash -n debian/postrm

clean: clean-common
	cd debian-contrib && $(MAKE) distclean
	cd apache-contrib && $(MAKE) distclean
	rm -rf stamp-* config.status builddir makefile-tmp
	rm -f debian/*.tar.gz* debian/{httpd,apachectl}.8
	rm -f debian/{*.gif,*.jpg,*apxs} .install* src/modules/extra/*.c
	chmod +x debian/$(package)config debian/{*postinst,*postrm} debian/init
	find .. -name $(package)*dsc.asc -size 0 -maxdepth 1 -exec rm {} ";"

binary-doc:
	@echo -e "\n\n*** Package $(package)-doc\n"
	rm -rf debian/doc
	dh_installdeb -p$(package)-doc -Pdebian/doc
	mkdir -p debian/doc/usr/doc/$(package)
	cp -p -r htdocs/* debian/doc/usr/doc/$(package)
	rm -rf $$(find debian/doc -name CVS -o -name LICENSE)
	dh_installdocs -v -p$(package)-doc -Pdebian/doc
	dh_gencontrol -p$(package)-doc -Pdebian/doc
	dh_installchangelogs -p$(package)-doc -Pdebian/doc src/CHANGES
	dh_compress -v -p$(package) -Pdebian/doc
	dh_md5sums -p$(package)-doc -Pdebian/doc
	dh_fixperms -p$(package)-doc -Pdebian/doc
	dpkg --build debian/doc ..

binary-dev: stamp-apache stamp-common
	@echo -e "\n\n*** Package $(package)-dev\n"
	dh_installdeb -p$(package)-dev -Pdebian/dev
	mkdir -p debian/dev/usr/{bin,doc,include/$(package)-$(version_main)}
	dh_installdocs -p$(package)-dev -Pdebian/dev
	$(INST) debian/apxs debian/dev/usr/bin/apxs
	cp -p builddir/src.*/include/*.h \
		builddir/src.*/os/unix/{*.h,*inline.c} \
		debian/dev/usr/include/$(package)-$(version_main)
	chown -R root.root debian/dev && chmod -R go=rX debian/dev
	dh_gencontrol -p$(package)-dev -Pdebian/dev
	dh_installchangelogs -p$(package)-dev -Pdebian/dev src/CHANGES
	dh_compress -v -p$(package)-dev -Pdebian/dev
	dh_md5sums -p$(package)-dev -Pdebian/dev
	dh_fixperms -p$(package)-dev -Pdebian/dev
	dpkg --build debian/dev ..

binary-indep: checkroot binary-dev binary-doc

install-modules:
#	This unlicenced module says the Apache licence isn't appropriate.
	test ! -d apache-contrib/mod_allowdev
#	This module has no licence granted.
	test ! -d apache-contrib/mod_auth_inst
#	These modules have no licence or ownership statement.
	test ! -d apache-contrib/mod_cache
	test ! -d apache-contrib/mod_cvs
	test ! -d apache-contrib/mod_lock
#	This is free but duplicates what a Debian package does better.
	test ! -d apache-contrib/mod_fastcgi
#	This is free but needs EAPI, which isn't in this yet.
	test ! -d apache-contrib/mod_define

#	Note: the mod_put copyright is in debian/copyright.
	mkdir -p debian/common/usr/lib/$(package)/$(version_main)
	$(INST) -m 644 builddir/src.*/modules/*/*.so \
		$$(find apache-contrib -name "*.so") \
		$$(find debian-contrib -name "*.so") \
		debian/common/usr/lib/$(package)/$(version_main)
	rm debian/common/usr/lib/$(package)/$(version_main)/mod_macro.so
	$(INST) -m 644 debian/*.info debian-contrib/info/*.info \
		debian/common/usr/lib/$(package)/$(version_main)
	@echo -e "\n*** Module roll call\n"
	@cd debian/common/usr/lib/apache/1.3 && \
		cat ../../../../../module-manifest \
		| xargs -i ls -l --no-group {}
	@echo -e "\n(all modules in manifest managed to compile)\n\n"

binary-common: stamp-common
	@echo -e "\n\n*** Package $(package)-common\n"
	dh_installdeb -p$(package)-common -Pdebian/common
	mkdir -p debian/common/usr/{bin,sbin,lib/$(package)}
	mkdir -p debian/common/usr/{man/{man1,man8},share/$(package)/icons}
	mkdir -p debian/common/usr/doc/$(package)/{icons,examples}
	mkdir -p debian/{common/usr/lib/cgi-bin,dev/usr/man/man8}
	$(MAKE) -f debian/rules install-modules
	dh_installdocs -p$(package)-common -Pdebian/common \
		ABOUT_APACHE KEYS Announcement INSTALL README* WARNING*
	$(INST) -s builddir/src.*/support/suexec \
		debian/common/usr/lib/$(package)
	$(INST) -s builddir/src.*/support/{htpasswd,htdigest} \
		debian/common/usr/bin
	$(INST) -s builddir/src.*/support/{logresolve,rotatelogs} \
		debian/common/usr/sbin
	sed -e s?/usr/local/bin/perl?/usr/bin/perl?g \
		builddir/src.*/support/dbmmanage \
		> debian/common/usr/bin/dbmmanage
	chmod 755 debian/common/usr/bin/dbmmanage
	$(INST) -s builddir/src.*/support/ab debian/common/usr/bin/ab
	set -x; cd builddir/src.*/support; for m in *.1; do \
	    sed -e "s/httpd/apache/g" -e "s/HTTPD/APACHE/g" \
		-e "s/1m/8/g" $$m -e "s/apache.conf/httpd.conf/g" \
		> ../../../debian/common/usr/man/man1/$$m; \
	done
	set -x; cd debian; for m in *.8; do \
	    sed -e "s/httpd/apache/g" -e "s/HTTPD/APACHE/g" \
		-e "s/1m/8/g" $$m -e "s/apache.conf/httpd.conf/g" \
		> ../debian/common/usr/man/man8/$$m; \
	done
	rm debian/common/usr/man/man8/{httpd,apachectl}.8
	cp -p debian/intro.html debian/mod*txt \
		builddir/src.*/support/suexec.{c,h} \
		debian/common/usr/doc/$(package)
	cd debian && uudecode logos.uue && tar -zxvf logos.tar.gz
	cp -p icons/{README,*.gif} htdocs/*.gif debian/*.gif \
		debian/common/usr/share/$(package)/icons
	cp -p htdocs/*.gif debian/*.gif \
		debian/common/usr/doc/$(package)/icons
	cp -p icons/README debian/common/usr/doc/$(package)/README.icons
	$(INST) -m 644 $$(cat debian/examples) \
		debian/common/usr/doc/$(package)/examples
	sed -e s?/usr/local/bin/perl?/usr/bin/perl?g \
		cgi-bin/printenv \
		> debian/common/usr/doc/$(package)/examples/printenv
	sed -e s?/usr/local/bin/perl?/usr/bin/perl?g \
		builddir/src.*/support/phf_abuse_log.cgi \
		> debian/common/usr/doc/$(package)/examples/phf_abuse_log.cgi
	dh_strip -p$(package)-common -Pdebian/common
	chown -R root.root debian/common && chmod -R go=rX debian/common
	dh_shlibdeps -p$(package)-common -Pdebian/common \
		debian/tmp/usr/sbin/apache
	dh_gencontrol -p$(package)-common -Pdebian/common
	dh_installchangelogs -p$(package)-common -Pdebian/common src/CHANGES
	dh_compress -v -p$(package)-common -Pdebian/common
	dh_md5sums -p$(package)-common -Pdebian/common
	dh_fixperms -p$(package)-common -Pdebian/common
	dpkg --build debian/common ..

install-main:
	@echo -e "\n\n*** ApacheLike Package $(package)\n"
	rm -rf debian/tmp
	dh_installdeb -p$(package) -Pdebian/tmp
	mkdir -p debian/tmp/usr/{sbin,man/man8}
	mkdir -p debian/tmp/etc/{cron.daily,init.d}
	mkdir -p debian/tmp/etc/$(package)
	mkdir -p debian/tmp/usr/doc/$(package)/examples
	mkdir -p debian/tmp/var/log/$(package)
	mkdir -p debian/tmp/var/cache/$(package)
	sed -e s?/usr/local/apache/logs/httpd.pid?/var/run/$(package).pid?g \
		-e s?/usr/local/apache/src/httpd?/usr/sbin/$(package)?g \
		builddir/src.*/support/apachectl \
		> debian/tmp/usr/sbin/$(package)ctl
	chmod 755 debian/tmp/usr/sbin/$(package)ctl
	cp -p debian/httpd.8 debian/tmp/usr/man/man8/$(package).8 
	cp -p debian/apachectl.8 debian/tmp/usr/man/man8/$(package)ctl.8
	$(INST) debian/apacheconfig debian/tmp/usr/sbin/$(package)config
	dh_installdocs -p$(package)
	ln -s ../man7/undocumented.7.gz \
		debian/tmp/usr/man/man8/$(package)config.8.gz
	echo; dpkg -l gcc "libc6*" binutils ldso make dpkg-dev $(BUILDINFO) \
		| awk '$$1 == "ii" { printf("%s-%s\n", $$2, $$3) }' \
		| tee debian/tmp/usr/doc/$(package)/buildinfo.Debian; echo
	dh_installchangelogs -p$(package) -Pdebian/tmp src/CHANGES
	dh_installinit -p$(package) -Pdebian/tmp
	dh_installcron -v -p$(package) -Pdebian/tmp

binary-arch: checkroot stamp-apache binary-common
	$(MAKE) -f debian/rules install-main package=$(package)
	@echo -e "\n--- final processing ---\n"
	$(INST) builddir/src.*/apache debian/tmp/usr/sbin/$(package)
	dh_strip -p$(package)
	$(INST) -m 644 conf/high* conf/*dist-win \
		debian/tmp/usr/doc/$(package)/examples
	dh_compress -v -p$(package)
	$(INST) -m 644 debian/*.conf \
		debian/tmp/usr/doc/$(package)/examples
	$(INST) -m 644 apache-contrib/*/*.html \
		debian/tmp/usr/doc/$(package)
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	$(INST) -o www-data -g www-data -m 755 \
		-d debian/tmp/var/cache/apache
	LD_LIBRARY_PATH=$(shell pwd)/debian/tmp/usr/lib/apache/1.3 \
		dh_shlibdeps -p$(package) debian/tmp/usr/sbin/apache
	dh_gencontrol -p$(package)
	dh_md5sums -p$(package)
	dpkg --build debian/tmp ..
	@echo -e "\nw00 w00\n"

define checkdir
	test -f debian/rules
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

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

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep clean checkroot
