#!/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

clean: 
	dh_testdir
	dh_testroot
	
	-rm -rf build
	-rm -f *-stamp
	dh_clean

build: build-stamp
build-stamp:
	dh_testdir
	
	# Create svg and png files
	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 ; \
		inkscape --export-png="$(CURDIR)/build/usr/share/openclipart/png/$$dir/$$pngfile" \
			 "$$dir/$$file" 2>&1 | grep -v "^$$" | uniq; \
	done
  
	# Replace duplicates files by symlinks
	cd $(CURDIR)/build/ ; \
	fdupes -r -1 . | 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

	# Create OOo gallery files; we need to add the files in hunks
	# because we othererwise may get too many arguments....
	mkdir -p $(CURDIR)/build/usr/lib/libreoffice/share/gallery
	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}'` ; \
		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" --number-from "70" `cat $$file | xargs`; \
		done; \
	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 LibreOffice 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
