#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGS = -Wall -fPIC
CFLAGS += $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS += $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

version=$(shell dpkg-parsechangelog | sed -e '/^Version/!d;s/^Version: //g;s/.*://g;s/-.*//g')
major=$(shell dpkg-parsechangelog | sed -e '/^Version/!d;s/^Version: //g;s/\..*//g')

package=libcfitsio$(major)
dev-package=libcfitsio$(major)-dev
doc-package=libcfitsio$(major)-doc

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp:
	dh_testdir

	CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CPPFLAGS) $(CFLAGS)" LD_FLAGS="$(LDFLAGS)" ./configure \
		--prefix=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--enable-reentrant
	$(MAKE)
	cc $(LDFLAGS) -shared -D_REENTRANT \
		-Wl,-zdefs -Wl,-soname,libcfitsio.so.$(major) \
		-o libcfitsio.so.$(version) `echo *.o` -lm -lpthread -lz

	touch $@

build-indep: build-indep-stamp
build-indep-stamp:
	dh_testdir

	# cfitsio.tex
	mkdir -p $(CURDIR)/cfitsio
	hevea -fix -o $(CURDIR)/cfitsio/cfitsio cfitsio.tex
	cd $(CURDIR)/cfitsio && hacha cfitsio && rm -f cfitsio *.haux *.htoc *.hind

	# fitsio
	mkdir -p $(CURDIR)/fitsio
	hevea -fix -o $(CURDIR)/fitsio/fitsio fitsio.tex
	cd $(CURDIR)/fitsio && hacha fitsio && rm -f fitsio *.haux *.htoc *.hind

	# quik
	mkdir -p $(CURDIR)/quick
	hevea -fix -o $(CURDIR)/quick/quick quick.tex
	cd $(CURDIR)/quick && hacha quick && rm -f quick *.haux *.htoc *.hind

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f libcfitsio.*
	rm -rf cfitsio fitsio quick

	dh_clean

install: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	# Main install
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp CFITSIO_LIB=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	
	install libcfitsio.so.* $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	ln -s libcfitsio.so.$(version) $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcfitsio.so.$(major)
	ln -s libcfitsio.so.$(major) $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcfitsio.so

	dh_install --list-missing --sourcedir=debian/tmp

# Build architecture-independent files here.
binary-indep: build install 
	dh_testdir
	dh_testroot
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i -- -Zxz

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a changes.txt
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_link -a
	dh_strip -a --dbg-package=libcfitsio3-dbg
	dh_compress -a
	dh_fixperms -a 
	dh_makeshlibs
	dh_installdeb -a
	dh_shlibdeps
	dh_gencontrol -a 
	dh_md5sums -a 
	dh_builddeb -a -- -Zxz

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