#!/usr/bin/make -f
#export DH_VERBOSE=1

-include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

VER_MAJOR = 1
VER_MINOR = 00
VER_PATCH = $(shell dpkg-parsechangelog | grep '^Version' | cut -d' ' -f2 | cut -f1 -d- | cut -f2 -d~)
UPSTREAM_SRC = dicom3tools_1.00.snapshot.$(VER_PATCH)

VER_FULL = $(VER_MAJOR).$(VER_MINOR)~$(VER_PATCH)
DEBIAN_SRC_DIR = dicom3tools-$(VER_FULL)
DEBIAN_SRC_TAR = dicom3tools_$(VER_FULL).orig.tar.gz

ifneq (,$(findstring regression_testing_dicom,$(MM_BUILD_OPTIONS)))
DCLIST = -name andump\* -o -name dcsmpte\* -o -name dccp\* -o -name dcintro\* -o -name pdftodc\* -o -name rawnjl2\* -o -name ancreate\* -o -name dcencap\* -o -name dctoraw\* -o -name dcjpeg\* -o -name dcmulti\* -o -name dcuncat\* -o -name pnmtodc\* -o -name dcjls\* -o -name dcj2k\* -o -name dcunjpeg\* -o -name dcunjls\* -o -name bmpdump\* -o -name jpegdump\* -o -name dcdirdmp\* -o -name dcdump\* -o -name dcentvfy\* -o -name dcfile\* -o -name dchist\* -o -name dciodvfy\* -o -name dckey\* -o -name dcsort\* -o -name dcsrdump\* -o -name dcstats\* -o -name dctable\* -o -name dctopgm8\* -o -name dcposn\* -o -name dctopgx\* -o -name dctopnm\* -o -name pgxtodc\* -o -name rawftodc\* -o -name rawtodc\* -o -name dcunrgb\*
ifneq (,$(filter default_uid_root=%,$(MM_BUILD_OPTIONS)))
DEFAULTUIDROOT = $(patsubst default_uid_root=%,%,$(filter default_uid_root=%,$(MM_BUILD_OPTIONS)))
IMAKEFLAGS = -DDefaultUIDRoot=$(DEFAULTUIDROOT)
endif
else
DCLIST = -name andump\* -o -name jpegdump\* -o -name dcdirdmp\* -o -name dcdump\* -o -name dcentvfy\* -o -name dcfile\* -o -name dchist\* -o -name dciodvfy\* -o -name dckey\* -o -name dcsort\* -o -name dcsrdump\* -o -name dcstats\* -o -name dctable\* -o -name dctopgm8\* -o -name dcposn\* -o -name dctopgx\* -o -name dctopnm\* -o -name dcunrgb\*
endif

configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	# http://www.gnu.org/manual/gawk/html_node/Special-FD.html
	find . -name \*.awk -exec sed -i -e 's@/dev/tty@/dev/stderr@g' {} \;
	# Add here commands to configure the package.
	sh Configure
	imake -I./config -DTmpPath=/tmp -DOptimizeLevel=-O2 $(IMAKEFLAGS)
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) World C_DEBUGFLAGS="$(CFLAGS)" CPLUSPLUS_DEBUGFLAGS="$(CXXFLAGS)" C_EXTRA_LOAD_FLAGS="$(LDFLAGS)" CPLUSPLUS_EXTRA_LOAD_FLAGS="$(LDFLAGS)"
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) clean
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	#dh_clean -k 
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/dicom3tools.
	# install everything
	$(MAKE) DESTDIR=$(CURDIR)/debian/dicom3tools install
	# install man pages:
	$(MAKE) DESTDIR=$(CURDIR)/debian/dicom3tools install.man
	# remove empty man page
	find $(CURDIR)/debian/dicom3tools -type f -size 0 -print | xargs rm
	# now that we have removed empty man page, we need to remove associated binaries
	for file in ancp andiff antodc.all binpatch dcanon dcburn dccmp dccomb dcdecmpr dcdiff dclutburn \
							dclutmix dcmvhier.8only dcmvhier.all dcortho dcostosr dcpost dcproj dcrmsfx.all \
							dcsrmrg dcsub dcswab dcunjpeg.all dumptiff dumpwhat gentodc.dat.all \
							jpegsplit pbmswbit pgmtobmp pnmpred pqsplit rawarith rawdiff rawmask; do \
		find $(CURDIR)/debian/dicom3tools -type f -name $$file\* -print | xargs rm; \
	done
	# Move the special .so (groff source file to /usr/share/dicom3tools to please lintian
	mkdir -p $(CURDIR)/debian/dicom3tools/usr/share/dicom3tools
	mv $(CURDIR)/debian/dicom3tools/usr/share/man/man1/*.so $(CURDIR)/debian/dicom3tools/usr/share/dicom3tools
	# Let's use the new file location:
	find $(CURDIR)/debian/dicom3tools/usr/share/man/man1 -type f -exec sed -i -e 's@.so man1@.so ../dicom3tools@g' {} \;
	# Cannot start a line with a . (it is a macro)
	# http://www.mail-archive.com/groff@gnu.org/msg04712.html
	find $(CURDIR)/debian/dicom3tools/usr/share/man/man1 -type f -exec sed -i -e 's/^\.\.\.$$/\\\&\.\.\./g' {} \;
	#  a newline character is not allowed in an escape name / a space character is not allowed in an escape name
	find $(CURDIR)/debian/dicom3tools/usr/share/man/man1 -type f -exec sed -i -e 's/\\n/\\/g' {} \;
	# hyphen-used-as-minus-sign
	#find $(CURDIR)/debian/dicom3tools/usr/share/man/man1 -type f -exec sed -i -e 's/-/\\-/g' {} \;
	# Can't use above -> manpage-has-bad-whatis-entry
	# Remove (except special list from official upstream releases) :
	find $(CURDIR)/debian/dicom3tools/usr/bin -type f -not \( $(DCLIST) \) -print | xargs rm;
	find $(CURDIR)/debian/dicom3tools/usr/share/man -type f -not \( $(DCLIST) \) -print | xargs rm;
	# Remove all static lib
	rm -rf $(CURDIR)/debian/dicom3tools/usr/lib

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

build-arch: build
build-indep: build

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

get-orig-source:
	uscan --verbose --force-download --rename --destdir ../tarballs

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