#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

DEB_HOST_ARCH_CPU := $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

full_version	:= $(shell dpkg-parsechangelog | grep ^Version: | sed -e 's/^.*: //g')
version		:= $(shell echo $(full_version) | sed -e 's/[~+-].*//g')
major           := $(shell echo $(version) | sed -e 's/\..*//g')

ifeq ($(DEB_HOST_ARCH_CPU), mipsel)
kfreebsd_cpu	:= mips
else
kfreebsd_cpu	:= $(DEB_HOST_ARCH_CPU)
endif

HEADERS_PACKAGE	:= $(CURDIR)/debian/kfreebsd-kernel-headers
SRC_TAR		:= /usr/src/kfreebsd-source-$(version).tar.xz

clean:
	dh_testdir
	dh_testroot
	rm -rf .pc
	rm -f *-stamp
	rm -rf sys usr.sbin misc

	$(MAKE) -C test clean

	dh_clean

.PHONY: clean

build-indep:

build-arch: build-arch-stamp
build-arch-stamp:
	dh_testdir

	tar xfJ $(SRC_TAR) --wildcards --strip-components=1 "*.h"
	mkdir -p misc/sys

	set -e ; \
	find sys -type f | (while read i ; do \
	sed -i $$i \
		-e 's/__FreeBSD_version/__FreeBSD_kernel_version/g' \
		\
		-e 's/defined\s*(\s*__GNUCLIKE_[A-Z_]\+\s*)/defined(__GNUC__)/g' \
		-e 's/#\s*ifdef\s*__GNUCLIKE_[A-Z_]\+\(\s\|$$\)/#if defined(__GNUC__)/g' \
		-e 's/#\s*ifndef\s*__GNUCLIKE_[A-Z_]\+\(\s\|$$\)/#if !defined(__GNUC__)/g' \
		\
		-e '/^\s*__FBSDID\s*(.*)\s*;$$/d' \
		\
		-e 's/defined\s*(\s*__CC_SUPPORTS_\(INLINE\|__INLINE\|__INLINE__\|__FUNC__\|WARNING\|VARADIC_XXX\|DYNAMIC_ARRAY_INIT\)\s*)/defined(__GNUC__)/g' \
		-e 's/#\s*ifdef\s*__CC_SUPPORTS_\(INLINE\|__INLINE\|__INLINE__\|__FUNC__\|WARNING\|VARADIC_XXX\|DYNAMIC_ARRAY_INIT\)\(\s\|$$\)/#if defined(__GNUC__)/g' \
		-e 's/#\s*ifndef\s*__CC_SUPPORTS_\(INLINE\|__INLINE\|__INLINE__\|__FUNC__\|WARNING\|VARADIC_XXX\|DYNAMIC_ARRAY_INIT\)\(\s\|$$\)/#if !defined(__GNUC__)/g' \
		\
		-e 's/__unused/__attribute__((__unused__))/g' \
		-e 's/__used/__attribute__((__used__))/g' \
		-e 's/__packed\(\s\|;\)/ __attribute__((__packed__))\1/g' \
		-e 's/__aligned(\([^)]*\?\))/ __attribute__((aligned(\1)))/g' \
		-e 's/\(\s\|^\|(\)u_\(char\|int\|short\|long\)\(\s\)/\1unsigned \2\3/g' \
		-e 's/__printflike(\([0-9]\+\),\s*\([0-9]\+\))/__attribute__((__format__ (__printf__, \1, \2)))/g' \
		\
		-e 's/defined\s*(\s*_SYS_CDEFS_H_\s*)/defined(_SYS_CDEFS_H)/g' \
		-e 's/#\s*ifdef\s*_SYS_CDEFS_H_\(\s\|$$\)/#ifdef _SYS_CDEFS_H/g' \
		-e 's/#\s*ifndef\s*_SYS_CDEFS_H_\(\s\|$$\)/#ifndef _SYS_CDEFS_H/g' \
		\
		-e 's/#\s*include\s\+<sys\/types\.h>\s*/#include <sys\/kglue\/sys\/types.h>/g' \
		-e 's/#\s*include\s\+<sys\/time\.h>\s*/#include <sys\/kglue\/sys\/time.h>/g' \
		-e 's/#\s*include\s\+<sys\/param\.h>\s*/#include <sys\/kglue\/sys\/param.h>/g' \
		-e 's/#\s*include\s\+<sys\/signal\.h>\s*/#include <sys\/kglue\/sys\/signal.h>/g' \
		-e 's/#\s*include\s\+<sys\/queue\.h>\s*/#include <sys\/kglue\/sys\/queue.h>/g' \
		-e 's/#\s*include\s\+<netinet\/if_ether\.h>\s*/#include <sys\/kglue\/netinet\/if_ether.h>/g' \
		-e 's/#\s*include\s\+<sys\/_termios\.h>\s*/#include <sys\/termios.h>/g' \
		\
		-e 's/__size_t/__this_is_a_temporary_kludge__size_t/g' $(shell ## see rev 184135 in GCC SVN ##) \
		\
		-e 's/\(\s\|^\|(\)\(__\)\?ino_t\(\s\|)\|;\)/\1__kernel_ino_t\3/g' \
		-e 's/\(\s\|^\|(\)\(__\)\?MAXLOGNAME\(\s\|)\|;\)/\1__kernel_MAXLOGNAME\3/g' \
		-e 's/\(\s\|^\|(\)\(__\)\?WCHAR_MAX\(\s\|)\|;\)/\1__kernel_WCHAR_MAX\3/g' \
		-e 's/\(\s\|^\|(\)\(__\)\?WCHAR_MIN\(\s\|)\|;\)/\1__kernel_WCHAR_MIN\3/g' \
	; done)
	echo "#include <sys/kern/param.h>" > misc/osreldate.h
	echo "#include <stdint.h>" > misc/sys/stdint.h

	# Revert the kglue redirection when mistakenly applied to headers that want to
	# include the GNU version (otherwise they'll just be including themselves!)
	sed -e 's/#\s*include\s\+<sys\/kglue\/sys\/types\.h>\s*/#include <sys\/types.h>/g' -i sys/sys/types.h
	sed -e 's/#\s*include\s\+<sys\/kglue\/sys\/time\.h>\s*/#include <sys\/time.h>/g' -i sys/sys/time.h
	sed -e 's/#\s*include\s\+<sys\/kglue\/sys\/param\.h>\s*/#include <sys\/param.h>/g' -i sys/sys/param.h
	sed -e 's/#\s*include\s\+<sys\/kglue\/sys\/signal\.h>\s*/#include <sys\/signal.h>/g' -i sys/sys/signal.h
	sed -e 's/#\s*include\s\+<sys\/kglue\/sys\/queue\.h>\s*/#include <sys\/queue.h>/g' -i sys/sys/queue.h
	sed -e 's/#\s*include\s\+<sys\/kglue\/netinet\/if_ether\.h>\s*/#include <netinet\/if_ether.h>/g' -i sys/netinet/if_ether.h

	QUILT_PATCHES=$(CURDIR)/debian/patches quilt push -a || test $$? = 2

	touch $@

build: build-indep build-arch

.PHONY: build-indep build-arch build

install-indep: build-indep

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install --sourcedir=$(CURDIR)
	dh_link

ifneq ($(filter amd64, $(kfreebsd_cpu)),)
	sh debian/generate-asm.sh $(kfreebsd_cpu) $(CURDIR)/sys $(HEADERS_PACKAGE)/usr/include
else
	mkdir -p $(HEADERS_PACKAGE)/usr/include/machine
	cd $(CURDIR)/sys/$(kfreebsd_cpu)/include \
		&& find . -type f -name "*.h" -exec cp --parents {} $(HEADERS_PACKAGE)/usr/include/machine \;
endif

ifneq ($(filter i386 amd64, $(kfreebsd_cpu)),)
	# Install "x86" directory (only on i386 and amd64).
	mkdir -p $(HEADERS_PACKAGE)/usr/include/x86
	cd $(CURDIR)/sys/x86/include \
		&& find . -type f -name "*.h" -exec cp --parents {} $(HEADERS_PACKAGE)/usr/include/x86 \;
endif

	# headers must be tested after they're installed
	$(MAKE) -C test

install: install-indep install-arch

.PHONY: install-indep install-arch install

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

# Build architecture-dependent files here.
binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_installdocs
#	dh_installman
	dh_installchangelogs
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: binary-indep binary-arch binary
