#!/usr/bin/make -f

OTRSHOME := /usr/share/otrs
DB_BASE  := /usr/share/dbconfig-common
DB_DIR   := $(DB_BASE)/data/otrs2
OTRS_DST := $(CURDIR)/debian/otrs2/

%:
	dh $@

override_dh_install:
	cp scripts/apache2-httpd.include.conf debian/otrs2.conf
	dh_install
	# Setup dbconfig-common
	# PostgreSQL
	cat scripts/database/schema.postgresql.sql \
	    scripts/database/initial_insert.postgresql.sql \
		scripts/database/schema-post.postgresql.sql > \
	    $(OTRS_DST)$(DB_DIR)/install/pgsql
	cp debian/schemas/DBUpdate-to-6.0.32 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.0.32
	cp debian/schemas/DBUpdate-to-6.1.2 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.1.2
	cp debian/schemas/DBUpdate-to-6.2.2 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.2.2
	cp debian/schemas/DBUpdate-to-6.4.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.4.1
	cp debian/schemas/DBUpdate-to-6.4.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.4.1
	cp debian/schemas/DBUpdate-to-6.5.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/pgsql/6.5.1
	# MySQL
	cat scripts/database/schema.mysql.sql \
	    scripts/database/initial_insert.mysql.sql \
		scripts/database/schema-post.mysql.sql > \
	    $(OTRS_DST)$(DB_DIR)/install/mysql
	cp debian/schemas/DBUpdate-to-6.0.32 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.0.32
	cp debian/schemas/DBUpdate-to-6.1.2 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.1.2
	cp debian/schemas/DBUpdate-to-6.2.2 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.2.2
	cp debian/schemas/DBUpdate-to-6.3.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.3.1
	cp debian/schemas/DBUpdate-to-6.4.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.4.1
	cp debian/schemas/DBUpdate-to-6.5.1 \
		$(OTRS_DST)$(DB_BASE)/scripts/otrs2/upgrade/mysql/6.5.1
	# Remove useless stuff.
	rm -rf $(OTRS_DST)$(OTRSHOME)/scripts/auto_build
	rm -rf $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/exportpdf/tests/
	rm -rf $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/samples/
	rm -rf $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/codesnippetgeshi/dev/
	rm -rf $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/magicline/dev/
	# Remove embedded ttf-dejavu fonts.
	rm $(OTRS_DST)/var/lib/otrs/fonts/DejaVu*.ttf
	# Remove embedded fontawesome fonts.
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/common/fonts/fontawesome-webfont.ttf
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/common/fonts/FontAwesome.otf
	# Remove extra license file.
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/LICENSE.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/codesnippet/lib/highlight/LICENSE
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/exportpdf/LICENSE.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/scayt/LICENSE.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/wsc/LICENSE.md
	# Remove extra documentation files.
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/jsdoc-readme.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/CHANGES.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/codesnippet/lib/highlight/CHANGES.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/plugins/codesnippet/lib/highlight/README.ru.md
	rm $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/thirdparty/ckeditor-*/skins/*/readme.md
	# Replace shebangs.
	find $(OTRS_DST)/usr/share/otrs/bin/ -type f -name \*.pl -exec sed 's;^#!/usr/bin/env perl;#!/usr/bin/perl;' -i {} \;
	find $(OTRS_DST)/usr/share/otrs/bin/ -type f -name \*.psgi -exec sed 's;^#!/usr/bin/env perl;#!/usr/bin/perl;' -i {} \;
	find $(OTRS_DST)/usr/share/otrs/scripts/ -type f -name \*.pl -exec sed 's;^#!/usr/bin/env perl;#!/usr/bin/perl;' -i {} \;
	# Fix permissions.
	chmod +x $(OTRS_DST)/usr/share/otrs/bin/cgi-bin/get-oauth2-token-by-authorization-code.pl
	chmod +x $(OTRS_DST)/usr/share/otrs/bin/fcgi-bin/get-oauth2-token-by-authorization-code.pl

override_dh_installchangelogs:
	dh_installchangelogs CHANGES.md
