#!/usr/bin/make -f

include /usr/share/cdbs/1/rules/debhelper.mk

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

makebuilddir/otrs2::
	@if [ -d doc/manual ]; then \
		echo 'The manual is provided by a separate package otrs2-doc, please remove it.'; \
		exit 1; \
	fi

binary-post-install/otrs2::
	# setup dbconfig-common
	cat scripts/database/otrs-schema.postgresql.sql \
	    scripts/database/otrs-initial_insert.postgresql.sql > \
	    $(OTRS_DST)$(DB_DIR)/install/pgsql
	cp  debian/schemas/DBUpdate-to-3.0.postgresql.sql \
		$(OTRS_DST)$(DB_DIR)/upgrade/pgsql/3.0
	cp  scripts/DBUpdate-to-3.1.postgresql.sql \
		$(OTRS_DST)$(DB_DIR)/upgrade/pgsql/3.1
	cat scripts/database/otrs-schema.mysql.sql \
	    scripts/database/otrs-initial_insert.mysql.sql > \
	    $(OTRS_DST)$(DB_DIR)/install/mysql
	cp  debian/schemas/DBUpdate-to-3.0.mysql.sql \
		$(OTRS_DST)$(DB_DIR)/upgrade/mysql/3.0
	cp  scripts/DBUpdate-to-3.1.mysql.sql \
		$(OTRS_DST)$(DB_DIR)/upgrade/mysql/3.1
	# I guess we do not need {redhat,suse,fedora}* stuff in Debian
	rm -f $(OTRS_DST)$(OTRSHOME)/scripts/fedora* \
	      $(OTRS_DST)$(OTRSHOME)/scripts/redhat* \
	      $(OTRS_DST)$(OTRSHOME)/scripts/rpc-example.pl \
	      $(OTRS_DST)$(OTRSHOME)/scripts/suse* \
		  $(OTRS_DST)$(OTRSHOME)/scripts/otrs*gentoo* \
	      $(OTRS_DST)$(OTRSHOME)/bin/Cron4Win32.pl
	# fix some permissions
	chmod a+x $(OTRS_DST)$(OTRSHOME)/scripts/tools/*.pl
	chmod a+x $(OTRS_DST)$(OTRSHOME)/bin/otrs.*
	chmod a+x $(OTRS_DST)$(OTRSHOME)/scripts/*.pl
	chmod a+x $(OTRS_DST)$(OTRSHOME)/bin/*.pl
	chmod a+x $(OTRS_DST)$(OTRSHOME)/bin/fcgi-bin/*.pl
	find $(OTRS_DST)/var -type f -name *.js -exec chmod a-x {} \;
	find $(OTRS_DST)/var -type f -name *.css -exec chmod a-x {} \;
	# fix a security issue, link against the system prototype.js
	rm -f $(OTRS_DST)/var/lib/otrs/httpd/htdocs/js/prototype.js
	# Remove embedded ttf-dejavu fonts.
	rm -f $(OTRS_DST)/var/lib/otrs/fonts/DejaVu*.ttf
	# Move away the spool directory.
	rm -r $(OTRS_DST)/var/lib/otrs/spool
	mv $(OTRS_DST)/var/lib/otrs/spool_dir $(OTRS_DST)/var/lib/otrs/spool
	# and yeah this fucking cdbs magic..
	dh_link -potrs2
	rm $(OTRS_DST)/var/lib/otrs/spool_dir

get-orig-source:
	-uscan --upstream-version 0
