#!/usr/bin/make -f

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

# SOURCEVERSION:=$(shell dpkg-parsechangelog | sed -rne 's,^Source: typo3-src(-.+),\1,p')
SOURCEVERSION:=-4.5

PACKAGENAME:=typo3_src$(SOURCEVERSION)
SOURCEPACKAGE:=typo3-src$(SOURCEVERSION)

T3SRCDIR:=usr/share/typo3/$(PACKAGENAME)

SOURCEINSTALLDIR:=debian/$(SOURCEPACKAGE)/$(T3SRCDIR)

DOCROOTDIR=debian/typo3-dummy/var/lib/typo3-dummy
ETCDIR=debian/typo3-dummy/etc/typo3-dummy

MAIN_TGZ_VERSION:=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*,\1,p')
SUB_TGZ_VERSION:=$(shell dpkg-parsechangelog | sed -rne 's,^Version: (.*)\+dfsg1~([^-]+).*,\2,p')
SUB2_TGZ_VERSION:=$(shell dpkg-parsechangelog | sed -rne 's,^Version: (.*)\+dfsg1([^-]+).*,\2,p')

ORIG_DIR:=$(SOURCEPACKAGE)_$(MAIN_TGZ_VERSION)+dfsg1$(SUB2_TGZ_VERSION)
BLANK_DIR:=blankpackage-$(MAIN_TGZ_VERSION)$(SUB_TGZ_VERSION)

ORIGTAR:=$(SOURCEPACKAGE)_$(MAIN_TGZ_VERSION)$(SUB_TGZ_VERSION).orig.tar
DEBTAR:=$(SOURCEPACKAGE)_$(MAIN_TGZ_VERSION)+dfsg1$(SUB2_TGZ_VERSION).orig.tar

clean:
	dh_testdir
	dh_testroot
	dh_clean
	debconf-updatepo
	rm -f $(SOURCEINSTALLDIR)/t3lib/fonts/nimbus.sfd.gz
	rm -rf build

build_pre:
	dh_testdir
	mkdir -p build

build: build_pre build/svg.swf build/svg.js build/svg.htc

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_install
	cp -r index.php t3lib typo3 $(SOURCEINSTALLDIR)
	cp debian/nimbus.sfd.gz $(SOURCEINSTALLDIR)/t3lib/fonts
	cp typo3conf/localconf.php $(ETCDIR)/localconf.php_template
#set correct permissions for cron script
	chmod a+x $(SOURCEINSTALLDIR)/typo3/cleaner_check.sh \
		  $(SOURCEINSTALLDIR)/typo3/cleaner_fix.sh \
		  $(SOURCEINSTALLDIR)/typo3/cli_dispatch.phpsh
#remove additional license files
	rm -f $(SOURCEINSTALLDIR)/typo3/LICENSE.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/rtehtmlarea/htmlarea/HTMLAREA_LICENSE.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/rtehtmlarea/htmlarea/plugins/InsertSmiley/smileys/mozilla_public_license.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/adodb/adodb/license.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/t3editor/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/openid/lib/php-openid/COPYING
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/rsaauth/resources/jsbn/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/lua/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/ometa/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/php/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/plsql/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/python/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/scheme/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/codemirror/contrib/sql/LICENSE
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/extjs/LICENSE.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/flashmedia/player.license.txt
	rm -f $(SOURCEINSTALLDIR)/typo3/contrib/modernizr/LICENSE.txt
# remove files linked to by later steps in build (dependencies)
	rm -f $(SOURCEINSTALLDIR)/t3lib/fonts/vera.ttf
	rm -rf $(SOURCEINSTALLDIR)/typo3/sysext/adodb/adodb
# fix other stuff
	rm -f $(SOURCEINSTALLDIR)/typo3/sysext/linkvalidator/.gitignore
# apply security patch
	cp debian/patches/player.swf $(SOURCEINSTALLDIR)/typo3/contrib/flashmedia/

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_lintian
	dh_installdebconf
	dh_installchangelogs -Ntypo3 -A ChangeLog
	dh_installcron
	dh_installdocs -Ntypo3 -A RELEASE_NOTES.txt README.txt NEWS.txt
	dh_installexamples
	dh_installinit
	dh_link
	dh_compress -Ntypo3
	dh_fixperms
	chown -hR root:www-data $(SOURCEINSTALLDIR)
	chown -hR www-data:www-data $(DOCROOTDIR)
	chown     www-data:www-data $(ETCDIR)/localconf.php_template
	find $(DOCROOTDIR) -type d -print0 | xargs -0 chmod 0750
	find $(DOCROOTDIR) -type f -print0 | xargs -0 chmod 0640
	chmod 600 $(ETCDIR)/localconf.php_template
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

build-arch: build
build-indep: build

get-orig-source:
# get source
	uscan --no-conf --force-download --rename --repack --download-current-version --destdir=.
# unpack and rename
	mkdir -p _unpack
	tar -xzf $(ORIGTAR).gz -C _unpack
	mv _unpack/$(BLANK_DIR) _unpack/$(ORIG_DIR)
# remove dfsg non complient files
	rm -f _unpack/$(ORIG_DIR)/typo3/contrib/jsmin/jsmin.php
	rm -f _unpack/$(ORIG_DIR)/typo3/sysext/em/res/js/ux/jslint.js
# remove embedded libraries
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/swiftmailer
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/prototype
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/scriptaculous
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/flashmedia/swfobject
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/swfupload
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/extjs/resources/charts.swf
	rm -rf _unpack/$(ORIG_DIR)/typo3/contrib/extjs/resources/expressinstall.swf
# repack
	tar -cf - -C _unpack $(ORIG_DIR) | gzip -9 - > $(DEBTAR).gz
	rm -rf _unpack

prepare-source:
	dh_testdir
	mv $(ORIGTAR).gz ../
	mv $(DEBTAR).gz ../
	tar --strip-components 1 -C . -xzf ../$(DEBTAR).gz

# Build architecture-dependent files here.
binary-arch: build install

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

RELEASE_NAME=
RELEASE_NUMBER=$(shell svnversion)
COMPILER_CONSTANTS=--define BUILD::releaseName,\'$(RELEASE_NAME)\' --define BUILD::releaseNumber,\'$(RELEASE_NUMBER)\'

build/svg.swf: debian/contrib/websvg/src/org/svgweb/SVGViewerWeb.as debian/contrib/websvg/src/org/svgweb/core/*.as debian/contrib/websvg/src/org/svgweb/nodes/*.as debian/contrib/websvg/src/org/svgweb/utils/*.as debian/contrib/websvg/src/org/svgweb/smil/*.as
#	cd debian/contrib/websvg/src/org/svgweb && as3compile $(COMPILER_CONSTANTS) -o ../../../../../build/svg.swf -I ../../ SVGViewerWeb.as

build/svg.js: debian/contrib/websvg/src/svg.js
	yui-compressor --type js --preserve-semi -o build/svg.js debian/contrib/websvg/src/svg.js

build/svg.htc: debian/contrib/websvg/src/svg.htc
	cp debian/contrib/websvg/src/svg.htc build/svg.htc
