#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

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

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH      = $(shell dpkg-architecture -qDEB_BUILD_ARCH | sed -e s/powerpcspe/ppc/ -e s/powerpc/ppc/)
HOST_CPU            = $(shell dpkg-architecture -qDEB_HOST_ARCH | sed -e s/powerpcspe/ppc/ -e s/powerpc/ppc/ -e s/amd64/x86_64/ -e s/armel/arm/)

ifeq ($(HOST_CPU),arm)
CFLAGS += -march=armv6
endif

# RTAIs own prefix handling
RTAI_PREFIX=/usr/lib/realtime

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# Name of package
package		= rtai
# KSRC is the location of the kernel source. This is the default value,
# when make-kpkg is used it will supply to real value
KSRC		= /usr/src/linux
# KDREV is the package-revision, as given to make-kpkg by the user.
# Just put a simply default value in here which we use when we test
# the packagebuilding without make-kpkg
ifeq ($(strip $(KDREV)),)
KDREV		= "test1.0"
endif

## Now to determine the kernel version, normally supplied by make-kpkg
ifeq ($(strip $(KVERS)),)
# Now we need to get the kernel-version somehow (we are not running
# under make-kpkg?)
ifeq ($(strip $(KSRC)),)
$(error Error. I do not know how to determine the kernel version)
else
kversion :=$(shell egrep '^VERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/[^0-9]*\([0-9]*\)/\1/')
kplevel  :=$(shell egrep '^PATCHLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                    sed -e 's/[^0-9]*\([0-9]*\)/\1/')
ksublevel:=$(shell egrep '^SUBLEVEL +=' $(KSRC)/Makefile 2>/dev/null | \
                  sed -e 's/[^0-9]*\([0-9]*\)/\1/')
EXTRA_VERSION:=$(shell egrep '^EXTRAVERSION +=' $(KSRC)/Makefile 2>/dev/null | \
                 sed -e 's/EXTRAVERSION[\t ]*=[\t ]*\(.*\)/\1/')
kextra:=$(strip $(EXTRA_VERSION))
HAVE_NEW_MODLIB:=$(shell egrep '\(INSTALL_MOD_PATH\)' \
                            $(KSRC)/Makefile 2>/dev/null )

# If you want to have more than one kernel configuration per kernel
# version, set FLAVOUR in the top level kernel Makefile *before*
# invoking make-kpkg -- it will be appended to UTS_RELEASE in
# version.h (separated by a hyphen). This affects everything -- the
# names and versions of the image, source, headers, and doc packages,
# and where the modules are searched for in /lib/modules.

ifdef FLAVOUR
# uhm - should check if we really have a Makefile capable of Flavours?
endif

FLAVOUR:=$(shell grep ^FLAVOUR $(KSRC)/Makefile 2>/dev/null | \
                  perl -ple 's/FLAVOUR[\s:=]+//g')

ifneq ($(strip $(FLAVOUR)),)
INT_FLAV := -$(FLAVOUR)
FLAV_ARG := FLAVOUR=$(FLAVOUR)
else
INT_FLAV :=
FLAV_ARG :=
endif

## This is the replacement for FLAVOUR
ifneq ($(strip $(APPEND_TO_VERSION)),)
iatv := $(strip $(APPEND_TO_VERSION))
EXTRAV_ARG := EXTRAVERSION=${EXTRA_VERSION}${iatv}
else
iatv :=
EXTRAV_ARG :=
endif

KVERS = $(kversion).$(kplevel).$(ksublevel)$(kextra)$(iatv)$(INT_FLAV)

endif
endif

non_epoch_version=$(shell echo $(KVERS) | perl -pe 's/^\d+://')
epoch=$(shell echo $(KVERS) | perl -ne 'm/^(\d+:)/ && print $$1')

# We also need the package version
pversion	= $(shell sed -ne '1s/.*\((.*)\).*/\1/' debian/changelog)
pversion	= $(shell sed -ne '1s/.*(\(.*\)).*/\1/p' debian/changelog)

# KDIST_DEST_DIR is the place where the final .deb package should be made. This is the
# default value, when make-kpkg is used it will supply to real value
KDIST_DEST_DIR		= ..

pmodules = $(package)-modules-$(non_epoch_version)
psource = $(package)-source

# Prepares the package for distribution.  Intended for the kernel
# maintainer.
kdist: kdist_clean kdist_config kdist_image

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly
# useful for calling configure)
kdist_config: kdist_config-stamp
kdist_configure: kdist_config-stamp

# try to support all of RTAIs configuring interfaces
CONFIG_TARGET_IF=cli
ifeq ($(CONFIG_TARGET),menuconfig)
CONFIG_TARGET_IF=gui
endif
ifeq ($(CONFIG_TARGET),gconfig)
CONFIG_TARGET_IF=gui
endif
ifeq ($(CONFIG_TARGET),xconfig)
CONFIG_TARGET_IF=gui
endif

kdist_config-stamp:
	sed 's:/usr/realtime:$(RTAI_PREFIX):' base/arch/$(HOST_CPU)/defconfig.in \
		| sed 's:/usr/src/linux:$(KSRC):' \
		> $(CURDIR)/base/arch/$(HOST_CPU)/defconfig
ifeq ($(CONFIG_TARGET_IF),cli)
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
	       --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
	       --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	       --with-linux-dir=$(KSRC) --includedir=\$${prefix}/include/rtai \
	       --with-module-dir=/lib/modules/$(KVERS)/rtai --disable-rtailab \
	       --disable-leds
endif
ifeq ($(CONFIG_TARGET_IF),gui)
	$(MAKE) -f makefile $(CONFIG_TARGET)
endif
	touch kdist_config-stamp

# the kdist_image target is called by make-kpkg modules_image. It is
# responsible for compiling the module and creating the package. It
# should also clean up after making the module. Please note we use a
# separate binary-modules target to make testing the package building
# easier
kdist_image:
	$(MAKE) $(MFLAGS) -f debian/rules binary-modules
	$(MAKE) $(MFLAGS) -f debian/rules clean

# the kdist_clean target is called by make-kpkg modules_clean. It is
# responsible for cleaning up any changes that have been made by the
# other kdist_commands (except for the .deb files created).
kdist_clean:
	$(MAKE) $(MFLAGS) -f debian/rules clean

### end  KERNEL SETUP

ifeq ($(shell echo $(DEB_HOST_GNU_TYPE) | cut -d- -f1),powerpc)
CONFIG_OPTION=CONFIG_PPC
else ifeq ($(shell echo $(DEB_HOST_GNU_TYPE) | cut -d- -f1),arm)
CONFIG_OPTION=CONFIG_ARM
else ifeq ($(shell echo $(DEB_HOST_GNU_TYPE) | cut -d- -f1),x86_64)
CONFIG_OPTION=CONFIG_X86_64
else
CONFIG_OPTION=CONFIG_X86
endif

config.status: configure-stamp
configure-stamp:
	dh_testdir
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.sub base/config/autoconf/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
	cp -f /usr/share/misc/config.guess base/config/autoconf/config.guess
endif
	autoconf -f
	sed -e "s,^CONFIG_X86=,$(CONFIG_OPTION)=," debian/linux/.config.x86 > debian/linux/.config
ifeq ($(CONFIG_OPTION),CONFIG_ARM)
	echo "CONFIG_ARCH_PXA=y" >> debian/linux/.config
endif
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
	       --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
	       --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	       --with-linux-dir=$(CURDIR)/debian/linux --disable-leds \
	       --includedir=\$${prefix}/include/rtai --disable-rtailab \
	       --enable-comedi-lxrt --enable--comedi-lock
	touch configure-stamp

build-arch:  config.status build-arch-stamp
build-arch-stamp:
	dh_testdir
	cd base/sched/liblxrt && $(MAKE) CFLAGS="$(CFLAGS)"
	cd base/scripts && $(MAKE)
	touch build-arch-stamp

# the binary-modules target prepares the $(pmodules) package.
# It is called by make-kpkg and *not* during a normal build
binary-modules: kdist_config-stamp
	export DH_OPTIONS='-p$(pmodules)'

	# Is this needed for setting up a Depends?
	#echo "kpkg:Package-Version=$(epoch)$(pversion)+$(non_epoch_version)" \
	#	 >> debian/$(package).substvars
	echo "kpkg:Kernel-Version=$(non_epoch_version)" > \
		debian/$(pmodules).substvars

	# The substvars mechanism seems slightly broken, hack to get around it
	# stolen from the qce-ga package. Yaaaa!
	sed -e 's/$${kpkg\:Kernel\-Version}/$(non_epoch_version)/' \
	debian/control.in > debian/control

	dh_testdir
	${ROOT_CMD} dh_testroot
	dh_prep

	# Build the module
	$(MAKE)

	# Install the module
	$(MAKE) install DESTDIR=$(CURDIR)/debian/$(pmodules)

	# don't include files already present in other packages!
	rm -f debian/$(pmodules)/usr/bin/rtai-config
	rm -rf debian/$(pmodules)/usr/lib
	rm -rf debian/$(pmodules)/usr/include

	${ROOT_CMD} dh_installdebconf
	${ROOT_CMD} dh_installchangelogs
	${ROOT_CMD} dh_installdocs
	${ROOT_CMD} dh_strip
	${ROOT_CMD} dh_compress
	${ROOT_CMD} dh_fixperms
	${ROOT_CMD} dh_installdeb
	${ROOT_CMD} dh_gencontrol -- -v$(epoch)$(pversion)+$(KDREV)
	${ROOT_CMD} dh_md5sums
	${ROOT_CMD} dh_builddeb --destdir=$(KPKG_DEST_DIR)

build-indep: config.status  build-indep-stamp
build-indep-stamp:
	dh_testdir
	touch build-indep-stamp

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp kdist_config-stamp \
		debian/rtai-source.tar.bz2 \
		base/arch/powerpc configure-stamp
	rm -f debian/linux/.config

	# Add here commands to clean up after the build process.
	[ ! -f GNUmakefile ] || $(MAKE) distclean
	# Leftovers due to upstream bug XXX
	rm -f base/arch/*/calibration/Makefile
	rm -f base/arch/*/hal/Makefile

	rm -f config.log config.status .rtai_config .rtai_config.old rtai_config.h \
		libtool GNUmakefile stamp-h1 .cfok .cfchanged \
		build-arch-stamp build-indep-stamp
	rm -f config.sub config.guess base/config/autoconf/config.sub \
		base/config/autoconf/config.guess configure
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	#(cd base/arch && ln -s mips mipsel ; ln -s ppc powerpc )

	cd base/sched/liblxrt && $(MAKE) install DESTDIR=$(CURDIR)/debian/librtai-dev
	mkdir -p $(CURDIR)/debian/librtai1/usr/lib
	mv $(CURDIR)/debian/librtai-dev/usr/lib/liblxrt.so.* $(CURDIR)/debian/librtai1/usr/lib
	cd base/include && $(MAKE) install DESTDIR=$(CURDIR)/debian/librtai-dev
	cd rtai-py && $(MAKE) install pythondir=/usr/share/pyshared DESTDIR=$(CURDIR)/debian/python-rtai
	chmod a-x $(CURDIR)/debian/python-rtai/usr/share/pyshared/*.py
	dh_install -plibrtai-dev --sourcedir=. base/scripts/rtai-config usr/bin

	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(package)
	dh_installdirs -p$(psource)  usr/src/modules/$(package)/debian

	# Copy only the driver source to the proper location
	cp -a  \
		aclocal.m4 \
		ChangeLog \
		config.guess \
		config.sub \
		configure \
		configure.in \
		GNUmakefile.am \
		GNUmakefile.in \
		makefile \
		README.INSTALL \
		rtai_config.h.in \
		addons \
		base \
		testsuite \
		rtai-lab \
		rtai-py \
		debian/$(psource)/usr/src/modules/$(package)
	# move the defconfig files for all architecture we support to template file name
	mv debian/$(psource)/usr/src/modules/$(package)/base/arch/$(HOST_CPU)/defconfig \
		debian/$(psource)/usr/src/modules/$(package)/base/arch/$(HOST_CPU)/defconfig.in
#	# Copy the needed debian/ pieces to the proper location
#	cp debian/*-modules.* \
#		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/control.modules.in \
		debian/$(psource)/usr/src/modules/$(package)/debian/control.in
	cp debian/rules \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/compat \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/changelog \
		debian/$(psource)/usr/src/modules/$(package)/debian
	cp debian/copyright \
		debian/$(psource)/usr/src/modules/$(package)/debian

	tar cjf debian/rtai-source/usr/src/rtai-source.tar.bz2 --directory debian/$(psource)/usr/src modules
	rm -rf debian/$(psource)/usr/src/modules

	# Patch collection
	find base/arch -name 'hal-linux-*.patch' | xargs -I XXX cp XXX debian/rtai-source/usr/src/kernel-patches/diffs/rtai/

	# this one is also included in gcc-multilib (on some archs), see #707671
	-rm -f $(CURDIR)/debian/librtai-dev/usr/include/asm

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs ChangeLog -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i
	dh_pysupport -i
	dh_installman -i
	dh_lintian -i
	dh_link -i
	dh_compress -i -X.pdf $(CURDIR)/debian/rtai-source/usr/src/kernel-patches/diffs/rtai/*
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
	dh_installdocs -s
	dh_installexamples -s
	dh_install -s
	dh_installmenu -s
	dh_pysupport -s
	dh_lintian -s
	dh_installcron -s
	dh_installinfo -s
	dh_installchangelogs ChangeLog -s
	dh_strip -s
	dh_link -s
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install binary-modules kdist kdist_config kdist_image kdist_clean
