# Makefile for Canorus OS X package
#
# NOTE: Before running this file, do "make install" from the main source directory

# The name of the disk image file
DMG_FILE = Canorus.dmg

# The directory that servers as the root of the disk image
DMG_DIR = Canorus

# The directory that serves at the root of the package installation tree
PACKAGE_ROOT = package-contents

# Directory that contains the .app
APP_DIR = $(PACKAGE_ROOT)

FRAMEWORKS_DIR = $(APP_DIR)/Canorus.app/Contents/Frameworks

# Directory containing misc. resources
RESOURCES_DIR = files

# File with platform-specific instructions
README_FILE = README-OSX.txt

# Path to the PackageMaker executable
PACKAGEMAKER = /Developer/Applications/Utilities/PackageMaker.app/Contents/MacOS/PackageMaker

all: files permissions pkg dmg

# append Canorus version with SVN revision to README-OSX, e.g. "Version 0.3svn R509" 
readme:
	perl -e '$$line = <STDIN>;	\
		if ($$line =~ /svn/) {	\
			chomp $$line;	\
			$$rev = `svn info | sed -n "/Revision/ p"`;	\
			$$rev =~ s/Revision: /R/;	\
		}	\
		print "Canorus\nVersion $$line $$rev\n";' < ../VERSION >$(DMG_DIR)/$(README_FILE)
		cat $(RESOURCES_DIR)/$(README_FILE) >> $(DMG_DIR)/$(README_FILE)


# Include dynamically linked Qt Frameworks inside the app bundle for
# redistribution. This is done using the macdeployqt program provided
# since 4.5.0. However, version 4.5.0 incorrectly binds svg libraries.
# You either have to use 4.5.1 or apply the svg correction included in
# http://www.mail-archive.com/qt4-preview-feedback@trolltech.com/msg00849.html
links:
	(cd package-contents && macdeployqt Canorus.app)


# Copy files from the resources dir to the right place
files: readme
	cp $(RESOURCES_DIR)/canorus-example.xml $(DMG_DIR)


# Change permissions on the .app
permissions:
	chmod -R 0775 $(PACKAGE_ROOT)
	chgrp -R admin $(PACKAGE_ROOT) 
	sudo chown -R root $(PACKAGE_ROOT) 


# Create the installer .pkg file
pkg:
	$(PACKAGEMAKER) -build -v -ds \
	        -p "$(DMG_DIR)/Canorus.pkg" \
	        -f "$(PACKAGE_ROOT)" \
	        -r "$(RESOURCES_DIR)" \
	        -i "$(RESOURCES_DIR)/Info.plist" \
	        -d "$(RESOURCES_DIR)/Description.plist"


# Create the .dmg (disk image) file
dmg:
	hdiutil create -srcfolder $(DMG_DIR) Canorus.dmg 


# Remove all files generated by the packaging process
pkgclean:
	rm -f $(DMG_FILE)
	sudo rm -rf $(DMG_DIR)/*


# remove all files generated by "make install" from cmake;
# see also "make uninstall" by cmake
instclean:
	sudo rm -rf $(PACKAGE_ROOT)/*


allclean: instclean pkgclean

# Simply pack the application and fonts into a zip to avoid installers.
zip:
	rm -f Canorus.zip
	(cd $(PACKAGE_ROOT) && zip -9 -r ../../Canorus.zip Canorus.app)

