#!/usr/bin/make -f
# -*- makefile -*-

# export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS
TESTOPTS += -v
export TESTOPTS

DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

configure_options += --enable-multiarch
configure_options += --target=$(DEB_HOST_MULTIARCH)
configure_options += --program-suffix=2.0
configure_options += --with-soname=ruby-2.0
configure_options += --enable-shared
configure_options += --disable-rpath
configure_options += --with-baseruby=/usr/bin/ruby
configure_options += --with-sitedir='/usr/local/lib/site_ruby'
configure_options += --with-sitearchdir="/usr/local/lib/$(DEB_HOST_MULTIARCH)/site_ruby"

# the following are ignored by ./configure, but used by some extconf.rb scripts
configure_options += --enable-ipv6
configure_options += --with-dbm-type=gdbm_compat

# hardening and other standard Debian build flags
configure_options += $(shell dpkg-buildflags --export=configure)

# handle tcl/tk with multiarch support; fallback to non-multiarch locations
#configure_options += --with-tclConfig-dir=$(shell find /usr/lib /usr/lib/$(DEB_HOST_MULTIARCH) -maxdepth 1 -name tclConfig.sh | tail -n 1 | xargs dirname)
#configure_options += --with-tkConfig-dir=$(shell find /usr/lib /usr/lib/$(DEB_HOST_MULTIARCH) -maxdepth 1 -name tkConfig.sh | tail -n 1 | xargs dirname)
vtcltk=8.5
#configure_options += --with-tk-old-extconf
configure_options += --with-tcltkversion=$(vtcltk)
configure_options += --with-tclConfig-dir=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(vtcltk)/tclConfig.sh
configure_options += --with-tkConfig-dir=/usr/lib/$(DEB_HOST_MULTIARCH)/tk$(vtcltk)/tkConfig.sh
configure_options += --with-tcllib=tcl$(vtcltk)
configure_options += --with-tklib=tk$(vtcltk)
configure_options += --with-tcl-include=/usr/include/tcl$(vtcltk)
configure_options += --with-tk-include=/usr/include/tcl$(vtcltk)
configure_options += --with-tcl-lib=/usr/lib/$(DEB_HOST_MULTIARCH)
configure_options += --with-tk-lib=/usr/lib/$(DEB_HOST_MULTIARCH)
export DEB_LDFLAGS_MAINT_APPEND = -L$(CURDIR)/debian/lib

%:
	dh $@ --parallel --with autotools-dev --with autoreconf

override_dh_auto_configure:
	mkdir -p debian/lib
	ln -sf /usr/lib/$(DEB_HOST_MULTIARCH)/libtcl$(vtcltk).so debian/lib/libtcl.so
	ln -sf /usr/lib/$(DEB_HOST_MULTIARCH)/libtk$(vtcltk).so debian/lib/libtk.so
	dh_auto_configure -- $(configure_options)

override_dh_auto_clean:
	dh_auto_clean
	rm -rf debian/lib
	$(RM) -r .ext
	$(RM) -r doc/capi

override_dh_auto_build-arch:
	dh_auto_build -- main V=1

# This enables the full test suite
#override_dh_auto_test-arch:
#ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	$(MAKE) check V=1
#endif

override_dh_auto_install-arch:
	$(MAKE) install-nodoc DESTDIR=$(CURDIR)/debian/tmp
	# remove RPATH from tcltklib bindings
	chrpath --delete $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/ruby/2.0.0/tcltklib.so
	# handle embedded copy of jquery
	$(RM) $(CURDIR)/debian/tmp/usr/lib/ruby/2.0.0/rdoc/generator/template/darkfish/js/jquery.js
	dh_link -plibruby2.0 /usr/share/javascript/jquery/jquery.min.js /usr/lib/ruby/2.0.0/rdoc/generator/template/darkfish/js/jquery.js
	# sanity check
	debian/sanity_check

override_dh_auto_build-indep:
	$(MAKE) docs V=1

override_dh_auto_install-indep:
	$(MAKE) install-doc DESTDIR=$(CURDIR)/debian/ruby2.0-doc

override_dh_install-arch:
	dh_install
	# split Ruby/Tk files
	ruby debian/split-tk-out.rb
