#!/usr/bin/make -f

pkg-ctn	:= ctn
pkg-ctn-dev := ctn-dev

%:
	dh $@

# we must provide *SIZE inside dicom.h or otherwise applications which
# would use ctn might be doomed to get improper U32 types etc
# that file will be included in dicom.h - hard patched
debian/savefiles:
	mkdir -p debian/savefiles
	mkdir -p debian/savefiles/apps/dcm_modify_object
	cp -a apps/dcm_modify_object/gram.h debian/savefiles/apps/dcm_modify_object

debian/test-sizes: debian/savefiles
	cc     debian/test-sizes.c   -o debian/test-sizes

debian/intsizes.h: debian/test-sizes
	sh debian/intsizes_h.sh

override_dh_auto_build: debian/intsizes.h
	debian/test-sizes >| debian/ctnmake.debian.mysql.options.full
	echo "CFLAGS=$(CFLAGS)" >> debian/ctnmake.debian.mysql.options.full
	echo "LDFLAGS=$(LDFLAGS)" >> debian/ctnmake.debian.mysql.options.full
	cat debian/ctnmake.debian.mysql.options >> debian/ctnmake.debian.mysql.options.full
	mv debian/intsizes.h facilities/dicom/
	chmod +x $(CURDIR)/apps/scripts/createdirectory
	chmod +x $(CURDIR)/apps/scripts/checkEnvironment
	mkdir $(CURDIR)/lib
	mkdir $(CURDIR)/include

	(. debian/debian.mysql.options.env; make install)

override_dh_auto_clean:
	dh_auto_clean
	# restore changed files
	if [ -d debian/savefiles ] ; then \
	    mv debian/savefiles/apps/dcm_modify_object/gram.h apps/dcm_modify_object ; \
	    rm -rf debian/savefiles/apps ; \
	fi
	rm -rf debian/savefiles
	rm -f debian/ctn*.postinst.* debian/ctn*.prerm.* debian/intsizes.h
	rm -f debian/ctnmake.debian.mysql.options.full debian/test-sizes
	rm -f facilities/dicom/intsizes.h
	rm -f libsrc/*.c libsrc/*.h
	rm -f `find . -name \*.o -or -name \*.a`
	rm -rf include
	rm -rf lib

override_dh_prep:
	# ctn build process does build+install in one rush
	# dear dh_prep, please leave my debian/tmp
	mv $(CURDIR)/debian/tmp $(CURDIR)/debian/do_not_delete_this_tmp_dir
	dh_prep
	mv $(CURDIR)/debian/do_not_delete_this_tmp_dir $(CURDIR)/debian/tmp

override_dh_auto_install:
	echo 'Do not try to call `make install` since it requires setting variable DICOM_MAKE'

override_dh_fixperms:
	dh_fixperms
	chmod +x `echo $(CURDIR)/debian/$(pkg-ctn)/usr/share/ctn/mysql/*`
	chmod -x `echo $(CURDIR)/debian/$(pkg-ctn)/usr/share/ctn/mysql/*.script`

# Remark: The following uscan command requires devscripts > 2.12.4 which is not
#         yet released at the time of this package release.  The code can be obtained
#         via
#   git clone git://tille@git.debian.org/git/users/tille/devscripts.git
#         and then use scripts/uscan.pl
get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs --repack-compression xz
