#!/usr/bin/make -f
# This debian/rules file was written for the WebGUI package
# by Ernesto Hernández-Novich <emhn@usb.ve> taking ideas
# from the debian/rules templates included with dh-make-perl

# Uncomment this to turn on verbose mode
#export DH_VERBOSE=1

PACKAGE = $(shell dh_listpackages)

ifndef PERL
PERL = /usr/bin/perl
endif

TMP = $(CURDIR)/debian/$(PACKAGE)

build: build-stamp
build-stamp:
	dh_testdir
	# Build manpages for spectre and the utility scripts, out
	# of the POD inside them.
	for i in sbin/*.pl; do \
	    pod2man $$i wg-`basename $$i .pl`.1 ; \
	done;
	mv wg-spectre.1 spectre.1
	# docbook-to-man debian/webgui.sgml > webgui.1
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	dh_clean build-stamp install-stamp
	# Add here commands to clean up after the build process.
	rm -f *.1

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install --exclude=license.txt \
	           --exclude=LICENSE.txt \
	           --exclude=yui/        \
	           --exclude=tinymce/    \
	           --exclude=example     \
	           --exclude=.svn        \
	           --exclude=Thumbs.db   \
	           --exclude=.DS_Store
	# Drop the .pl extension from all the scripts in /usr/bin
	prename 's{\.pl\z}{}' $(TMP)/usr/bin/*.pl
	# Move spectre to /usr/sbin, since it's a daemon
	mv $(TMP)/usr/bin/spectre $(TMP)/usr/sbin
	# Rename all the scripts in /usr/bin to have wg- as prefix
	prename 's{/([^/]+)\z}{/wg-$$1}' $(TMP)/usr/bin/*
	# Fix permissions in /usr/share/webgui
	find $(TMP)/usr/share/webgui/lib      -type f      -exec chmod 644 {} \;
	find $(TMP)/usr/share/webgui/upgrades -type f      -exec chmod 644 {} \;
	find $(TMP)/usr/share/webgui/upgrades -name '*.pl' -exec chmod 755 {} \;
	chmod 755 $(TMP)/usr/share/webgui/upgrades/_upgrade.skeleton
	# Fix permissions in /var/lib/webgui (for uploads)
	find $(TMP)/var/lib/webgui           -type f -exec chmod 644 {} \;
	# Fix permissions in /usr/share/webgui/extras (JS libraries)
	find $(TMP)/usr/share/webgui/extras  -type f -exec chmod 644 {} \;
	# Fix permissions in /usr/share/webgui/t      (tests)
	find $(TMP)/usr/share/webgui/t       -type f -exec chmod 644 {} \;
	touch install-stamp

binary-arch:
# We have nothing to do here for an architecture-independent package

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs     --exclude=upgrades      \
				       --exclude=license.txt   \
	                   --exclude=.css          \
	                   --exclude=.js           \
				       --exclude=.svn          \
	                   --exclude=maintenance.html
	dh_installexamples --exclude=.css          \
	                   --exclude=.js           \
				       --exclude=.svn
	dh_installinit
	dh_installlogrotate
	dh_installman
	dh_lintian
	dh_perl
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
