#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

export DESTRESDIR=$(CURDIR)/debian/tmp
export IMGDIR=$(DESTRESDIR)/usr/share/icons/hicolor

# Grab the version from the changelog, up to the first -
export DEB_DEBIAN_DIR=$(dir $(firstword $(MAKEFILE_LIST)))
export DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog -l$(DEB_DEBIAN_DIR)/changelog \
                               | sed -rne 's,^Version: ([^-]+).*,\1,p')

%:
	dh $@ --parallel

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --sourcedirectory=RealTimeSync

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --sourcedirectory=RealTimeSync
	
override_dh_auto_install:
	dh_auto_install
	dh_auto_install --sourcedirectory=RealTimeSync
	
	# Extract program icons
	mkdir -p $(IMGDIR)/256x256/apps
	mkdir -p $(IMGDIR)/256x256/mimetypes
	# Do not generate 128x128 image on platform > Lucid, as 'convert' is not able to handle it
	# See LP : 807013
	#mkdir -p $(IMGDIR)/128x128/apps
	#mkdir -p $(IMGDIR)/128x128/mimetypes
	mkdir -p $(IMGDIR)/48x48/apps
	mkdir -p $(IMGDIR)/48x48/mimetypes
	mkdir -p $(IMGDIR)/32x32/apps
	mkdir -p $(IMGDIR)/32x32/mimetypes
	mkdir -p $(IMGDIR)/16x16/apps
	mkdir -p $(IMGDIR)/16x16/mimetypes

	# FreeFileSync
	convert debian/FreeFileSync.ico[0] $(IMGDIR)/256x256/apps/freefilesync.png
	convert debian/FreeFileSync.ico[0] $(IMGDIR)/256x256/mimetypes/application-x-freefilesync-ffs.png
	# Do not generate 128x128 image on platform > Lucid, as 'convert' is not able to handle it
	# See LP : 807013
	#convert lib/FreeFileSync.ico[1] $(IMGDIR)/128x128/apps/freefilesync.png
	#convert lib/FreeFileSync.ico[1] $(IMGDIR)/128x128/mimetypes/application-x-freefilesync-ffs.png
	convert debian/FreeFileSync.ico[2] $(IMGDIR)/48x48/apps/freefilesync.png
	convert debian/FreeFileSync.ico[2] $(IMGDIR)/48x48/mimetypes/application-x-freefilesync-ffs.png
	convert debian/FreeFileSync.ico[3] $(IMGDIR)/32x32/apps/freefilesync.png
	convert debian/FreeFileSync.ico[3] $(IMGDIR)/32x32/mimetypes/application-x-freefilesync-ffs.png
	convert debian/FreeFileSync.ico[4] $(IMGDIR)/16x16/apps/freefilesync.png
	convert debian/FreeFileSync.ico[4] $(IMGDIR)/16x16/mimetypes/application-x-freefilesync-ffs.png
	
	# RealTimeSync
	convert debian/RealTimeSync.ico[0] $(IMGDIR)/256x256/apps/realtimesync.png
	convert debian/RealTimeSync.ico[0] $(IMGDIR)/256x256/mimetypes/application-x-freefilesync-real.png
	# Do not generate 128x128 image on platform > Lucid, as 'convert' is not able to handle it
	# See LP : 807013
	#convert RealtimeSync/RealtimeSync.ico[1] $(IMGDIR)/128x128/apps/realtimesync.png
	#convert RealtimeSync/RealtimeSync.ico[1] $(IMGDIR)/128x128/mimetypes/application-x-freefilesync-real.png
	convert debian/RealTimeSync.ico[2] $(IMGDIR)/48x48/apps/realtimesync.png
	convert debian/RealTimeSync.ico[2] $(IMGDIR)/48x48/mimetypes/application-x-freefilesync-real.png
	convert debian/RealTimeSync.ico[3] $(IMGDIR)/32x32/apps/realtimesync.png
	convert debian/RealTimeSync.ico[3] $(IMGDIR)/32x32/mimetypes/application-x-freefilesync-real.png
	convert debian/RealTimeSync.ico[4] $(IMGDIR)/16x16/apps/realtimesync.png
	convert debian/RealTimeSync.ico[4] $(IMGDIR)/16x16/mimetypes/application-x-freefilesync-real.png
    
	# FreeFileSync Batch
	convert debian/Batch.ico[0] $(IMGDIR)/256x256/mimetypes/application-x-freefilesync-batch.png
	# Do not generate 128x128 image on platform > Lucid, as 'convert' is not able to handle it
	# See LP : 807013
	#convert lib/Batch.ico[1] $(IMGDIR)/128x128/mimetypes/application-x-freefilesync-batch.png
	convert debian/Batch.ico[2] $(IMGDIR)/48x48/mimetypes/application-x-freefilesync-batch.png
	convert debian/Batch.ico[3] $(IMGDIR)/32x32/mimetypes/application-x-freefilesync-batch.png
	convert debian/Batch.ico[4] $(IMGDIR)/16x16/mimetypes/application-x-freefilesync-batch.png	

	# Build desktop files
	mkdir -p $(DESTRESDIR)/usr/share/applications
	intltool-merge debian/po/freefilesync -d -u -c debian/po/.intltool-merge-cache debian/freefilesync.desktop.in $(DESTRESDIR)/usr/share/applications/freefilesync.desktop
	intltool-merge debian/po/freefilesync -d -u -c debian/po/.intltool-merge-cache debian/realtimesync.desktop.in $(DESTRESDIR)/usr/share/applications/realtimesync.desktop

	find debian/freefilesync/usr/share/FreeFileSync -type f -execdir chmod -x {} \;

override_dh_installchangelogs:
	dh_installchangelogs Changelog.txt

get-orig-source:
	# Uscan can grab upstream source zip
	uscan --noconf --force-download --download-current-version --debug --verbose --destdir=. $(DEB_DEBIAN_DIR)/..
	# Unpack
	# Repacked source should have a top-level directory ending in .orig
	# as a reminder to other developers
	unzip FreeFileSync_$(DEB_UPSTREAM_VERSION)_Source.zip -d freefilesync-$(DEB_UPSTREAM_VERSION)
	rm -f *.orig.tar.gz *.orig.tar.xz
	cd freefilesync-$(DEB_UPSTREAM_VERSION) && \
	    mv wx+ zen FreeFileSync/Source && \
	    mv FreeFileSync/Build FreeFileSync/Source && \
	    rm License.txt && \
	    mv FreeFileSync/Source/* . && \
	    rmdir FreeFileSync/Source && \
	    rmdir FreeFileSync
	rm FreeFileSync_$(DEB_UPSTREAM_VERSION)_Source.zip
	find -L "freefilesync-$(DEB_UPSTREAM_VERSION)" -xdev -type f -print | LC_ALL=C sort \
	| XZ_OPT="-6v" tar -caf "freefilesync_$(DEB_UPSTREAM_VERSION).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "freefilesync-$(DEB_UPSTREAM_VERSION)"
