# This is a general make file for Cocoa 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)']")

product := $(call getInfoKey,CFBundleName)
release := $(call getInfoKey,CFBundleShortVersionString)
name := $(product)
releaseName := $(shell echo $(product) | sed 's/ //')-$(release)
temp := $(build)/$(product)
install := $(HOME)/Library/InputManagers/$(product)

default:
	xcodebuild 

install:
	-chmod -R u+w "$(install)"
	-rm -rf "$(install)"
	xcodebuild clean install

dist: site disk-image source
	# Create disk image and source tarball

source:
	# Create source tarball
	bzr export "../versions/$(releaseName).src.tgz"

disk-image: install 
	# Create a read only disk image
	-chmod -R u+w "$(temp)"
	-rm -rf "$(temp)"
	mkdir -p "$(temp)"
	# Copy folder with a custom icon from a disk image
	./copydmg Template.dmg Folder "$(temp)/$(product)"
	cp -r "$(install)/$(product).bundle" "$(temp)/$(product)/"
	cp -r "$(install)/Info" "$(temp)/$(product)/"
	cp "site/index.html" "$(temp)/ReadMe.html"
	cp -r "site/media" "$(temp)/"
	cp "COPYING" "$(temp)/"
	cp test.command "$(temp)/"
	# create disk read only compressed image
	hdiutil create -format UDZO \
		-imagekey zlib-level=9 \
		-srcfolder "$(temp)" \
		"$(build)/$(releaseName).dmg"
	chmod -R u+w "$(temp)"
	rm -rf "$(temp)"
	# zip the image dropping directory entries
	zip -9 -m -j "../Versions/$(releaseName).zip" \
		"$(build)/$(releaseName).dmg"

site:
	$(MAKE) -C site

strings:
	genstrings -s LocalizedString -o Resources/English.lproj *.m *.h

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


.PHONY: default install dist source disk-image clean site strings
