#!/usr/bin/make -f
# This is part of the acl-installer program: 
#    http://b9.com/acl-installer.html

#
# Copyright (c) 2002 Kevin M. Rosenberg <kmr@debian.org>
#
# acl-installer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License (version 2) as
# published by the Free Software Foundation.
#
# acl-installer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have a copy of the GNU General Public License on your
# Debian system in the file /usr/share/common-licenses/GPL-2

ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)

pkg=acl-pro-installer
pkg-trial=acl-installer
pkg-alisp=acl-alisp
pkg-alisp8=acl-alisp8
pkg-mlisp=acl-mlisp
pkg-mlisp8=acl-mlisp8
pkg-acldoc=acldoc-el

pkg-name	:= acl-pro
pkg-trial-name	:= acl

prefix		:= debian/$(pkg)
prefix-trial	:= debian/$(pkg-trial)
prefix-acldoc	:= debian/$(pkg-acldoc)
clc-bin-dir	:= usr/lib/common-lisp/bin
acl-dir		:= usr/lib/$(pkg-name)
trial-dir	:= usr/lib/$(pkg-trial-name)
emacs-dir	:= usr/share/emacs/site-lisp
site-start-dir	:= etc/emacs/site-start.d

all-clc-pkgs	:= $(pkg-alisp) $(pkg-alisp8) $(pkg-mlisp) $(pkg-mlisp8)

base-clc-lib-dir	:= usr/lib/common-lisp

configure: configure-stamp

configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	# Add here to build the package
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	dh_installdirs -p $(pkg-trial) $(trial-dir) $(clc-bin-dir)

	# Add here commands to install the package into debian/tmp.
	dh_installdirs -p $(pkg) $(acl-dir)
	dh_installdirs -p $(pkg-alisp) $(clc-bin-dir)
	dh_installdirs -p $(pkg-alisp8) $(clc-bin-dir)
	dh_installdirs -p $(pkg-mlisp) $(clc-bin-dir)
	dh_installdirs -p $(pkg-mlisp8) $(clc-bin-dir)
	dh_install -p $(pkg) siteinit.cl $(acl-dir)
	dh_install -p $(pkg-trial) siteinit.cl $(trial-dir)

	sed -e "s@%dir%@/$(acl-dir)@;" dl-and-install-patches.sh.template > $(prefix)/$(acl-dir)/dl-and-install-patches.sh
	sed -e "s@%dir%@/$(trial-dir)@;" dl-and-install-patches.sh.template > $(prefix-trial)/$(trial-dir)/dl-and-install-patches.sh
	chown root.root $(prefix)/$(acl-dir)/dl-and-install-patches.sh \
		$(prefix-trial)/$(trial-dir)/dl-and-install-patches.sh
	chmod +x $(prefix)/$(acl-dir)/dl-and-install-patches.sh \
		 $(prefix-trial)/$(trial-dir)/dl-and-install-patches.sh

	for p in $(all-clc-pkgs); do \
	       	echo "processing CLC script for $$p" ;\
		target=`echo $$p | sed -e "s/^acl-//;"` ;\
		dir=/$(acl-dir); \
		sed -e "s@%dir%@$$dir@;" < acl.sh.template | \
		sed -e "s@%target%@$$target@;" | \
		sed -e "s@%builder%@$$target@;" > $$target.sh ;\
		sed -e "s@%basedir%@/$(base-clc-lib-dir)@;" < install-clc.cl.template | \
			sed -e "s@%impl%@$$target@" | \
			sed -e "s@%dirname%@$(pkg-name)@" > install-clc-$${target}.cl; \
		chown root.root $$target.sh; \
		chmod +x $$target.sh; \
		dh_install -p $$p $$target.sh $(clc-bin-dir); \
		dh_install -p $$p install-clc-$${target}.cl $(acl-dir); \
	done

	sed -e "s@%dir%@/usr/lib/$(pkg-trial-name)@;" < acl.sh.template | \
	sed -e "s@%target%@$(pkg-trial-name)@;" | \
	sed -e "s@%builder%@$(pkg-trial-name)@;" > $(pkg-trial-name).sh
	chown root.root $(pkg-trial-name).sh
	chmod +x $(pkg-trial-name).sh
	dh_install -p $(pkg-trial) $(pkg-trial-name).sh $(clc-bin-dir)
	sed -e "s@%basedir%@/$(base-clc-lib-dir)@;" < install-clc.cl.template | \
		sed -e "s@%impl%@$(pkg-trial-name)@" | \
		sed -e "s@%dirname%@$(pkg-trial-name)@" > $(prefix-trial)/$(trial-dir)/install-clc-$(pkg-trial-name).cl


	for p in $(all-clc-pkgs); do \
                echo "Processing maintainer scripts for $$p" ;\
		l=`echo $$p | sed -e "s/^acl-//;"` ;\
		sed -e "s/%pkg%/$$p/;" < debian/prerm.template | \
			sed -e "s/%impl%/$$l/;" \
			 > debian/$$p.prerm ;\
		sed -e "s/%pkg%/$$p/;" < debian/postinst.template | \
			sed -e "s/%impl%/$$l/;" \
			 > debian/$$p.postinst ;\
		chown root.root debian/$$p.prerm debian/$$p.postinst; \
		chmod +x debian/$$p.prerm debian/$$p.postinst; \
	done

	# Emacs package
	dh_installdirs -p $(pkg-acldoc) $(emacs-dir) $(site-start-dir)
	dh_install -p $(pkg-acldoc) acldoc.el $(emacs-dir)
	dh_install -p $(pkg-acldoc) debian/50acldoc.el $(site-start-dir)

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
#	dh_testversion
	dh_testdir
	dh_testroot
	dh_installdebconf	
	dh_installdocs -A README
	dh_installexamples
#	dh_installmenu
	dh_installemacsen
#	dh_installpam
#	dh_installinit
#	dh_installcron
	dh_installman -p $(pkg) debian/alisp.1 debian/alisp8.1 debian/mlisp.1 debian/mlisp8.1
	dh_installman -p $(pkg-trial) debian/acl.1
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
ifeq ($(ARCH),amd64)
	dh_gencontrol -- -VEXTRADEPENDS=,ia32-libs
else
	dh_gencontrol
endif
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
