#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Cristoph Lameter.

# uncomment this to turn on verbose mode
#export DH_VERBOSE=1

pkg             := cl-imho
pkg-apache      := libapache-mod-webapp

prefix          := debian/tmp
prefix-apache   := debian/$(pkg-apache)

docdir		:= $(prefix)/usr/share/doc/$(pkg)
apache-dir	:= $(prefix-apache)/usr/lib/apache/1.3

ver-major	:= $(shell cat VERSION)

##
## sources are stored in c-l-c repository subdir
##
clc-systems	:= usr/share/common-lisp/systems
clc-src		:= usr/share/common-lisp/source
# where imho lisp goes, sans prefix
clc-imho        := $(clc-src)/imho-$(ver-major)
# where imho lisp goes, avec prefix
lispdir		:= $(prefix)/$(clc-imho)

##
## lisp sources
##
srcs            := $(wildcard *.lisp) \
		   $(shell find experimental -type f -name '*.lisp')

# tools
INSTALL         := install
INSTALLFLAGS    := -g root -o root -m 0644
INSTALLDIRFLAGS := -d -g root -o root -m 0755

build: 
	dh_testdir
	$(MAKE) -C apache
	$(MAKE) -C doc
	touch build

clean:
	$(checkdir)
	rm -f build
	-$(MAKE) clean
#	 be clever and try to grab a CVS changelog
	[ -f ChangeLog ] || [ ! -f ../$(pkg)/ChangeLog ] || cp ../$(pkg)/ChangeLog .
	[ -f ChangeLog ] || [ ! -f ../$(subst cl-,,$(pkg))/ChangeLog ] || cp ../$(pkg)/ChangeLog .
	dh_clean


##
## the primary lisp package in arch-indep, deal with it here
##
binary-indep: build
	$(checkdir)
	dh_testroot
	dh_clean -k
	dh_installdirs -p $(pkg) $(clc-systems) $(clc-src)
	$(INSTALL) $(INSTALLDIRFLAGS) $(lispdir)

##
##       c-l-c/sources
##
#	 installation of pkg srcs, preserving dir structure	
	set -e; \
	for src in $(srcs); do \
	    [ -d $(lispdir)/`dirname $$src` ] || \
		$(INSTALL) $(INSTALLDIRFLAGS) $(lispdir)/`dirname $$src` ;\
	    $(INSTALL) $(INSTALLFLAGS) $$src $(lispdir)/$$src ;\
	done
#        c-l-c requires that the sources dir is a symlink, so use the major version
	dh_link -p $(pkg) $(clc-imho) $(clc-src)/imho

##
##	 c-l-c/systems
##
	$(INSTALL) $(INSTALLFLAGS) imho.system $(prefix)/$(clc-systems)/

	dh_installdocs -i
#	 dh_installdocs can't install wildcard, so do this by hand
	$(INSTALL) $(INSTALLFLAGS) doc/*.html $(docdir)
	dh_installexamples -i
	find $(docdir) -name .cvsignore | xargs rm -f

#	dh_installmenu -i
#	dh_installcron -i
#	dh_installmanpages -i
#	dh_undocumented -i

	[ ! -f ChangeLog ] || dh_installchangelogs -i ChangeLog
	[ -f ChangeLog ] || dh_installchangelogs -i

#	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_shlibdeps -i
	dh_gencontrol -i
#	dh_makeshlibs -i
	dh_md5sums -i
	dh_builddeb -i


##
## the apache connector is arch-dependant, build it here
##
binary-arch: build
	$(checkdir)
	dh_testroot
	dh_clean -k

	$(INSTALL) $(INSTALLDIRFLAGS) $(apache-dir)

	$(INSTALL) $(INSTALLFLAGS) apache/apache-1.3/mod_webapp.so $(apache-dir)
	$(INSTALL) $(INSTALLFLAGS) debian/400mod_webapp.info $(apache-dir)

	dh_installdocs -a
	dh_installexamples -a
#	dh_installmenu -a
#	dh_installcron -a
#	dh_installmanpages -a
#	dh_undocumented -a

	[ ! -f ChangeLog ] || dh_installchangelogs -a ChangeLog
	[ -f ChangeLog ] || dh_installchangelogs -a

	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_makeshlibs -a
	dh_md5sums -a
	dh_builddeb -a

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

define checkdir
	test -f debian/rules
	test -f VERSION
endef
