#!/usr/bin/make -f

include /usr/share/dpkg/buildtools.mk
include /usr/share/dpkg/buildflags.mk


INSTDIRARGS = SCRIPTDIR=/usr/share/doc/calc/examples


.PHONY: build
build: build-arch build-indep

.PHONY: build-arch
build-arch: build-stamp

.PHONY: build-indep
build-indep: build-stamp

build-stamp:
	dh_testdir

	# We always have to clean up first since we don't know which options were used
	# when building last time.
	-make clobber

	# First the library build. We build this without readline support to avoid
	# dependencies on libreadline in libcalc.
	make $(INSTDIRARGS) EXTRA_CFLAGS="$(CFLAGS) $(CPPFLAGS)" EXTRA_LDFLAGS="$(LDFLAGS)" LCC=$(CC)
ifeq (, $(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make chk
endif

	# Clean up since we want to rebuild with different options below. But move the libraries
	# built above to a safe place first.
	mkdir -p debian/calc-dev-build
	mv libcalc.a debian/calc-dev-build
	mv custom/libcustcalc.a debian/calc-dev-build
	-make clobber
	
	# Now the build of the executable. This one uses readline.
	make $(INSTDIRARGS) EXTRA_CFLAGS="$(CFLAGS) $(CPPFLAGS)" EXTRA_LDFLAGS="$(LDFLAGS)" LCC=$(CC) \
	    USE_READLINE=-DUSE_READLINE READLINE_LIB=-lreadline
ifeq (, $(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make chk
endif

	touch build-stamp

.PHONY: clean
clean:
	dh_testdir
	dh_testroot
	-rm -f build-stamp install-stamp

	-make clobber

	dh_clean
	-rm -fr debian/calc-dev-build

.PHONY: install
install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs

	make install $(INSTDIRARGS) T=$(shell pwd)/debian/calc

	# calc-dev
	mv debian/calc/usr/include/calc/*.h debian/calc-dev/usr/include/calc
	rmdir debian/calc/usr/include/calc/custom debian/calc/usr/include/calc debian/calc/usr/include
	cp debian/calc-dev-build/lib*calc.a debian/calc-dev/usr/lib
	cp sample_*.c debian/calc-dev/usr/share/doc/calc-dev/examples
	cp sample.README debian/calc-dev/usr/share/doc/calc-dev/examples/README
	# calc-common
	mv debian/calc/usr/share debian/calc-common/usr/share
	cd debian/calc-common/usr/share/doc && mv calc calc-common
	cd debian/calc-common/usr/share && mv calc/README doc/calc-common/README.calclib
	# calc
	rm debian/calc/usr/bin/calc-static debian/calc/usr/lib/libcalc.a debian/calc/usr/lib/libcustcalc.a
	rmdir debian/calc/usr/lib

	touch install-stamp

.PHONY: binary-indep
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -pcalc-common BUGS README.md
	dh_installchangelogs -pcalc-common CHANGES
	dh_install -i
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

.PHONY: binary-arch
binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -pcalc-dev LIBRARY
	dh_installchangelogs -pcalc-dev CHANGES
	dh_link -pcalc usr/share/doc/calc-common usr/share/doc/calc
	dh_lintian -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

.PHONY: binary
binary: binary-indep binary-arch

.PHONY: get-orig-source
get-orig-source:
	cd $(dir $(firstword $(MAKEFILE_LIST)))/.. && \
	    uscan --verbose --force-download --rename --destdir $(CURDIR)
