#!/usr/bin/make -f
# -*- makefile -*-

export DH_VERBOSE=1

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
BMAKEFLAGS += -j$(NUMJOBS)
endif

ifeq "$(DEB_BUILD_ARCH)" "i386"
EPICS_HOST_ARCH:=linux-x86
else ifeq "$(DEB_BUILD_ARCH)" "amd64"
EPICS_HOST_ARCH:=linux-x86_64
else
EPICS_HOST_ARCH:=$(shell ./startup/EpicsHostArch)
endif
export EPICS_HOST_ARCH

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Link only --as-needed
# EPICS uses -O3, let's not lower the standards
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
export DEB_CFLAGS_STRIP = -g -O2
export DEB_CXXFLAGS_STRIP = -g -O2

# chop out the source version from Changelog (ie 3.14.12)
# Taken from CDBS
DEB_VERSION = $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION = $(shell echo $(DEB_VERSION) | cut -d: -f2-)
SOV=$(shell echo "$(DEB_NOEPOCH_VERSION)"| cut -f '1' -d '-')
DEBV=$(shell echo "$(DEB_NOEPOCH_VERSION)"| cut -f '2-' -d '-')

# fetch the list of all binary packages being built
ALLBINS := $(shell dh_listpackages)

TARGETS = $(EPICS_HOST_ARCH)-debug

#ifneq (,$(findstring epics-win32,$(ALLBINS)))
#TARGETS += win32-x86-mingw
#endif

export CROSS_COMPILER_TARGET_ARCHS=$(TARGETS)

export SHRLIB_VERSION=$(SOV)

%:
	ls modules/RE* || true
	dh $@ --with epics -Sepicsmake --parallel
	ls modules/RE* || true

override_dh_auto_clean:
	ls modules/RE* || true
	dh_auto_clean
	rm -rf configure/conf.d
	rm -f modules/RE* || true

override_dh_auto_build:
	install -d configure/conf.d
	./debian/gen-harden.sh > configure/conf.d/05hardening.make
	dh_auto_build -- FINAL_LOCATION=/usr/lib/epics EPICS_SITE_VERSION=$(DEBV)
	sed -i -e "s|\$$ENV{EPICS_HOST_ARCH}|$(EPICS_HOST_ARCH)|g" bin/*/makeBaseApp.pl

override_dh_auto_install:
	dh_auto_install -- FINAL_LOCATION=/usr/lib/epics EPICS_SITE_VERSION=$(DEBV)

# skip this since we should install configure/RELEASE
override_dh_epics_postinstall:
	# no op

override_dh_auto_test: # skip for development

USRBINS=makeBaseApp.pl makeBaseExt.pl softIoc caget caput camonitor cainfo caRepeater casw msi iocLogServer ascheck

PERLBINS=caget.pl caput.pl camonitor.pl cainfo.pl capr.pl

# /usr/share/perl5
PERLLIB=$(shell perl -MConfig -e 'print $$Config{vendorlib}')
# /usr/lib/x86_64-linux-gnu/perl5/5.20  (deb8)
# /usr/lib/perl5 (deb7)
PERLARCH=$(shell perl -MConfig -e 'print $$Config{vendorarch}')

override_dh_install:
	sed -i -e "s|^.*EPICS_SITE_VERSION.*$$|EPICS_SITE_VERSION = $(DEBV)|" debian/tmp/usr/lib/epics/configure/CONFIG_SITE

	# some object code is getting installed somehow
	rm -f debian/tmp/usr/lib/epics/bin/linux-*/*.o

	# relocate configure/
	# copy to /etc/epics/configure and /usr/share/epics-dev/configure

	install -d debian/tmp/etc/epics
	install -d debian/tmp/usr/share/epics-dev
	# make sure the clang compiler specific headers get included
	# this is to help with clang based tooling and interop
	install -d debian/tmp/usr/lib/epics/include/compiler/clang
	cp -dR modules/libcom/src/osi/compiler/clang debian/tmp/usr/lib/epics/include/compiler/

	cp -dR debian/tmp/usr/lib/epics/configure debian/tmp/etc/epics/
	mv debian/tmp/usr/lib/epics/configure debian/tmp/usr/share/epics-dev/configure

	install -d debian/tmp/usr/lib/epics/configure

	# populated /usr/lib/epics/configure with symlinks to /etc/epics/configure
	# except for conf.d and rules.d
	for ff in debian/tmp/etc/epics/configure/*; do \
		ln -s /etc/epics/configure/`basename $$ff` debian/tmp/usr/lib/epics/configure/`basename $$ff`; \
	done

	install -d debian/tmp/etc/epics/configure/conf.d
	cp configure/conf.d/05hardening.make debian/tmp/etc/epics/configure/conf.d/
	echo "ALL_CROSS_COMPILER_TARGET_ARCHS += $(TARGETS)" > debian/tmp/etc/epics/configure/conf.d/01debug.make

	install -d debian/tmp/usr/bin

	# install executable to /usr/bin
	for B in $(USRBINS); do mv debian/tmp/usr/lib/epics/bin/$(EPICS_HOST_ARCH)/$$B debian/tmp/usr/bin/$$B; done
	# remove extension from perl scripts
	for P in debian/tmp/usr/bin/*.pl; do mv $$P $${P%.pl};done
	# leave sym-links to /usr/bin in /usr/lib/epics/bin/$EPICS_HOST_ARCH
	for B in debian/tmp/usr/bin/*; \
		do ln -s ../../../../bin/`basename $$B` debian/tmp/usr/lib/epics/bin/$(EPICS_HOST_ARCH)/`basename $$B`; \
	done

	install -d debian/epics-dev/$(PERLLIB)
	install -d debian/epics-perl/$(PERLARCH)/auto
	install -d debian/epics-perl/$(PERLARCH)/auto/Cap5

	# install EPICS::* modules under /$(PERLLIB)/
	mv debian/tmp/usr/lib/epics/lib/perl/EPICS debian/epics-dev/$(PERLLIB)/

	# Relocate CA to /usr/lib/perl5/
	mv debian/tmp/usr/lib/epics/lib/perl/CA.pm debian/epics-perl/$(PERLARCH)/
	# libCap5.so to /usr/lib/perl5/auto/
	mv debian/tmp/usr/lib/epics/lib/perl/5.*/*/libCap5.so debian/epics-perl/$(PERLARCH)/auto/Cap5/Cap5.so

	# Don't need other copies
	rm debian/tmp/usr/lib/epics/lib/linux-*/libCap5.so

	# This path is used by CA.pm
	ln -s ${EPICS_HOST_ARCH} debian/tmp/usr/lib/epics/lib/host

	dh_install --fail-missing

	install -d debian/epics-perl/usr/lib/epics/bin/$(EPICS_HOST_ARCH)
	install -d debian/epics-perl/usr/lib/epics/bin/$(EPICS_HOST_ARCH)-debug

	for arch in $(EPICS_HOST_ARCH); do for name in $(PERLBINS); do \
	  mv debian/epics-dev/usr/lib/epics/bin/$$arch/$$name debian/epics-perl/usr/lib/epics/bin/$$arch/ \
	;done ;done

ifneq (,$(findstring epics-win32,$(ALLBINS)))
	install -d debian/epics-win32/usr/lib/epics/include/os
	mv debian/epics-dev/usr/lib/epics/include/os/WIN32 debian/epics-win32/usr/lib/epics/include/os/
endif

	chmod -x debian/epics-dev/usr/lib/epics/configure/Sample.Makefile

override_dh_installdocs:
	dh_installdocs -A README

override_dh_strip_rtems:
	dh_strip_rtems --dbg-package=libepics$(SOV)-dbg

override_dh_makeshlibs:
	dh_makeshlibs -Nepics-dev -a
	# prevent the .so symlinks under usr/lib/epics/lib from being detected.
	dh_makeshlibs -pepics-dev -Xusr/lib/epics

override_dh_shlibdeps:
	dh_shlibdeps -Nlibepics$(SOV) -a
	# Prevent self dependency
	#  This would happen because the package provides several dependent libraries
	dh_shlibdeps -plibepics$(SOV) -- -xlibepics$(SOV)

# We provide our own deps
override_dh_epicsdep:

# Failure to start does not halt package install
override_dh_installinit:
	dh_installinit --error-handler=true
