#!/usr/bin/make -f
# -*- makefile -*-
export DH_OPTIONS
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@

override_dh_auto_configure:
	FC=/usr/bin/gfortran FFLAGS="-fPIC -g -Wall -O2 " CC=/usr/bin/gcc CFLAGS="-fPIC -g -Wall -O2 -lgfortran"  \
	  dh_auto_configure -- --enable-shared --prefix=/usr
	sed 's/$$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/' $(CURDIR)/debian/libf77dcl5.install.in > $(CURDIR)/debian/libf77dcl5.install
	sed 's/$$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/' $(CURDIR)/debian/libf77dcl-dev.install.in > $(CURDIR)/debian/libf77dcl-dev.install

override_dh_auto_install:
	mkdir -p $(CURDIR)/debian/tmp/usr
	dh_auto_install -- prefix=$(CURDIR)/debian/tmp/usr
	rm debian/tmp/usr/bin/dclconfig
	find debian/tmp/usr/bin -type f   \
		! -name "*dclps*"             \
		! -name "dclcc"               \
		! -name "dclfrt"              \
		! -name "dclconfig*"          \
		| xargs chrpath -d
	for f in `find debian/tmp/usr/bin/ ! -type d ` ; do \
		mv $$f debian/tmp/usr/bin/`basename $$f`-f77 ;\
	done
	rm debian/tmp/usr/bin/dclfrt-f77 ; cp debian/dclfrt debian/tmp/usr/bin/dclfrt-f77 ; chmod 755 debian/tmp/usr/bin/dclfrt-f77
	rm debian/tmp/usr/bin/dclcc-f77  ; cp debian/dclcc debian/tmp/usr/bin/dclcc-f77   ; chmod 755  debian/tmp/usr/bin/dclcc-f77
	$(CC) -shared -Wl,-soname,libf77dcl.so.5 -o $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libf77dcl.so.5.4.0 `find $(CURDIR) -name \*.o` 
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libf77dcl.so.5.4.0 libf77dcl.so.5 )
	( cd $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) ; ln -s libf77dcl.so.5.4.0 libf77dcl.so )

override_dh_install:
	dh_install --sourcedir=debian/tmp

override_dh_auto_clean:
	[ ! -f Mkinclude ] || $(MAKE) distclean
	dh_auto_clean
	rm -f $(CURDIR)/lib/lib*.so
	rm -f $(CURDIR)/lib/lib*.so.*
	rm -f debian/libf77dcl5.install
	rm -f debian/libf77dcl-dev.install

override_dh_compress:
	dh_compress -X.png -X.f -X.dat

override_dh_installchengelogs:
	dh_installchangelogs --keep HISTORY

override_dh_installdocs:
	dh_installdocs -A README CREDITS

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=libf77dcl5-dbg
