#!/usr/bin/make -f

export DH_COMPAT=4

sdl-pkg		:= sdl
sdl-ffi-pkg	:= sdl-ffi
gl-pkg		:= opengl
ex-pkg		:= sdl-demos

sdl-debpkg  	:= cl-sdl
gl-debpkg	:= cl-sdl-opengl
ex-debpkg	:= cl-sdl-demos

sdl-files	:= sdl/sdl.lisp sdl/sdl-ext.lisp sdl/util.lisp sdl/cl-sdl.lisp
sdl-ffi-files	:= ffi/uffi.lisp
gl-files	:= opengl/gl.lisp opengl/gl-ext.lisp opengl/glu.lisp
ex-files	:= examples/data.lisp examples/2d-test.lisp examples/sdl-test.lisp examples/example1.lisp
ex-nehe-files	:= examples/nehe/*.lisp
ex-data		:= examples/data/{cl-sdl.bmp,cl-sdl.xcf,star.bmp,tut10.world}

#examples	:= simple-client.lisp
#docs		:= api.html

clc-source	:= usr/share/common-lisp/source
clc-systems	:= usr/share/common-lisp/systems

sdl-clc-pkg	:= $(clc-source)/$(sdl-pkg)
sdl-doc-dir	:= usr/share/doc/$(sdl-debpkg)

sdl-ffi-clc-pkg	:= $(clc-source)/$(sdl-ffi-pkg)

gl-clc-pkg	:= $(clc-source)/$(gl-pkg)
gl-doc-dir	:= usr/share/doc/$(gl-debpkg)

ex-clc-pkg	:= $(clc-source)/$(ex-pkg)
ex-doc-dir	:= usr/share/doc/$(ex-debpkg)

minor-libs	:= ttf img mix
ml-pkg		:= sdl-$$l
ml-debpkg	:= cl-sdl-$$l
ml-files	:= sdl-$$l/*.lisp
ml-clc-pkg	:= $(clc-source)/$(ml-pkg)
ml-doc-dir	:= usr/share/doc/$(ml-debpkg)


cl-sdl-lib-dir	:= usr/lib/cl-sdl/

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

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	make

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	#rm -f debian/$(debpkg).postinst.* debian/$(debpkg).prerm.*

	make clean

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	# Add here commands to install the package into debian/$(pkg).

	# SDL
	dh_installdirs -p$(sdl-debpkg) $(clc-systems) $(sdl-clc-pkg)/sdl $(sdl-doc-dir) $(sdl-ffi-clc-pkg)/ffi $(cl-sdl-lib-dir)
	dh_install -p$(sdl-debpkg) $(sdl-pkg).asd $(sdl-clc-pkg)
	dh_install -p$(sdl-debpkg) $(sdl-files) $(sdl-clc-pkg)/sdl
	#dh_install -p$(sdl-debpkg) $(docs) $(doc-dir)
	dh_link -p$(sdl-debpkg) $(sdl-clc-pkg)/$(sdl-pkg).asd $(clc-systems)/$(sdl-pkg).asd
	dh_install -p$(sdl-debpkg) $(sdl-ffi-pkg).asd $(sdl-ffi-clc-pkg)
	dh_install -p$(sdl-debpkg) $(sdl-ffi-files) $(sdl-ffi-clc-pkg)/ffi
	dh_link -p$(sdl-debpkg) $(sdl-ffi-clc-pkg)/$(sdl-ffi-pkg).asd $(clc-systems)/$(sdl-ffi-pkg).asd
	make install_sdl DESTDIR=$(CURDIR)/debian/$(sdl-debpkg)/usr
	dh_installexamples -p$(sdl-debpkg) examples/example1.lisp
	
	# OpenGL
	dh_installdirs -p$(gl-debpkg) $(clc-systems) $(gl-clc-pkg)/opengl $(gl-doc-dir) $(cl-sdl-lib-dir)
	dh_install -p$(gl-debpkg) $(gl-pkg).asd $(gl-clc-pkg)
	dh_install -p$(gl-debpkg) $(gl-files) $(gl-clc-pkg)/opengl
	#dh_install -p$(gl-debpkg) $(docs) $(doc-dir)
	dh_link -p$(gl-debpkg) $(gl-clc-pkg)/$(gl-pkg).asd $(clc-systems)/$(gl-pkg).asd
	make install_opengl DESTDIR=$(CURDIR)/debian/$(gl-debpkg)/usr

	# Minor libs
	(for l in $(minor-libs) ; do \
		dh_installdirs -p$(ml-debpkg) $(clc-systems) $(ml-clc-pkg)/$(ml-pkg) $(cl-sdl-lib-dir) ;\
		dh_install -p$(ml-debpkg) $(ml-pkg).asd $(ml-clc-pkg) ;\
		dh_install -p$(ml-debpkg) $(ml-files) $(ml-clc-pkg)/$(ml-pkg) ;\
		dh_link -p$(ml-debpkg) $(ml-clc-pkg)/$(ml-pkg).asd $(clc-systems)/$(ml-pkg).asd ;\
		make install_$$l DESTDIR=$(CURDIR)/debian/$(ml-debpkg)/usr ;\
	done)
	#dh_install -p$(ml-debpkg) $(ml-docs) $(ml-doc-dir)

# Build architecture-independent files here.
binary-indep: 
	dh_testdir
	dh_testroot
	dh_clean -k
	# Examples
	dh_installdirs -p$(ex-debpkg) $(clc-systems) $(ex-clc-pkg)/examples/nehe $(ex-doc-dir) usr/share/cl-sdl-demos/data
	dh_install -p$(ex-debpkg) $(ex-pkg).asd $(ex-clc-pkg)
	dh_install -p$(ex-debpkg) $(ex-files) $(ex-clc-pkg)/examples
	dh_install -p$(ex-debpkg) $(ex-nehe-files) $(ex-clc-pkg)/examples/nehe
	dh_install -p$(ex-debpkg) $(ex-data) usr/share/cl-sdl-demos/data
	dh_link -p$(ex-debpkg) $(ex-clc-pkg)/$(ex-pkg).asd $(clc-systems)/$(ex-pkg).asd
	dh_installdocs -p$(ex-debpkg)
	dh_installchangelogs -p$(ex-debpkg)
	dh_strip -p$(ex-debpkg)
	dh_compress -p$(ex-debpkg)
	dh_fixperms -p$(ex-debpkg)
	dh_installdeb -p$(ex-debpkg)
	dh_shlibdeps -p$(ex-debpkg)
	dh_gencontrol -p$(ex-debpkg)
	dh_md5sums -p$(ex-debpkg)
	dh_builddeb -p$(ex-debpkg)

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf	
	dh_installdocs -a
	dh_installexamples -a $(examples)
#	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installman
#	dh_installinfo
#	dh_undocumented
	dh_installchangelogs -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_makeshlibs
	dh_installdeb -a
#	dh_perl
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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

