#!/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 doc/README.in man/lintian.pod.in \
	     man/lintian-info.pod
allchecks := $(wildcard checks/*)
allcollect := $(wildcard collection/*)
tagfiles := $(wildcard testset/tags.* t/*/*/tags)
testfiles := $(wildcard t/tests/*/*.desc)
perlprovides := data/fields/perl-provides
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) private/generate-profiles.pl
	private/generate-profiles.pl

runtests: $(neededfiles) $(allchecks) $(allcollect) $(tagfiles) $(testfiles)
	@echo .... running tests ....
	rm -rf "$(TEST_WORK_DIR)"
	mkdir "$(TEST_WORK_DIR)"
	private/runtests $(PAR_ARGS) $(onlyrun)
	if [ "$(onlyrun)" = "" ]; then touch $@; fi

# Like runtests but only runs tests affecting a particular tag.  That tag
# should be specified by setting the tag makefile variable.
check-tag:
	@if [ "$(tag)" = "" ]; then \
		echo 'Specify tag to test with tag=<tag>'; exit 1; \
	fi
	rm -rf "$(TEST_WORK_DIR)"
	mkdir "$(TEST_WORK_DIR)"
	private/runtests $(PAR_ARGS) -t $(tag)

# 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
	LINTIAN_ROOT="" ./frontend/lintian --help \
		| tail -n +3 | $(PERL) -n -e 'print "  $$_"' >doc/help.tmp
	$(PERL) -p -e 'BEGIN { open HELP, "<", "doc/help.tmp" or die; local $$/ = undef; $$h = <HELP> }; s/%LINTIAN_HELP%/$$h/' doc/README.in >doc/README

	# 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(which needs the libapt-pkg-perl package)\n} . q{*}x60 . qq{\n}' $(perlprovides)

	touch $@

clean: $(neededfiles)
	@echo .... cleaning ....
	dh_testdir
	dh_testroot
	rm -f runtests build-stamp
	rm -f doc/help.tmp doc/README
	rm -rf doc/lintian.html/ doc/lintian.txt
	rm -rf "$(TEST_WORK_DIR)"
	rm -rf man/man1/
	rm -fr debian/test.locale
	dh_clean

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

	dh_install
# some manual work
	sed -i 's/my $$LINTIAN_VERSION = "<VERSION>"/my $$LINTIAN_VERSION = "$(VER)"/' $(tmp)/usr/bin/lintian
	install -m 644 doc/lintianrc.example $(tmp)/etc/lintianrc
	dh_installdocs
	dh_installchangelogs
	dh_installman
	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
