#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
SHELL=bash

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

CFLAGS = -Wall -Wno-unknown-pragmas -g -pipe -fPIC -I. \
         -D_GNU_SOURCE -DNEED_SOLARIS_BOOLEAN \
	-isystem /usr/include/freebsd \
	-Werror=implicit-function-declaration \
	$(NULL)

# Satisfy 64-bit off_t assumptions in the compat layer (see #734782)
CFLAGS += -D_FILE_OFFSET_BITS=64

# Enable fseeko / ftello prototypes, used in the compat layer
CFLAGS += -D_LARGEFILE_SOURCE

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))

	# Do not mess with MAKEFLAGS until we transfer control to BSD make (see below)
	BSD_MAKEFLAGS += -j$(NUMJOBS)
endif

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

SVN_BASE	:= https://svn.freebsd.org/base
#SVN		:= $(SVN_BASE)/release/$(version).0
SVN		:= $(SVN_BASE)/releng/$(version)
#SVN		:= $(SVN_BASE)/stable/$(major)
#SVN		:= $(SVN_BASE)/head

SOURCE = ctfutils
ORIG_DIR = $(SOURCE)-$(tar_version)
ORIG_TAR = $(SOURCE)_$(tar_version).orig.tar.xz

PATH := /usr/lib/freebsd:$(PATH)
SOURCE_DATE_EPOCH ?= $(shell date -d "$$(dpkg-parsechangelog -SDate)" +%s)
DESTDIR = $(CURDIR)/debian/tmp
PMAKE = \
	MAKEFLAGS=$(BSD_MAKEFLAGS) \
	COPTS="$(CFLAGS)" CFLAGS="$(CFLAGS)" NO_WERROR=1 NOGCCERROR=1 \
	OPENSOLARIS_USR_DISTDIR="$(CURDIR)/cddl/contrib/opensolaris" \
	OPENSOLARIS_SYS_DISTDIR="$(CURDIR)/sys/cddl/contrib/opensolaris" \
        NOSHARED=NO NO_SHARED=NO DESTDIR=$(DESTDIR) make
export SHLIB_MAJOR = 0

get-orig-source:
	rm -rf $(ORIG_DIR)
	for i in \
		cddl/compat/opensolaris \
		sys/cddl/compat/opensolaris \
		\
		cddl/contrib/opensolaris/common/ctf \
		cddl/contrib/opensolaris/tools/ctf \
		cddl/contrib/opensolaris/lib/libctf \
		cddl/contrib/opensolaris/lib/libgen \
		sys/cddl/contrib/opensolaris/uts/common/ctf \
		sys/cddl/contrib/opensolaris/uts/common/sys \
		\
		cddl/lib/libctf \
		cddl/usr.bin/ctfconvert \
		cddl/usr.bin/ctfdump \
		cddl/usr.bin/ctfmerge \
	         ; do \
	    svn export $(SVN)/$$i $(ORIG_DIR)/$$i || exit ; \
	done
	# Clamp timestamps to be no newer than last changelog entry, see
	# https://wiki.debian.org/ReproducibleBuilds/TimestampsInTarball
	find $(ORIG_DIR) -newermt "@$(SOURCE_DATE_EPOCH)" -print0 \
	 | xargs -0r touch --no-dereference --date="@$(SOURCE_DATE_EPOCH)"
	# Create tarball with files sorted in a stable order, see
	# https://wiki.debian.org/ReproducibleBuilds/FileOrderInTarballs
	find $(ORIG_DIR) -print0 | LC_ALL=C sort -z \
	 | tar --null --no-recursion --files-from - \
	    --numeric-owner --owner 0 --group 0 \
	    -cJf ../$(ORIG_TAR)
	rm -rf $(ORIG_DIR)

build: build-arch build-indep

build-arch: build-ctfutils

build-indep:
# We have nothing to do by default.


build-libctf: build-libctf-stamp
build-libctf-stamp:
	$(PMAKE) -C $(CURDIR)/cddl/lib/libctf
	touch $@

build-ctfutils: build-ctfutils-stamp
build-ctfutils-stamp: build-libctf-stamp build-libctf-stamp
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfconvert
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfdump
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfmerge
	touch $@

clean:
	dh_testdir
	dh_testroot
	$(PMAKE) -C $(CURDIR)/cddl/lib/libctf clean
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfconvert clean
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfdump clean
	$(PMAKE) -C $(CURDIR)/cddl/usr.bin/ctfmerge clean

	rm -f *stamp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep -s
	dh_installdirs -s
	dh_install -s


binary: binary-arch binary-indep

binary-indep: build install
# We have nothing to do by default.

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_installdocs -a
#	dh_installexamples
	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_perl
#	dh_python
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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