#!/usr/bin/make -f

#
# debian/rules for vile/xvile
#

SHELL = /bin/bash
DPKG_EXPORT_BUILDFLAGS = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/buildflags.mk

configure_flags = \
    --with-locale \
    --with-perl \
    --with-loadable-filters=all \
    --disable-rpath-hack \
    --disable-stripping

xshell = x-terminal-emulator -e /usr/share/vile/xshell.sh

# install path for upstream make
TMP = $(CURDIR)/debian/tmp

build: build-arch build-indep
build-arch build-indep: build-stamp
build-stamp:
	dh_testdir
	rm -rf t

	# Update config.guess, config.sub
	dh_update_autotools_config

	# Configure vile
	dh_auto_configure --builddirectory=t/vile -- $(configure_flags)

	# Build vile, vile-common, vile-filters
	$(MAKE) -C t/vile all vile-perl-api.doc
	mkdir -p t/vile/perl
	(cd perl; find * -type f -name '*.p[lm]' -print | \
	    xargs grep -l =head1 | \
	    while read file; do \
		out=$$(echo $$file | sed 's#/#::#g; s/\.p.$$/.doc/'); \
		pod2text --stderr $$file ../t/vile/perl/$$out; \
	    done)

	# Configure xvile
	dh_auto_configure --builddirectory=t/xvile -- $(configure_flags) \
	    --with-xpm --with-screen=Xaw \
	    --with-app-defaults=/etc/X11/app-defaults \
	    --with-pixmapdir='$$(prefix)/share/pixmaps' \
	    --with-icondir='$$(prefix)/share/icons' \
	    --with-icon-theme=hicolor
	echo '#define DEFAULT_XSHELL "$(xshell)"' >> t/xvile/config.h

	# Build xvile
	$(MAKE) -C t/xvile xvile

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp
	dh_clean
	rm -rf t

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Install vile, vile-filters, vile-common
	$(MAKE) -C t/vile DESTDIR=$(TMP) install
	ls -l $(TMP)/usr/bin/vile-pager $(TMP)/usr/bin/vile-libdir-path \
	    $(TMP)/usr/bin/vile-to-html  # examples
	pod2man $(TMP)/usr/bin/vileget > \
	    $(TMP)/usr/share/man/man1/vileget.1

	install -d $(TMP)/usr/share/lintian/overrides
	install -c -m 644 debian/overrides/vile \
	    $(TMP)/usr/share/lintian/overrides

	# Install xvile
	install -d $(TMP)/usr/bin $(TMP)/etc/X11/app-defaults \
            $(TMP)/usr/share/applications $(TMP)/usr/share/pixmaps \
	    $(TMP)/usr/share/vile

	install -c t/xvile/xvile macros/lxvile macros/uxvile $(TMP)/usr/bin

	$(MAKE) -C t/xvile DESTDIR=$(TMP) install-app install-icon
	$(MAKE) -C t/xvile DESTDIR=$(TMP) bindir='$$(datadir)' \
	    $(TMP)/usr/share/vile/vilemenu.rc \
	    $(TMP)/usr/share/vile/xshell.sh

	install -c -m 644 debian/vile.mini.xpm $(TMP)/usr/share/pixmaps

	(cd macros && for P in *.desktop; \
	 do \
	     install -c -m 644 $$P $(TMP)/usr/share/applications/dickey-$$P; \
	 done)

	dh_movefiles
	touch $@

# Build architecture-independent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_link              -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

# Build architecture-dependent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs       -i
	dh_installexamples   -i
	dh_installchangelogs -i -k CHANGES

	# Cleanup permissions and #! lines.
	chmod +x debian/vile-common/usr/share/doc/vile-common/examples/*
	sed -i '1s%#!/usr/bin/env perl%#!/usr/bin/perl%' \
	    debian/xvile/usr/bin/lxvile \
	    debian/vile-common/usr/bin/vileget \
	    debian/vile-common/usr/share/doc/vile-common/examples/*

	dh_compress          -i
	dh_fixperms          -i
	dh_installdeb        -i
	dh_gencontrol        -i
	dh_md5sums           -i
	dh_builddeb          -i

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