#!/usr/bin/make -f

SHELL = /bin/sh

r = $(shell pwd)/debian/wml
t = $(shell pwd)/debian/tmp
p = wml

# Repackaging variables
DEBIAN_REVISION := $(shell echo -n `head -1 debian/changelog | sed -e 's/^.*(\(.*\)).*$$/\\1/'`)
DEBIAN_VERSION := $(shell echo -n $(DEBIAN_REVISION) | sed -e 's/-.*//')
UPSTREAM_VERSION := $(shell echo -n $(DEBIAN_VERSION) | sed -e 's/ds[0-9]$$//')
UPSTREAM_DIR := "$(t)/wml-$(UPSTREAM_VERSION)"
DEBIAN_DIR := "$(t)/wml-$(UPSTREAM_VERSION).orig"

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
unexport CFLAGS
unexport CPPFLAGS
unexport LDFLAGS

%:
	dh $@ --with autotools-dev

override_dh_auto_install:
	dh_auto_install                              -- prefix=$(r)/usr mandir=$(r)/usr/share/man INSTALLDIRS=perl
	dh_auto_install -Dwml_common/filepathconvert -- prefix=$(r)/usr mandir=$(r)/usr/share/man INSTALLDIRS=perl
	dh_auto_install -Dwml_common/gd              -- prefix=$(r)/usr mandir=$(r)/usr/share/man INSTALLDIRS=perl
	# Install icons for wml::std::logo
	dh_auto_install -Dwml_misc -- libdir=$(r)/usr/share/wml

	find $(r)/usr -type f -name .packlist -delete
	find $(r) -type d -empty -delete
	#   Fix documentation
	for f in $(r)/usr/share/man/*/*; do \
	   sed -e 's|.*/\([^/	]*\)	.*/\([^/	]*\)|s/\2/\1/|' debian/links |\
	   sed -f - $$f > $$f.new && mv $$f.new $$f; \
	done

override_dh_installdocs:
	dh_installdocs
	find $(r) '(' -name '*.spec' -o -name '*.spec.in' ')' -delete

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_perl:
	dh_perl usr/lib/wml/perl/lib

########################################################
### The remainder is just the get-orig-source target ###
########################################################
get-orig-source:
	# Prepare repackaging directory
	rm -rf $(t)
	mkdir -p $(t)
	# Unpack tar ball as downloaded by uscan(1)
	tar xvz -C $(t) -f ../wml_$(UPSTREAM_VERSION).orig.tar.gz
	# Rename directory
	mv -v $(UPSTREAM_DIR) $(DEBIAN_DIR)

	# We don't want upstream VCS stuff in our orig.tar.gz
	-find $(DEBIAN_DIR) -name .svn -exec rm -rvf '{}' ';'

	# Also remove stuff which should not belong in the upstream
	# distribution files either.
	find $(DEBIAN_DIR) -name '*~' -exec rm -rvf '{}' ';'
	find $(DEBIAN_DIR) -name '*.tmp' -exec rm -rvf '{}' ';'

	# Remove stuff which will be removed by make distclean anyway
	find $(DEBIAN_DIR) -mindepth 1 -name '*.orig' -exec rm -rvf '{}' ';'
	rm -rvf $(DEBIAN_DIR)/wml_backend/p2_mp4h/autom4te.cache

	# Remove non-free stuff
	rm -vf $(DEBIAN_DIR)/wml_docs/wml_std_*.txt
	rm -vf $(DEBIAN_DIR)/wml_docs/wml_barebone.txt

	# This was maybe not the brightest idea, if one wants to
	# switch to dh-autoreconf...
	find $(DEBIAN_DIR) -name 'configure.in' -exec rm -rvf '{}' ';'

	# Remove all embedded code copies not needed to build the
	# Debian package.
	rm -vf $(DEBIAN_DIR)/config/aclocal.m4
	rm -vf $(DEBIAN_DIR)/config/configure*
	rm -vf $(DEBIAN_DIR)/config/prep_modules_files.sh
	rm -rvf $(DEBIAN_DIR)/wml_aux/freetable
	rm -rvf $(DEBIAN_DIR)/wml_aux/txt2html
	rm -rvf $(DEBIAN_DIR)/wml_aux/htmlclean
	rm -rvf $(DEBIAN_DIR)/wml_aux/iselect
	rm -rvf $(DEBIAN_DIR)/wml_aux/linklint
	rm -rvf $(DEBIAN_DIR)/wml_aux/tidy
	rm -rvf $(DEBIAN_DIR)/wml_aux/weblint
	rm -rvf $(DEBIAN_DIR)/wml_backend/p2_mp4h
	rm -rvf $(DEBIAN_DIR)/wml_backend/p3_eperl
	rm -rvf $(DEBIAN_DIR)/wml_backend/p4_gm4
	rm -rvf $(DEBIAN_DIR)/wml_backend/p9_slice
	rm -rvf $(DEBIAN_DIR)/wml_common/bitvector
	rm -rvf $(DEBIAN_DIR)/wml_common/gd/libgd
	rm -rvf $(DEBIAN_DIR)/wml_common/getoptlong
	rm -rvf $(DEBIAN_DIR)/wml_common/imagesize
	rm -rvf $(DEBIAN_DIR)/wml_common/io
	rm -rvf $(DEBIAN_DIR)/wml_common/htmlclean
	rm -rvf $(DEBIAN_DIR)/wml_common/termreadkey
	# Do not remove wml_test for now.

	# Pack the Debian Source
	tar cv -C $(t) -f ../wml_$(DEBIAN_VERSION).orig.tar \
		wml-$(UPSTREAM_VERSION).orig
	gzip -9vf ../wml_$(DEBIAN_VERSION).orig.tar

	# Clean up again
	rm -rf $(t)
