#!/usr/bin/make -f

# The following is a list of the non-free files that Wordpress
# ships in their archive that we will strip in get-orig-source.
UPSTREAM_FILE_BLACKLIST=wp-content/plugins/hello.php

WP_CONTENT = debian/wordpress/var/lib/wordpress/wp-content

%:
	dh $@ --with linktree

override_dh_auto_build:
	for i in debian/languages/*.po; do \
		msgfmt $$i -o $${i%%.po}.mo; \
        done

override_dh_install:
	dh_install -Xlicense.txt -XMIT-LICENSE

override_dh_fixperms:
	dh_fixperms
	chown www-data:www-data $(WP_CONTENT)/blogs.dir $(WP_CONTENT)/uploads

WP_CONTENT_TARGET = /usr/share/wordpress/wp-content
WP_CONTENT_ORIG = debian/wordpress$(WP_CONTENT_TARGET)
override_dh_link: 
	dh_link
	# Populate /var/lib/wordpress/wp-content with default themes and plugins
	for file in $(WP_CONTENT_ORIG)/themes/* $(WP_CONTENT_ORIG)/plugins/*; do \
	    file="$${file##$(WP_CONTENT_ORIG)/}"; \
	    ln -sf $(WP_CONTENT_TARGET)/$$file $(WP_CONTENT)/$$file; \
	done

# Use like this: debian/rules get-orig-source VERSION=3.4
get-orig-source:
	@test -n "$(VERSION)" || (echo "Please set VERSION on the command-line. Ex: debian/rules get-orig-source VERSION=3.4" >&2; exit 1)
	@test ! -e ../wordpress_$(VERSION)+dfsg.orig.tar.xz || (echo "You already have ../wordpress_$(VERSION)+dfsg.orig.tar.xz" >&2; exit 1)
	uscan --noconf --force-download --rename --download-version $(VERSION) --destdir=.
	tar --transform "s,^.[^/]*,wordpress-$(VERSION)/," -xzvf \
		./wordpress_$(VERSION).orig.tar.gz
	rm ./wordpress_$(VERSION).orig.tar.gz
	cd wordpress-$(VERSION) && rm $(UPSTREAM_FILE_BLACKLIST)
	tar -acvf ../wordpress_$(VERSION)+dfsg.orig.tar.xz wordpress-$(VERSION)
	rm -rf wordpress-$(VERSION)
	@echo "Successfully created ../wordpress_$(VERSION)+dfsg.orig.tar.xz"
