#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# 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/signs_and_symbols/flags/national_flag_of_the_re_.svg \
		  ./clipart/signs_and_symbols/flags/the_official_flag_of_i_r.svg \
		  ./clipart/food/bread_and_wine_mark_near_.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/shapes/tangram_erwan_01.svg \
		  ./clipart/shapes/tangram_erwan_02.svg \
		  ./clipart/recreation/games/chess/tamerlane_chess_jonadab_.svg \
		  ./clipart/recreation/tamerlane_chess_jonadab_.svg

clean:
	dh_testdir
	dh_testroot

	# 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


build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp:
	dh_testdir

	# 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

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Install PNG files
	cp -af $(CURDIR)/build/usr/share/openclipart/png/* \
		$(CURDIR)/debian/openclipart-png/usr/share/openclipart/png/

	# Install SVG files
	cp -af $(CURDIR)/build/usr/share/openclipart/svg/* \
		$(CURDIR)/debian/openclipart-svg/usr/share/openclipart/svg/

	# Install OOo files
	mkdir -p $(CURDIR)/debian/openclipart-libreoffice/usr/lib/libreoffice/share/gallery
	cp -af $(CURDIR)/build/usr/lib/libreoffice/share/gallery/* \
		$(CURDIR)/debian/openclipart-libreoffice/usr/lib/libreoffice/share/gallery/


# Build architecture-dependent files here.
# We have nothing to do by default.
binary-arch: build install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
#	dh_installlogrotate
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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