# This is a general make file for Cocoa Python projects.
#
# To use in a new project, just copy into your project directory. It
# should work out of the box with a project created by Xcode.

# TODO: get this from the Project.xcode file
build := build

# Get project data from the Info.plist
plistFile := $(wildcard Info.plist)
ifndef plistFile
  $(error Info.plist missing)
endif   

# Get keys from Info.plist. Use with $(call getInfoKey,KeyName)
getInfoKey = $(shell python -c "import plistlib; \
	print plistlib.Plist.fromFile('$(plistFile)')['$(1)']")

project := $(call getInfoKey,CFBundleName)
release := $(call getInfoKey,CFBundleShortVersionString)
version := $(call getInfoKey,CFBundleVersion)
name := $(project)-$(release)


app: clean
	# Create a standalone application
	rm -rf "$(build)/*"
	python setup.py py2app


version: clean
	# Take snapshot and bump version number
	rm -rf "$(build)/*"
	# Use zip until freeshell download problems are solved
	cd ..; zip -9 -r "Versions/$(name)-$(version).source.zip" \
		"$(shell basename `pwd`)"
	# Bump version number
	python -c "import plistlib; \
		i = plistlib.Plist.fromFile('$(plistFile)'); \
		i['CFBundleVersion'] = str($(version) + 1); \
		i.write('$(plistFile)')"


dist: disk-image version
	# Create disk image then create new version


disk-image: app 
	# Create a read only compressed disk image with the application.
	rm -rf "$(build)/$(project)"
	mkdir -p "$(build)/$(project)"
	mv "$(build)/$(project).app" "$(build)/$(project)"
	hdiutil create -format UDZO -srcfolder "$(build)/$(project)" \
		"$(build)/$(name).dmg"
	rm -rf "$(build)/$(project)"
	# zip the image dropping directory entries
	zip -9 -m -j "../Versions/$(name)-$(version).zip" "$(build)/$(name).dmg"



tidy := tidy -config tidy.conf -modify

tidy:
	# Tidy the help book and other html files
	# WARNING: TIDY WRAP BREAKS THE HELP BOOK!
	find "English.lproj/Limon Help"	-type f -name '*.html' -print \
		-exec $(tidy) "{}" \;
	$(tidy) English.lproj/Credits.html


update-services:
	# Update the Services menu
	python -c "import AppKit; AppKit.NSUpdateDynamicServices()"


clean:
	# Remove junk files
	-find . -name "*.pyc" -exec rm -f "{}" \;
	-find . -name "*~" -exec rm -f "{}" \;
	-find . -name ".DS_Store" -exec rm -f "{}" \;


.PHONY: app version dist disk-image clean tidy update-services
