#!/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-uncommonsql
pkg-meta	:= cl-metadata
pkg-pg		:= cl-uncommonsql-postgresql
pkg-mysql	:= cl-uncommonsql-mysql
pkg-oracle	:= cl-uncommonsql-oracle
all-pkgs	:= $(pkg) $(pkg-meta) $(pkg-pg) $(pkg-mysql) $(pkg-oracle)

prefix		:= debian/tmp
prefix-meta	:= debian/$(pkg-meta)
prefix-pg	:= debian/$(pkg-pg)
prefix-mysql	:= debian/$(pkg-mysql)
prefix-oracle	:= debian/$(pkg-oracle)

ver-major	:= $(shell cat VERSION)

##
## sources are stored in c-l-c source dir; we are not bothering with
## the repository dir 
##
clc-systems	:= usr/share/common-lisp/systems
clc-src		:= usr/share/common-lisp/source
clc-usql        := $(clc-src)/uncommonsql-$(ver-major)

##
## destination for the various sources; what this needs to be depends
## on both c-l-c's expectatations and the system files 
##
lispdir		:= $(prefix)/$(clc-usql)
lispdir-meta	:= $(prefix-meta)/$(clc-usql)/sql
lispdir-pg	:= $(prefix-pg)/$(clc-usql)/dbms/postgresql
lispdir-mysql	:= $(prefix-mysql)/$(clc-usql)/dbms/mysql
lispdir-oracle	:= $(prefix-oracle)/$(clc-usql)/dbms/oracle


##
## lisp sources per pkg
##
srcs		:= $(wildcard *.lisp) \
		   $(filter-out sql/metadata.lisp,$(wildcard sql/*.lisp))
srcs-meta	:= sql/metadata.lisp
srcs-pg		:= $(wildcard dbms/postgresql/*.lisp)
srcs-mysql	:= $(wildcard dbms/mysql/*.lisp)
srcs-oracle	:= $(wildcard dbms/oracle/*.lisp)

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


build:
	@echo "nothing to do for build"

clean:
	$(checkdir)
	-$(MAKE) clean
	set -e; for p in $(all-pkgs); do \
		rm -f debian/$$p.postinst debian/$$p.prerm ;\
	done
#	 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

binary-indep:
	$(checkdir)
	dh_testroot
	dh_clean -k
	dh_installdirs --all -i $(clc-systems) $(clc-src)
	$(INSTALL) $(INSTALLDIRFLAGS) $(lispdir) $(lispdir-meta) \
		$(lispdir-pg) $(lispdir-mysql) $(lispdir-oracle)

##
##       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-usql) $(clc-src)/uncommonsql

# 	 other packages, which are flat in dir structure
	$(INSTALL) $(INSTALLFLAGS) $(srcs-meta) $(lispdir-meta)
	$(INSTALL) $(INSTALLFLAGS) $(srcs-pg) $(lispdir-pg)
	$(INSTALL) $(INSTALLFLAGS) $(srcs-mysql) $(lispdir-mysql)
	$(INSTALL) $(INSTALLFLAGS) $(srcs-oracle) $(lispdir-oracle)

#	 these packages need to maintain a dual c-l-c identity and
#	 integration identity as presumed by the system files
	dh_link -p $(pkg-meta) $(clc-usql) $(clc-src)/metadata
	dh_link -p $(pkg-pg) $(clc-usql) $(clc-src)/uncommonsql-postgresql
	dh_link -p $(pkg-mysql) $(clc-usql) $(clc-src)/uncommonsql-mysql
	dh_link -p $(pkg-oracle) $(clc-usql) $(clc-src)/uncommonsql-oracle

##
##	 c-l-c/systems
##
	$(INSTALL) $(INSTALLFLAGS) uncommonsql.system $(prefix)/$(clc-systems)/
	$(INSTALL) $(INSTALLFLAGS) metadata.system $(prefix-meta)/$(clc-systems)/
#        the database backend system files are adapted from dbms/*/system.lisp
	grep -v "mk:oos" dbms/postgresql/system.lisp > \
                $(prefix-pg)/$(clc-systems)/uncommonsql-postgresql.system
	grep -v "mk:oos" dbms/mysql/system.lisp > \
                $(prefix-mysql)/$(clc-systems)/uncommonsql-mysql.system
	grep -v "mk:oos" dbms/oracle/system.lisp > \
                $(prefix-oracle)/$(clc-systems)/uncommonsql-oracle.system


#	 it's boring to maintain all these identical postinst/prerm maintainer 
#	 scripts independantly, so we maintain a single template instead and 
#	 split that out at build time
	set -e ;\
	for p in $(all-pkgs); do \
                echo "processing maintainer scripts for $$p" ;\
		l=`echo $$p | sed -e "s/^cl-//;"` ;\
		sed -e "s/%%/$$l/;" debian/prerm.template > debian/$$p.prerm ;\
		sed -e "s/%%/$$l/;" debian/postinst.template > debian/$$p.postinst ;\
	done

	dh_installdocs -i CONTRIBUTORS
	dh_installexamples -i
#	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

binary-arch:
#	nothing to do

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
