#!/usr/bin/make -f

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build: configure-stamp

install: configure-stamp
	dh_testdir
	dh_testroot
	dh_installdirs

	# Main Mahara files
	cp -r $(CURDIR)/htdocs/* $(CURDIR)/debian/mahara/usr/share/mahara

	# Exclude common files
	find $(CURDIR)/debian/mahara/usr/share/mahara/ -name "readme.txt" -exec rm {} \;
	find $(CURDIR)/debian/mahara/usr/share/mahara/ -name "license.txt" -exec rm {} \;
	find $(CURDIR)/debian/mahara/usr/share/mahara/ -name "COPYING*" -exec rm {} \;
	find $(CURDIR)/debian/mahara/usr/share/mahara/ -name "README" -exec rm {} \;
	find $(CURDIR)/debian/mahara/usr/share/mahara/ -name "LICENSE*" -exec rm {} \;
	find $(CURDIR)/debian/mahara/usr/share/mahara/theme -name "captcha.ttf" -exec rm {} \;

	# Exclude specific files/directories
	rm -f $(CURDIR)/debian/mahara/usr/share/mahara/robots.txt
	rm -f $(CURDIR)/debian/mahara/usr/share/mahara/lib/phpmailer/ChangeLog.txt
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/adodb/docs/
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/adodb/tests/
	rm -f $(CURDIR)/debian/mahara/usr/share/mahara/lib/adodb/pear/readme.Auth.txt
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/pear/File
	rm -f $(CURDIR)/debian/mahara/usr/share/mahara/lib/pear/File.php
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/pear/PEAR
	rm -f $(CURDIR)/debian/mahara/usr/share/mahara/lib/pear/PEAR.php
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/dwoo/dwoo
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/lib/htmlpurifier
	rm -rf $(CURDIR)/debian/mahara/usr/share/mahara/js/tinymce

	# Fix broken permissions
	chmod -x $(CURDIR)/debian/mahara/usr/share/mahara/lib/pieforms/pieform/*/*.php

	# Move mediaplayer block into the contrib package
	mv $(CURDIR)/debian/mahara/usr/share/mahara/lib/flowplayer $(CURDIR)/debian/mahara-mediaplayer/usr/share/mahara/lib/
	mv $(CURDIR)/debian/mahara/usr/share/mahara/lib/flowplayer.audio $(CURDIR)/debian/mahara-mediaplayer/usr/share/mahara/lib/
	mv $(CURDIR)/debian/mahara/usr/share/mahara/artefact/file/blocktype/internalmedia $(CURDIR)/debian/mahara-mediaplayer/usr/share/mahara/artefact/file/blocktype/

	# Config files (common package)
	cp $(CURDIR)/debian/config.php $(CURDIR)/debian/mahara/etc/mahara/
	cp $(CURDIR)/htdocs/robots.txt $(CURDIR)/debian/mahara/etc/mahara/

	# Config file (-apache2)
	cp $(CURDIR)/htdocs/.htaccess $(CURDIR)/debian/mahara-apache2/usr/share/mahara
	cp $(CURDIR)/debian/apache.conf $(CURDIR)/debian/mahara-apache2/etc/mahara/apache.conf

clean:
	dh_testdir
	dh_testroot
	rm -f configure-stamp
	dh_clean 
	debconf-updatepo

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installdebconf
	dh_installcron
	dh_installlogrotate
	dh_compress 
	dh_fixperms
	dh_lintian
	dh_installdeb
	dh_link
	dh_gencontrol
	dh_md5sums
	dh_builddeb -- -Z bzip2

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