#!/usr/bin/make -f
#  $Id$
#
#  debian/rules file for phpbb3 Debian package
#
#  Written from scratch, with inspiration from apache's debian/rules, and the
#  examples from dh_make
#  Copyright 2002, 2004 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
#
#  Complete overhaul for debhelper 7 and dpkg source format 3 (quilt)
#  Copyright 2010 J.M. Roth <jmroth@iip.lu>
#
#  Distributed under the GNU General Public License version 2

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

# Bash is easier
export SHELL = /bin/bash

VERSION := $(shell head -1 debian/changelog | sed 's/.*(//;s/).*//')
UPSTREAM   := $(shell head -1 debian/changelog | sed 's/.*(//;s/-.?*).*//')
# MAINTAINER := $(shell grep ^Maintainer: debian/control | sed 's/^Maintainer: //')

get-orig-source:
	debian/get-orig-source ${UPSTREAM} _all
	@# mv *.orig*.tar* ..

override_dh_auto_clean:
	rm -Rf debian/tmp
	debconf-updatepo -v

override_dh_install:
	@# include all language packs that are present
	@# no need to update phpbb3-l10n.install each time
	mkdir -p debian/tmp/l10n
	set -e ; for lang in l10n-lang-* ; do \
		dir=$$(echo $$lang | sed -e 's/l10n-lang-//' -e 's/-/_/g') ; \
		cp -r $$lang debian/tmp/l10n/$$dir/ ; \
	done #>> $@.log
	dh_install

override_dh_installchangelogs:
	dh_installchangelogs docs/CHANGELOG.html -pphpbb3
	dh_installchangelogs -pphpbb3-l10n

override_dh_installdeb:
	@# customize the stuff in the build directory
	rm debian/phpbb3/usr/share/phpbb3/install/schemas/index.htm
	cd debian/phpbb3/usr/share/phpbb3/docs && rm AUTHORS COPYING INSTALL.html
	sed -i "s/@VERSION@/$(VERSION) (Debian)/" debian/phpbb3/usr/share/phpbb3/www/common.php

	install -m 0755 debian/database_update_debian.php debian/phpbb3/usr/share/phpbb3/install/database_update_debian.php

	@# prune the language packs and shield-ranks
	find debian/phpbb3-l10n/usr/share/phpbb3/www/language \( -name docs -o -name mods \) -type d -print0 | xargs -0r rm -r
	find debian/phpbb3-l10n/usr/share/phpbb3/styles \( -name docs -o -name mods \) -type d -print0 | xargs -0r rm -r
	find debian/phpbb3-l10n/usr/share/phpbb3/www/language \( -name README -o -name COPYING -o -name LICENSE \) -type f -print0 | xargs -0r rm
	find debian/phpbb3-l10n/usr/share/phpbb3/styles \( -name README -o -name COPYING -o -name LICENSE \) -type f -print0 | xargs -0r rm
	rm -r debian/phpbb3/var/lib/phpbb3/images/ranks/shield-ranks/contrib

	@### dbconfig install

	@# mysql
	cp install/schemas/mysql_41_schema.sql debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/mysql
	cat debian/schema_data_debian.sql >> debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/mysql

	@# postgres
	cp install/schemas/postgres_schema.sql debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/pgsql
	cat debian/schema_data_debian.sql | sed "s/# POSTGRES BEGIN #/BEGIN;/" | sed "s/^#.*//" | sed -e 's/`/"/g' -e 's/UNIX_TIMESTAMP()/EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))/' >> debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/pgsql
	debian/postgres_update_seqs.sh install/schemas/postgres_schema.sql >> debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/pgsql
	echo "COMMIT;" >> debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/pgsql

	@# sqlite
	grep -v ^# install/schemas/sqlite_schema.sql > debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/sqlite
	debian/sqliteconv1.sh debian/schema_data_debian.sql | perl -p debian/sqliteconv2.pl \
	  >> debian/phpbb3/usr/share/phpbb3/dbconfig-common/data/phpbb3/install/sqlite

	@### dbconfig update
	@###(some update /data/ copied via dh_install already)

	@# mysql
	install -m 0755 debian/database_update_debian.php debian/phpbb3/usr/share/dbconfig-common/scripts/phpbb3/upgrade/mysql/3.0.7-PL1-1

	@# postgres
	@#scripts
	install -m 0755 debian/database_update_debian.php debian/phpbb3/usr/share/dbconfig-common/scripts/phpbb3/upgrade/pgsql/3.0.7-PL1-1
	@#data: nothing
	@#3.0.7 phpbb_modules_seq update is in postinst because it needs to run BEFORE dbconfig-common update script

#	gzip -9 debian/phpbb3/usr/share/phpbb3/schemas/*.sql
	find debian/phpbb3* -type d -name .svn | xargs -r rm -r

	dh_installdeb

%:
	dh $@
