#!/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
version=$(shell expr `pwd` : '.*-\([0-9.].*\)')
version_main=$(shell expr `pwd` : '.*-\([0-9]*.[0-9.]*\)')

modperl=mod_perl-1.00
ARCH=$(shell dpkg --print-architecture)
CC=cc

include debian/rules.common
include debian/rules.apache


debian/inc2shtml:
	cd debian; $(CC) -O2 -s -o inc2shtml inc2shtml.c

debian/unescape:
	cd debian; $(CC) -O2 -s -o unescape unescape.c


perl: $(modperl)/blib/arch/auto/Apache/Apache.so

$(modperl)/blib/arch/auto/Apache/Apache.so:
	@echo -e "\n--- building mod-perl ---\n"
	cd $(modperl) && \
		perl Makefile.PL APACHE_SRC=../../src/ NO_HTTPD=1 \
		DYNAMIC=1 PERL_STACKED_HANDLERS=1
	cd $(modperl) && $(MAKE)
	rm -f $$(find $(modperl) -name .exists)


build-support:
	@echo -e "\n--- building support binaries ---\n"
	cd support; $(MAKE) CFLAGS="-s -DDEBIAN"
	cd support; $(MAKE) CFLAGS="-s -DDEBIAN" suexec
	@$(MAKE) -f debian/rules debian/inc2shtml
	@$(MAKE) -f debian/rules debian/unescape


build: stamp-configure $(modperl)/blib/arch/auto/Apache/Apache.so
	$(checkdir)
	@echo -e "\n--- establishing sanity ---\n"
	$(MAKE) -f debian/rules check-apache-scripts
	$(MAKE) -f debian/rules build-support
	@echo -e "\n--- building $(package) ---\n"
	cp -v $$(dpkg -L perl | egrep "*\.a$$") src
	mkdir -p src/modules/perl
	cp -rf $(modperl)/src/modules/perl/* src/modules/perl
	cd src; $(MAKE)
	@echo -e "\n--- building apache modules ---\n"
	cd src; $(MAKE) modules
	touch build


clean: clean-common
	cd support && $(MAKE) clean; rm -f suexec
	-cd $(modperl) && $(MAKE) distclean
	rm -f $(modperl)/t/net/perl/eg
	mkdir -p src/modules/perl
	if [ -f src/Makefile ]; then cd src; $(MAKE) clean; rm -f Makefile; fi
	rm -f debian/{inc2shtml,unescape,*.tar.gz*} src/{httpd,modules.c,*.a}
	rm -rf src/modules/perl stamp-configure
	chmod +x debian/{apacheconfig,*inst,*rm}


binary-indep: checkroot binary-apache-doc binary-apache-dev


install-bin:
	@echo -e "\n--- installing binaries ---\n"
	install debian/cron.daily debian/tmp/etc/cron.daily/$(package)
	install debian/init.d debian/tmp/etc/init.d/$(package)
	install -m 644 debian/*.info debian/tmp/usr/lib/$(package)/1.2
ifeq ($(ARCH),sparc)
	rm debian/tmp/usr/lib/$(package)/1.2/mod_perl.info
	rm debian/tmp/usr/lib/$(package)/1.2/mod_auth_dbm.info
endif
	install support/htpasswd debian/{unescape,inc2shtml} \
		debian/tmp/usr/bin
	install debian/apacheconfig debian/tmp/usr/sbin
	install support/{htdigest,dbmmanage,logresolve,rotatelogs} \
		debian/tmp/usr/sbin
	install support/httpd_monitor debian/tmp/usr/sbin/$(package)_monitor
	install support/suexec debian/tmp/usr/lib/$(package)


install-docs:
	@echo -e "\n--- installing man pages and docs ---\n"
	install -d debian/tmp/usr/{man/man{1,8},doc/$(package)/icons}
	cp debian/changelog debian/tmp/usr/doc/$(package)/changelog.Debian
	cp CHANGES README support/suexec.* debian/tmp/usr/doc/$(package)
	cp src/CHANGES debian/tmp/usr/doc/$(package)/changelog
	find debian/tmp/usr/doc/$(package) -type f -maxdepth 1 \
		-exec gzip -v9 {} ";"
	cp ABOUT_APACHE KEYS LICENSE debian/tmp/usr/doc/$(package)
	cp debian/intro.html debian/tmp/usr/doc/$(package)
	cp cgi-bin/* debian/tmp/usr/doc/$(package)/examples/cgi-bin
	cp icons/* htdocs/*.gif debian/tmp/usr/doc/$(package)/icons
	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/tmp/usr/man/man8/$$m; \
	done
	cp debian/htpasswd.1 debian/tmp/usr/man/man1
	install -m 644 support/httpd.8 debian/tmp/usr/man/man8/$(package).8
	ln -s ../man7/undocumented.7.gz debian/tmp/usr/man/man1/unescape.1.gz
	ln -s ../man7/undocumented.7.gz debian/tmp/usr/man/man1/inc2shtml.1.gz
	ln -s ../man7/undocumented.7.gz \
		debian/tmp/usr/man/man8/apacheconfig.8.gz
	ln -s ../man7/undocumented.7.gz debian/tmp/usr/man/man8/dbmmanage.8.gz
	find debian/tmp/usr/man -type f -exec gzip -v9 {} ";"
	install -d debian/tmp/usr/doc/$(package)/examples
	cp -v $$(cat debian/examples) debian/tmp/usr/doc/$(package)/examples
	cp conf/access.conf-dist \
		debian/tmp/usr/doc/$(package)/examples/access.conf
	cp conf/httpd.conf-dist \
		debian/tmp/usr/doc/$(package)/examples/httpd.conf
	cp conf/srm.conf-dist \
		debian/tmp/usr/doc/$(package)/examples/srm.conf
	cp conf/mime.types debian/tmp/usr/doc/$(package)/examples/mime.types


binary-arch: checkroot build
	$(MAKE) -s -f debian/rules check-apache-links
	@echo -e "\n\n*** Main Package $(package)\n"
	rm -rf debian/tmp && install -d debian/tmp/usr/lib/$(package)/1.2
	$(MAKE) -f debian/rules install-apache-dirs
	cp -a debian/{conffiles,post*,pre*} debian/tmp/DEBIAN
	-install -m 644 src/*.so debian/tmp/usr/lib/$(package)/1.2
	install src/httpd debian/tmp/usr/sbin/$(package)
	@$(MAKE) -f debian/rules install-bin
	@$(MAKE) -f debian/rules install-docs
	@$(MAKE) -f debian/rules install-perl
	@$(MAKE) -f debian/rules build-debmake
	@echo -e "\n--- final processing ---\n"
	-strip --strip-unneeded debian/tmp/usr/sbin/apache \
		debian/tmp/usr/lib/apache/1.2/*.so \
		debian/tmp/usr/lib/perl5/$(ARCH)-linux/5.004/auto/Apache/*.so
	chmod 644 debian/tmp/usr/lib/perl5/$(ARCH)-linux/5.004/auto/Apache/*.so
	install -d debian/tmp/{usr/lib/cgi-bin,var/{log,spool}}
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	install -o root -g www-data -m 2775 \
		-d debian/tmp/var/{log,spool}/apache
#	dpkg-shlibdeps debian/tmp/usr/{bin/*,sbin/*}
	dpkg-shlibdeps $$( file debian/tmp/usr/*bin/* \
		| sed -n "/ELF/{; s/:.*//; p; };" )
	dpkg-gencontrol -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
