#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# ctn code base is large and ancient (including compatibility code for
# supporting compilers of the pre-C89 era).  Patching to accomodate for
# contemporary compiler behavior results in unreasonably large changes,
# so we favor dropping the checks for dangerous behaviors.  For more
# information, refer to #1074893.
export DEB_CFLAGS_MAINT_APPEND = \
	-Wno-error=incompatible-pointer-types \
	-Wno-error=implicit-int \
	-Wno-error=int-conversion

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`
