#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

manpage = debian/davs2.1
executable = debian/tmp/usr/bin/davs2

%:
	dh $@ --sourcedirectory=build/linux

override_dh_auto_configure:
	VER_SHA="$(DEB_DISTRIBUTION)" dh_auto_configure -- \
		--enable-shared \
		--enable-pic \
		--extra-cflags="${CPPFLAGS} -fvisibility=hidden -DDAVS2_EXPORTS"

# generate manpage based on --help option of script itself
override_dh_auto_install:
	dh_auto_install
	help2man \
		--name="AVS2 (IEEE 1857.4) decoder" \
		--no-info \
		--help-option=foobar \
		--no-discard-stderr \
		--version-string=$(DEB_VERSION_UPSTREAM) \
		--output=$(manpage) \
		$(executable) \
		|| { $(executable) --help; false; }
	perl -i -0 -p \
		-e 's/^\.SH DESCRIPTION\n.*davs2 foobar\n\.PP\ndavs2 parameters\n//m;' \
		-e 's/^\.IP\nusage: +davs2 +(.+)/.SH SYNOPSIS\n.B davs2\n$$1\n.SH OPTIONS/mg;' \
		-e 's/^\+\S+\+\n\|.*\n\+\S+\+\n//m;' \
		-e 's/^\+.*//ms;' \
		-e 's/^\| *(\S+) *\| *(\S+(?: \w\S*)?) *\| *(\S+(?: +\S+)*) *\|/.TP\n$$1 $$2\n$$3/mg;' \
		-e 's/ \\fB(\\-\\-(?:in|out)put)\\fR/ $$1/g;' \
		-e 's/ (avs2file|outputfile|recfile|threads|test\.avs|dec\.yuv|rec\.yuv|[NM]\b)/ \\fI$$1\\fR/g;' \
		-e 's/\[(\\-[a-z]\b)/[\\fB$$1\\fR/g;' \
		-e 's/\\fR([.a-z]+)/$$1\\fR/g;' \
		$(manpage)

override_dh_clean:
	dh_clean
	rm -f $(manpage)

# avoid triggering configure during cleanup
override_dh_auto_clean:
	[ ! -f build/linux/config.mak ] || dh_auto_clean
