#!/usr/bin/make -f

DEBPUTY_INSTALLED_ROOT_DIR=/usr/share/dh-debputy
DEBPUTY_INSTALLED_PLUGIN_ROOT_DIR=/usr/share/debputy/

subs = -e 's|^$1 = .*|$1 = $2|'

# Nothing to do by default
all:

test:
	py.test -v

install:
	install -m0755 -Dt$(DESTDIR)/usr/bin dh_debputy dh_installdebputy assets/debputy
	install -m0755 -Dt$(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR) deb_packer.py deb_materialization.py
	install -m0644 -Dt$(DESTDIR)/usr/share/perl5/Debian/Debhelper/Sequence lib/Debian/Debhelper/Sequence/*.pm
	install -m0644 -Dt$(DESTDIR)/usr/share/perl5/Dpkg/BuildDriver lib/Dpkg/BuildDriver/*.pm
	cp -a --reflink=auto src/debputy $(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR)
	install -m0755 -d $(DESTDIR)/$(DEBPUTY_INSTALLED_PLUGIN_ROOT_DIR)
	cp -a --reflink=auto debputy $(DESTDIR)/$(DEBPUTY_INSTALLED_PLUGIN_ROOT_DIR)
	sed -i \
	   $(call subs,_VERSION,"$(shell dpkg-parsechangelog -SVersion)") \
	   $(call subs,DEBPUTY_ROOT_DIR,pathlib.Path("$(DEBPUTY_INSTALLED_ROOT_DIR)")) \
	   $(call subs,DEBPUTY_PLUGIN_ROOT_DIR,pathlib.Path("$(DEBPUTY_INSTALLED_PLUGIN_ROOT_DIR)")) \
	   $(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR)/deb_materialization.py \
	   $(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR)/deb_packer.py \
	   $(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR)/debputy/version.py
	find $(DESTDIR)/usr/share/dh-debputy -type d -name '__pycache__' -exec rm -fr {} +
	install -m0755 -d $(DESTDIR)/usr/share/man/man1
	pod2man --utf8 --section=1 --name="debputy" -c "The debputy Debian packager helper stack" debputy.pod \
	   $(DESTDIR)/usr/share/man/man1/debputy.1
	pod2man --utf8 --section=1 --name="dh_debputy" -c "The debputy Debian packager helper stack" dh_debputy \
	   $(DESTDIR)/usr/share/man/man1/dh_debputy.1
	pod2man --utf8 --section=1 --name="dh_installdebputy" -c "The debputy Debian packager helper stack" dh_installdebputy \
	   $(DESTDIR)/usr/share/man/man1/dh_installdebputy.1
	devutils/install-translations
	chmod -R u=rwX,go=rX \
	    $(DESTDIR)/$(DEBPUTY_INSTALLED_ROOT_DIR)/debputy \
	    $(DESTDIR)/$(DEBPUTY_INSTALLED_PLUGIN_ROOT_DIR)/debputy
