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

build-arch:

build-indep: 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/lib/libreoffice/share/gallery \
      echo $(CURDIR)/build/usr/share/openclipart2/svg/$$dir; \
      mkdir -p $(CURDIR)/build/usr/share/openclipart2/svg/$$dir; \
		sed -e '/<i:pgf id="adobe_illustrator_pgf">/,/<\/i:pgf>/d' "$(CURDIR)/clipart/$$dir/$$file" > \
			"$(CURDIR)/build/usr/share/openclipart2/svg/$$dir/$$file"; \
		mkdir -p $(CURDIR)/build/usr/share/openclipart2/png/$$dir; \
		inkscape --export-png="$(CURDIR)/build/usr/share/openclipart2/png/$$dir/$$pngfile" \
			 "$$dir/$$file" 2>&1 | grep -v "^$$" | uniq; \
      optipng -quiet "$(CURDIR)/build/usr/share/openclipart2/png/$$dir/$$pngfile"; \
	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 LibreOffice 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
	for dir in `find build/usr/share/openclipart2/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 "100" `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/openclipart2/png/* \
		$(CURDIR)/debian/openclipart2-png/usr/share/openclipart2/png/

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

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

	# Install Overrides file
	install -p -o root -g root -m 644 $(CURDIR)/debian/openclipart2-svg.overrides $(CURDIR)/debian/openclipart2-svg/usr/share/lintian/overrides/openclipart2-svg

# 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_installdocs
	dh_installexamples
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	NO_PNG_PKG_MANGLE=1 dh_builddeb

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