#!/usr/bin/make -f

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
upstreamver = $(shell echo $(debver) | cut -d '-' -f 1,1 )
# this figures out the last merge point from 'master' into the Debian branch and
# then described this commit by parsing its commit message
gitver = $(shell [ -x /usr/bin/git ] && git log -n 1 --oneline $$(git merge-base -a HEAD upstream) | cut -d ' ' -f '4,4')

# get install locations for matlab packages
include /usr/share/matlab/debian/defs.make

# TODO plugins do not handle this well
LDFLAGS = -Wl,--allow-shlib-undefined
export LDFLAGS

# need to do it after inclusion of cmake class to get the flag
# appended after -Wall
CFLAGS += -Wno-unused
export CFLAGS

# what are we doing this for
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
# where to perform the general build
DEB_BUILDDIR = "build-$(DEB_BUILD_GNU_TYPE)"
# where to build the static corelibs
DEB_BUILDDIR_STATIC = "build-$(DEB_BUILD_GNU_TYPE)-static"

DEB_CMAKE_EXTRA_FLAGS = \
    -DAFNI_SHOWOFF:STRING="=Debian-$(DEB_BUILD_GNU_TYPE)" \
    -DBUILD_WITH_INSTALL_RPATH:BOOL=ON \
    -DCMAKE_SKIP_RPATH:BOOL=OFF \
    -DCMAKE_INSTALL_RPATH:STRING="/usr/lib/afni/lib" \
    -DCMAKE_EXE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
    -DCMAKE_MODULE_LINKER_FLAGS:STRING="$(LDFLAGS)" \
    -DCMAKE_SHARED_LINKER_FLAGS:STRING="$(LDFLAGS)" \
    -DAFNI_BUILD_LOCAL_NIFTICLIBS:BOOL=OFF \
    -DAFNI_BUILD_LOCAL_GIFTI:BOOL=OFF \
    -DAFNI_BUILD_LOCAL_3DEDGE3:BOOL=OFF \
    -DAFNI_BUILD_WITH_LESSTIF2:BOOL=ON \
    -DAFNI_INSTALL_BIN_DIR:STRING=/lib/afni/bin \
    -DAFNI_INSTALL_LIB_DIR:STRING=/lib/afni/lib \
    -DAFNI_INSTALL_INCLUDE_DIR:STRING=/include/afni \
    -DAFNI_INSTALL_PLUGIN_DIR:STRING=/lib/afni/plugins \
    -DAFNI_INSTALL_SCRIPT_DIR:STRING=/share/afni/scripts \
    -DAFNI_INSTALL_PICS_DIR:STRING=/share/afni/pics \
    -DAFNI_INSTALL_POEMS_DIR:STRING=/share/afni/poems

# one ring to rule them all ...
%:
	dh $@ --buildsystem=cmake


override_dh_auto_configure:
	# check whether something changed in the matlab install locations
	# in which case we'd have to adjust debhelper's .install files
	# content
	[ "$(MATLAB_MDIR)" = "/usr/share/matlab/site/m" ]
	dh_auto_configure --builddirectory=$(DEB_BUILDDIR) -- \
      $(DEB_CMAKE_EXTRA_FLAGS) \
      -DBUILD_SHARED_LIBS:BOOL=ON \
      -DAFNI_BUILD_CORELIBS_ONLY:BOOL=OFF
	dh_auto_configure --builddirectory=$(DEB_BUILDDIR_STATIC) -- \
      $(DEB_CMAKE_EXTRA_FLAGS) \
      -DBUILD_SHARED_LIBS:BOOL=OFF \
      -DAFNI_BUILD_CORELIBS_ONLY:BOOL=ON


override_dh_auto_build:
	dh_auto_build --builddirectory=$(DEB_BUILDDIR)
	dh_auto_build --builddirectory=$(DEB_BUILDDIR_STATIC)


override_dh_auto_install:
	dh_auto_install --builddirectory=$(DEB_BUILDDIR)
	dh_auto_install --builddirectory=$(DEB_BUILDDIR_STATIC) -- \
	  DESTDIR=$(CURDIR)/debian/tmp/static 
	# fix tcsh interpreter location
	for f in $$(egrep -lR '^#![ ]*/bin/tcsh' debian/tmp); do \
		echo "Fixing tcsh interpreter location in '$$f'" ; \
		sed -i -e 's|^#![ ]*/bin/tcsh|#!/usr/bin/tcsh|' $$f ; \
	done
	# for some reason 3dSetupGroupInCorr gets destroyed during install
	# need to redo and fix rpath manually
	cp $(DEB_BUILDDIR)/3dSetupGroupInCorr debian/tmp/lib/afni/bin/
	chrpath -r /usr/lib/afni/lib debian/tmp/lib/afni/bin/3dSetupGroupInCorr
	# matlab stuff -- fix permission
	install -d debian/tmp/matlab
	install -m 644 -t debian/tmp/matlab matlab/*


override_dh_auto_clean:
	dh_auto_clean --builddirectory=$(DEB_BUILDDIR)
	dh_auto_clean --builddirectory=$(DEB_BUILDDIR_STATIC)
	# ensure proper permissions for the wrapper
	chmod +x debian/afni_wrapper


# make orig tarball from repository content
get-orig-source:
	-quilt pop -a
	@echo "Testing for uncommited changes"
	@git diff --quiet HEAD
	# orig tarball, turn directory into something nicer
	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ HEAD | \
		gzip -9 > $(srcpkg)_$(gitver).orig.tar.gz
