#!/usr/bin/make -f

ifneq (, $(findstring noopt, $(DEB_BUILD_OPTIONS)))
  EXTRA_CFLAGS = -O0
else
  EXTRA_CFLAGS =
endif

ifneq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
  DOCHECK =
else
  DOCHECK = 1
endif

#ifneq (, $(filter arm m68k, $(DEB_HOST_ARCH)))
#  GCC = gcc-3.3
#else
#  GCC = gcc
#endif
GCC = gcc


INSTDIRARGS = SCRIPTDIR=/usr/share/doc/apcalc/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) CCMISC="$(EXTRA_CFLAGS)" LCC=$(GCC)
ifdef DOCHECK
	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/apcalc-dev-build
	mv libcalc.a debian/apcalc-dev-build
	mv custom/libcustcalc.a debian/apcalc-dev-build
	-make clobber
	
	# Now the build of the executable. This one uses readline.
	make $(INSTDIRARGS) CCMISC="$(EXTRA_CFLAGS)" LCC=$(GCC) \
	    USE_READLINE=-DUSE_READLINE READLINE_LIB=-lreadline
ifdef DOCHECK
	make chk
endif

	touch build-stamp

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

	-make clobber
	# Shipped with the original source, but modified during build. Remove to avoid
	# spurious diffs, they get regenerated when required.
	rm -f custom/Makefile custom/Makefile.simple

	dh_clean
	-rm -fr debian/copyright debian/apcalc-dev-build

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

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

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

	cat debian/copyright.head COPYING > debian/copyright

	touch install-stamp

.PHONY: binary-indep
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -papcalc-common BUGS README
	dh_installchangelogs -i CHANGES
	dh_installmenu -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 -papcalc-dev LIBRARY
	dh_installchangelogs -papcalc-dev CHANGES
	dh_lintian -a
	dh_link -papcalc usr/share/doc/apcalc-common usr/share/doc/apcalc
	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)
