#!/usr/bin/make -f

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

# Put here files that are oversized and need to have a reduced size
OVERSIZED_FILES = ./clipart/animals/mammals/big_cats/tiger_graig_ryan_smith_-_01.svg \
		  ./clipart/computer/microchip_v.2_havok_redh_01.svg \
		  ./clipart/food/beverages/milk_mateya_01.svg \
		  ./clipart/food/bread_and_wine_mark_near_.svg \
		  ./clipart/food/breads_and_carbs/bread_mateya_01.svg \
		  ./clipart/food/breads_and_carbs/pasta_mateya_01.svg \
		  ./clipart/food/dairy/cheese_mateya_01.svg \
		  ./clipart/food/desserts/cake_mateya_01.svg \
		  ./clipart/food/fruit/apple_mateya_01.svg \
		  ./clipart/food/fruit/banana_mateya_01.svg \
		  ./clipart/food/meats_and_eggs/egg_mateya_01.svg \
		  ./clipart/food/meats_and_eggs/salami_mateya_01.svg \
		  ./clipart/food/vegetables/paprika_mateya_01.svg \
		  ./clipart/food/vegetables/salad_mateya_01.svg \
		  ./clipart/geography/africa_01.svg \
		  ./clipart/geography/europe_01.svg \
		  ./clipart/geography/oceania_01.svg \
		  ./clipart/geography/world_map_01.svg \
		  ./clipart/geography/world_map_02.svg \
		  ./clipart/recreation/games/chess/tamerlane_chess_jonadab_.svg \
		  ./clipart/recreation/tamerlane_chess_jonadab_.svg \
		  ./clipart/shapes/tangram_erwan_01.svg \
		  ./clipart/shapes/tangram_erwan_02.svg \
		  ./clipart/signs_and_symbols/flags/national_flag_of_the_re_.svg \
		  ./clipart/signs_and_symbols/flags/the_official_flag_of_i_r.svg \
		  ./clipart/signs_and_symbols/stop_sign_miguel_s_nchez_.svg \
		  ./clipart/transportation/roadsigns/stop_sign_right_font_mig_.svg

%:
	dh $@

override_dh_clean:
	# Files with space in their name are causing problems. Removing spaces.
	-mv "./clipart/recreation/music/CD_Player" "./clipart/recreation/music/CD Player"
	-mv "./clipart/unsorted/profilo_della_citta.svg" "./clipart/unsorted/profilo_della citta.svg"

	-rm -rf build
	-rm -f *-stamp
	dh_clean

override_dh_auto_build:
	# Files with space in their name are causing problems. Removing spaces.
	mv "./clipart/recreation/music/CD Player" "./clipart/recreation/music/CD_Player"
	mv "./clipart/unsorted/profilo_della citta.svg" "./clipart/unsorted/profilo_della_citta.svg"

	# Create svg and png files
	set -e -x; cd $(CURDIR)/clipart && for i in `find . -name "*.svg" -printf "%p " ` ; do \
		echo "Processing $$i" ; \
		dir=`dirname $$i` ; \
		file=`basename $$i` ; \
		pngfile=`basename $$file svg`png ; \
		mkdir -p $(CURDIR)/build/usr/share/openclipart/svg/$$dir ; \
		sed -e '/<i:pgf id="adobe_illustrator_pgf">/,/<\/i:pgf>/d' $(CURDIR)/clipart/$$dir/$$file > \
			$(CURDIR)/build/usr/share/openclipart/svg/$$dir/$$file ; \
		mkdir -p $(CURDIR)/build/usr/share/openclipart/png/$$dir ; \
		if echo $(OVERSIZED_FILES) | grep -q $$i ; then \
			inkscape -w 1500 --export-filename=$(CURDIR)/build/usr/share/openclipart/png/$$dir/$$pngfile \
				 $$dir/$$file 2>&1 | grep -v "^$$" | uniq; \
		else \
			inkscape --export-filename=$(CURDIR)/build/usr/share/openclipart/png/$$dir/$$pngfile \
				 $$dir/$$file 2>&1 | grep -v "^$$" | uniq; \
		fi ; \
		optipng -quiet $(CURDIR)/build/usr/share/openclipart/png/$$dir/$$pngfile; \
	done

	# Replace duplicates files by symlinks
	cd $(CURDIR)/build/ ; \
	set -e -x; fdupes -r -1 -o name . | while read lines ; do \
		unset MAIN ; \
		for dupes in $$lines ; do \
			if [ -z "$$MAIN" ] ; then \
	    			MAIN=`echo $$dupes | sed s/\\.//` ; \
			else \
				ln -sf $$MAIN $$dupes ; \
			fi ; \
		done ; \
	done

	# Move some files
	mv $(CURDIR)/build/usr/share/openclipart/svg/animals/birds/macchina_fotografica_di_01.svg \
		$(CURDIR)/build/usr/share/openclipart/svg/electronics/
	mv $(CURDIR)/build/usr/share/openclipart/png/animals/birds/macchina_fotografica_di_01.png \
		$(CURDIR)/build/usr/share/openclipart/png/electronics/

	# Create OOo gallery files; we need to add the files in hunks
	# because we otherwise may get too many arguments....
	mkdir -p $(CURDIR)/build/usr/lib/libreoffice/share/gallery
	set -e -x; for dir in `find build/usr/share/openclipart/png -mindepth 1 -maxdepth 1 -type d | LC_CTYPE=C sort` ; do \
		gal_name=$${dir##*/}; \
		gal_oooname=`echo $$gal_name | awk '{gsub("_"," ");a=toupper(substr($$0,1,1));b=substr($$0,2);print a b}' | sed -e "s/^Education/Education2/" | sed -e "s/^People/People2/" | sed -e "s/^Transportation/Transportation2/"` ; \
		echo "Doing gallery $$gal_oooname..."; \
		find $(CURDIR)/$$dir -name "*.png" -and -not -type l >> build/$$gal_name.filelist; \
		split -d -l 250 build/$$gal_name.filelist build/$$gal_name.filelist- ; \
		for file in build/$$gal_name.filelist-*; do \
			echo "Processing filelist $$file"; \
			SAL_USE_VCLPLUGIN="svp" /usr/lib/libreoffice/program/gengal --name "$$gal_oooname" --path "$(CURDIR)/build/usr/lib/libreoffice/share/gallery" --destdir "$(CURDIR)/build" `cat $$file | xargs`; \
		done; \
	done

	cd $(CURDIR)/build/usr/lib/libreoffice/share/gallery; \
	for i in sdg sdv thm; do \
		mv shapes.$$i shapes-openclipart.$$i; \
	done

	touch build-stamp

override_dh_auto_install:
	# ignore install target of upstream Makefile
