#!/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/-.?*).*//')
# Last version that matches the database
LAST_VERSION := 3.0.12

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

override_dh_auto_clean:
	debconf-updatepo -v

override_dh_install:
	dh_install -XAUTHORS -XCOPYING -XINSTALL.html -XLICENSE -Xschemas/index.htm -XVERSION

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

override_dh_installdeb:
	@# customize Debian version
	sed -i "s/@VERSION@/$(VERSION) (Debian)/" debian/phpbb3/usr/share/phpbb3/www/common.php
	@# prune the language packs
	rm debian/phpbb3-l10n/usr/share/phpbb3/www/language/index.htm
	rm debian/phpbb3-l10n/usr/share/phpbb3/www/language/*/README
	rm -r debian/phpbb3-l10n/usr/share/phpbb3/www/language/*/mods

	@### 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/$(LAST_VERSION)

	@# postgres
	@#scripts
	install -m 0755 debian/database_update_debian.php debian/phpbb3/usr/share/dbconfig-common/scripts/phpbb3/upgrade/pgsql/$(LAST_VERSION)

	dh_installdeb

%:
	dh $@
