#!/usr/bin/make -f
# -*- makefile -*-

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

# set some variables
ICONDIR=usr/share/icons
SHAREDIR=usr/share/crystalcursors
WRAPPERDIR=etc/X11/cursors

%:
	dh $@

override_dh_auto_install:
	# Add here commands to install the package into debian/crystalcursors.
	$(MAKE) install PREFIX=$(CURDIR)/debian/crystalcursors/$(ICONDIR)
	find $(CURDIR)/debian/crystalcursors/$(ICONDIR) -type f -exec chmod 644 {} \;
	find $(CURDIR)/debian/crystalcursors/$(ICONDIR) -type d -exec chmod 755 {} \;

	# Don't overwrite system's default xcursor, just provide the directory
	rm -r $(CURDIR)/debian/crystalcursors/$(ICONDIR)/default/*

	# There are no left-hand "cross" cursors, so don't symlink to them:
	rm $(CURDIR)/debian/crystalcursors/$(ICONDIR)/crystal*left/cursors/plus

	# Find *.theme and copy 'em to wrapper-location (see #365072)
	# We have to change the Inherits line in (only) in the wrapper *.themes:
	#  1) find .themes
	#  2) remove lines with "Inherits" and save as tmp
	#  3) write new Inherits-line with name of the directory in /usr/share/icons
	# -- I know this is tricky, but I have not yet found a more elegant way to make
	#    crystalcursors work with debian's update-alternative
	# In addition, add new symlinks that Qt 4 looks for

	cd $(CURDIR)/debian/crystalcursors/$(ICONDIR); \
	for theme in crystal*; do \
	    grep -v Inherits $(CURDIR)/debian/crystalcursors/$(ICONDIR)/$${theme}/index.theme > tmp ; \
	    echo "Inherits = $${theme}" >> tmp ; \
	    install -m 644 tmp $(CURDIR)/debian/crystalcursors/$(WRAPPERDIR)/$${theme}.theme; \
	    rm tmp; \
	    while read target link; do \
	        if [ -e $$theme/cursors/$$target ]; then ln -sf $$target $$theme/cursors/$$link; fi; \
	    done < $(CURDIR)/debian/extra_symlinks;\
	done
