#!/usr/bin/make -f

PERL ?= /usr/bin/perl

VER := $(shell head -1 debian/changelog | sed -e 's/^.*(//' -e 's/).*$$//')
tmp := $(CURDIR)/debian/lintian
profiles := profiles/debian/main.profile \
	    profiles/debian/ftp-master-auto-reject.profile
neededfiles := debian/rules frontend/lintian $(profiles)
docsource := doc/lintian.xml README man/lintian.pod.in \
	     man/lintian-info.pod
allchecks := $(wildcard checks/*)
allcollect := $(wildcard collection/*)
tagfiles := $(wildcard t/changes/*.changes t/*/*/tags)
testfiles := $(wildcard t/tests/*/*.desc)
perlprovides := data/fields/perl-provides
autoreject_data := $(wildcard private/build-time-data/*)
onlyrun =
tag =

# Work/output dir for tests
# private/runtests gets TEST_WORK_DIR from environment
export TEST_WORK_DIR ?= $(CURDIR)/debian/test-out

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	testtarget = runtests
else
	testtarget =
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	jobs = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	PAR_ARGS=-j $(jobs)
endif

profiles: $(profiles)
$(profiles): $(allchecks) $(autoreject_data) private/generate-profiles.pl
	private/generate-profiles.pl

api-doc:
	private/generate-html-docs doc/api.html

runtests: $(neededfiles) $(allchecks) $(allcollect) $(tagfiles) $(testfiles)
	@echo .... running tests ....
	mkdir -p "$(TEST_WORK_DIR)"
	t/runtests --dump-logs -k $(PAR_ARGS) t "$(TEST_WORK_DIR)" $(onlyrun)
	if [ "$(onlyrun)" = "" ]; then touch $@; fi

# this target is only run manually
refresh-perl-provides:
	perl private/refresh-perl-provides > $(perlprovides)

build-arch build-indep build: build-stamp
build-stamp: $(neededfiles) $(docsource) $(testtarget)
	@echo .... running build ....
	dh_testdir
	cd doc && LC_ALL=C docbook2html  -V "%use-id-as-filename%" -o lintian.html lintian.xml
	cd doc && LC_ALL=C jw -b txt lintian.xml
	mkdir man/man1/
	private/generate-lintian-pod | \
		pod2man --name lintian --center "Debian Package Checker" --section=1 > man/man1/lintian.1
	pod2man --section=1 man/lintian-info.pod > man/man1/lintian-info.1
	private/generate-html-docs doc/api.html > /dev/null

	# check that the static data about perl core modules is up to date
	$(PERL) -ne '/PERL_VERSION=(.+)/ and $$] > $$1 and warn q{*}x60 . qq{\n$(perlprovides) needs an update, please run\n  debian/rules refresh-perl-provides\n} . q{*}x60 . qq{\n}' $(perlprovides)

	touch $@

clean: $(neededfiles)
	@echo .... cleaning ....
	dh_testdir
	dh_testroot
	rm -f runtests build-stamp
	rm -rf doc/lintian.html/ doc/lintian.txt
	rm -rf doc/api.html
	rm -rf "$(TEST_WORK_DIR)"
	rm -rf man/man1/
	dh_clean

binary-indep: $(neededfiles) build
	@echo .... binary-indep ....
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	dh_install
# some manual work
	perl -p -i -e 's/my \$$LINTIAN_VERSION;/my \$$LINTIAN_VERSION = q{$(VER)};/;' \
	     -e 's@my \$$INIT_ROOT(\s*=.*)?;@my \$$INIT_ROOT = q{/usr/share/lintian};@;' \
	    $(tmp)/usr/bin/lintian $(tmp)/usr/share/lintian/frontend/dplint
	install -m 644 doc/lintianrc.example $(tmp)/etc/lintianrc
	dh_installdocs
	dh_installchangelogs
	dh_installexamples
	dh_installman
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch:

binary:	binary-indep binary-arch

.PHONY: build binary binary-arch binary-indep clean refresh-perl-provides
.DELETE_ON_ERROR: runtests
