#!/usr/bin/make -f
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DPKG_EXPORT_BUILDFLAGS=1
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/buildflags.mk
FFLAGS += -fPIC
CFLAGS += -fPIC

%:
	dh $@

override_dh_auto_configure:
	$(shell ! grep -q jessie /etc/os-release || \
	  echo "activate-noawait ldconfig" > $(CURDIR)/debian/libcdcl7.triggers )
	for f in cdclcc cdclclr cdclcmap cdclconfig cdclconfig701 cdcldate cdcldbs cdclexec cdclfont cdclpara cdcltone cdcltrf cdclver crepsl; do \
	  ( cd $(CURDIR)/debian && sh ./generate_man.sh $$f ) ;\
	done
	CC=gcc dh_auto_configure -- $(shell dpkg-buildfalgs --export=configure)

override_dh_auto_install:
	mkdir -p $(CURDIR)/debian/tmp/usr
	dh_auto_install -- prefix=$(CURDIR)/debian/tmp/usr
	find $(CURDIR)/debian/tmp/usr/bin/ -type f \
	  ! -name "*dclps*" \
	  ! -name "cdclcc"  \
	  ! -name "cdclconfig*" \
	  | xargs chrpath -d
	$(CC) $(LDFLAGS) -shared -Wl,-soname,libf2c4dcl.so.7 \
	  -o $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libf2c4dcl.so.7.0.1 \
	  `find $(CURDIR)/f2csrc -name \*.o`
	$(CC) $(LDFLAGS) -shared -Wl,-soname,libcdcl.so.7 \
	  -o $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcdcl.so.7.0.1 \
	  `find $(CURDIR)/src -name \*.o`
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libcdcl.so.7.0.1 libcdcl.so.7 )
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libcdcl.so.7.0.1 libcdcl.so )
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libf2c4dcl.so.7.0.1 libf2c4dcl.so.7 )
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libf2c4dcl.so.7.0.1 libf2c4dcl.so )


override_dh_install:
	dh_install --sourcedir=debian/tmp

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(CURDIR)/debian/*.1
	rm -f $(CURDIR)/bin/cdclpsseparate
	rm -f $(CURDIR)/bin/cdclpsunrot

override_dh_installchangelogs:
	dh_installchangelogs --all --keep HISTORY
