#!/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
	$(checkdir)
	@echo -e "\n--- establishing $(package) $(version_main) sanity ---\n"
	$(MAKE) -f debian/rules check-apache-scripts
	@echo -e "\n--- building $(package) ---\n"
	$(MAKE) LDFLAGS_SHLIB="-Bshareable -lc"
	@echo -e "\n--- building $(package) modules ---\n"
	cd src/modules/throttle && \
		gcc -DLINUX=2 -DSTATUS -DUSE_HSREGEX -fpic -DSHARED_MODULE \
		-I../../include  -I../../os/unix -c mod_throttle.c && \
		ld -Bshareable -lc -o mod_throttle.so mod_throttle.o
	touch build

include debian/rules.common

stamp-configure: src/Configuration
	./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 \
		--includedir=/usr/include/apache-1.3 \
		--datadir=/var/www $(DISABLEMODS) --without-confadjust
	$(INST) debian/apaci src/
	touch 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
	rm -f stamp-configure config.status
	cd src/support && $(MAKE) clean; rm -f suexec
	if [ -f src/Makefile ]; then cd src; $(MAKE) clean; rm -f Makefile; fi
	rm -f debian/*.tar.gz* src/buildmark.c.rej debian/{httpd,apachectl}.8
	rm -f debian/{*.gif,*.jpg} .install*
	chmod +x debian/$(package)config
	find .. -name $(package)*dsc.asc -size 0 -maxdepth 1 -exec rm {} ";"
	rm -f $$(find . -name Makefile) src/include/ap_config_auto.h
	rm -f src/{apaci,modules.c,Makefile.config,Configuration.apaci}

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)
	chown -R root.root debian/doc && chmod -R go=rX debian/doc
	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:
	@echo -e "\n\n*** Package $(package)-dev\n"
	rm -rf debian/dev
	dh_installdeb -p$(package)-dev -Pdebian/dev
	mkdir -p debian/dev/usr/{doc,include/$(package)-$(version_main)}
	dh_installdocs -p$(package)-dev -Pdebian/dev
	cp -a src/include/*.h src/os/unix/*.h \
		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-doc binary-dev

install-modules:
	mkdir -p debian/common/usr/lib/$(package)/$(version_main)
	$(INST) -m 644 src/modules/*/*.so \
		debian/common/usr/lib/$(package)/$(version_main)
	-$(INST) -m 644 debian/*.info \
		debian/common/usr/lib/$(package)/$(version_main)

delete-modules:
ifeq ($(arch),sparc)
	rm -vf debian/common/usr/lib/$(package)/$(version_main)/*rewrite*
	rm -vf debian/common/usr/lib/$(package)/$(version_main)/*_db*
endif
ifeq ($(arch),alpha)
	rm -vf debian/common/usr/lib/$(package)/$(version_main)/*unique_id*
endif

binary-common:
	@echo -e "\n\n*** Package $(package)-common\n"
	rm -rf debian/common
	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
	$(MAKE) -f debian/rules install-modules
	dh_installdocs -p$(package)-common -Pdebian/common \
		ABOUT_APACHE KEYS Announcement INSTALL README*
	$(INST) -s src/support/suexec debian/common/usr/lib/$(package)
	$(INST) -s src/support/{htpasswd,htdigest} \
		debian/common/usr/bin
	$(INST) -s src/support/{logresolve,rotatelogs} \
		debian/common/usr/sbin
	sed -e s?/usr/local/bin/perl?/usr/bin/perl?g \
		src/support/dbmmanage > debian/common/usr/bin/dbmmanage
	chmod 755 debian/common/usr/bin/dbmmanage
	$(INST) -s src/support/ab debian/common/usr/bin/ab
	set -x; cd 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
	set -x; cd 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
	mv debian/common/usr/man/man8/httpd.8 debian
	mv debian/common/usr/man/man1/apachectl.1 debian
	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\@;/usr/include/apache-1.3;' \
		-e 's;\@sysconfdir\@;/etc/apache;' src/support/apxs \
		-e 's;/httpd;/apache;g' \
		-e 's;httpd.conf;apache.conf;g' \
		> debian/common/usr/sbin/apxs
	chmod 755 debian/common/usr/sbin/apxs
	cp -p debian/intro.html debian/mod*txt 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 \
		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
	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 \
		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.1 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 build binary-common
#	$(MAKE) -s -f debian/rules check-apache-links
	$(MAKE) -f debian/rules install-main package=$(package)
	@echo -e "\n--- final processing ---\n"
	$(INST) src/httpd 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
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	$(INST) -o root -g www-data -m 2775 \
		-d debian/tmp/var/cache/apache
	LD_LIBRARY_PATH=$(shell pwd)/debian/tmp/usr/lib/apache/1.3 \
		dh_shlibdeps -p$(package)
	dh_gencontrol -p$(package)
	dh_md5sums -p$(package)
	dpkg --build debian/tmp ..


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
