#!/usr/bin/make -f
# Simple set of debian/rules that uses debhelper.
# Based on an example by Joey Hess. 
# extended by Paul Corner, Alex Joni, Chris Radek, Jeff Epler (in cronological order)

# Copyright (C) 2006 Jeff Epler
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# When used to produce a debian package, this file is a script "used to
# control compilation and installation of the executable"

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

python_version = @PYTHON_VERSION@
kernel_version = @KERNEL_VERSION@

debian/control:
	echo '# THIS FILE IS AUTO-GENERATED FROM control.in' > debian/control
	sed -e 's/@@KVERSION@@/$(kernel_version)/g' \
	    debian/control.in >> debian/control

build: build-stamp
build-stamp:
	dh_testdir

	# Add here commands to compile the package.
ifeq "$(kernel_version)" "sim"
	cd src && ./configure --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --enable-simulator --enable-build-documentation=pdf
else
	cd src && ./configure --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --with-realtime=/usr/realtime-$(kernel_version) --enable-build-documentation=pdf
endif
	(cd src; $(MAKE) && $(MAKE) docs)
	touch build-stamp

clean: debian/control
	dh_testdir
	dh_testroot
	rm -f build-stamp

ifeq "$(kernel_version)" "sim"
	cd src && ./configure --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --enable-simulator
else
	cd src && ./configure --prefix=/usr --sysconfdir=/etc --mandir=/usr/share/man --with-realtime=/usr/realtime-$(kernel_version)
endif
	cd src && $(MAKE) clean -s
	rm -f Makefile.inc

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	#start the install
	mkdir -p debian/tmp
	-(cd debian/extras && find . | grep -v CVS |cpio -pdmuv ../tmp)
	-(cd debian/extras-@DISTRIB_NAME@ && find . | grep -v CVS | cpio -pdmuv ../tmp)
	(cd src; export DESTDIR=`pwd`/../debian/tmp; $(MAKE) $@)
	python@PYTHON_VERSION@ /usr/lib/python@PYTHON_VERSION@/compileall.py debian/tmp/usr/lib/python@PYTHON_VERSION@
	find debian/tmp/usr/lib/python@PYTHON_VERSION@ -name \*.py -exec rm {} \;
	mkdir -p debian/tmp/usr/lib debian/tmp/usr/include/emc2
	cp lib/*.a debian/tmp/usr/lib
	mkdir -p debian/tmp/usr/share/doc/emc2-dev
	mv debian/tmp/usr/share/doc/emc2/EMC2_Developer_Manual.pdf \
		debian/tmp/usr/share/doc/emc2-dev/
	cp docs/html/gcode*.html debian/tmp/usr/share/doc/emc2/
	rm debian/tmp/usr/share/doc/emc2/HAL_User_Manual.pdf 
	rm debian/tmp/usr/share/doc/emc2/EMC2_Manual_Pages.pdf 
	rm -rf debian/tmp/usr/share/doc/emc2/html
	-rm debian/tmp/emc2*.files
	-rm debian/tmp/etc/emc2/sample-configs/*/*position*.txt
	dh_movefiles
	if [ `find debian/tmp -type f | wc -c` != 0 ]; then \
		echo "Files exist in debian/tmp, but should not"; \
		exit 1; \
	fi

# 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_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress -X.pdf -X.txt
ifneq "$(kernel_version)" "sim"
	dh_fixperms -X/emc_module_helper -X/bfload
endif
	dh_installdeb
	dh_makeshlibs
	cat debian/@EMC2_PACKAGE_NAME@/DEBIAN/shlibs debian/shlibs.pre > debian/shlibs.local
	dh_shlibdeps -l debian/@EMC2_PACKAGE_NAME@/usr/lib
	dh_python
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

debian/rules: debian/configure debian/*.in
	debian/configure @TARGET@