# Description: Build file for creating DMG files.
# Copyright 2008 Remko Troncon <http://el-tramo.be/guides/fancy-dmg>
# License: BSD <http://opensource.org/licenses/bsd-license.php> 
#
# The DMG packager looks for a template.dmg.bz2 for using as its 
# DMG template. If it doesn't find one, it generates a clean one.
#
# If you create a DMG template, you should make one containing all
# the files listed in $(SOURCE_FILES) below, and arrange everything to suit
# your style. The contents of the files themselves does not matter, so
# they can be empty (they will be overwritten later). 
#
# Remko Troncon 
# http://el-tramo.be/about
#


################################################################################
# Customizable variables
################################################################################

NAME=MacIntelCinepaint
VERSION=0.23

SOURCE_DIR=.
SOURCE_FILES=MacIntelCinepaint

TEMPLATE_DMG=template.dmg
SIZE_DMG=70m

################################################################################
# DMG building. No editing should be needed beyond this point.
################################################################################

MASTER_DMG=$(NAME)-$(VERSION).dmg
WC_DMG=wc.dmg
WC_DIR=wc

.PHONY: all
all: $(MASTER_DMG)

$(TEMPLATE_DMG): $(TEMPLATE_DMG).bz2
	bunzip2 -k $<

$(TEMPLATE_DMG).bz2: 
	@echo --------------------- Generating empty template --------------------
	mkdir template
	hdiutil create -size "$(SIZE_DMG)" "$(TEMPLATE_DMG)" -srcfolder template -format UDRW -volname "$(NAME)"
	rmdir template
	bzip2 "$(TEMPLATE_DMG)"

$(WC_DMG): $(TEMPLATE_DMG)
	cp $< $@

$(MASTER_DMG): $(WC_DMG) $(addprefix $(SOURCE_DIR)/,$(SOURCE_FILES))
	@echo --------------------- Creating Disk Image --------------------
	mkdir -p $(WC_DIR)
	hdiutil attach "$(WC_DMG)" -noautoopen -mountpoint "$(WC_DIR)"
	for i in $(SOURCE_FILES); do  \
		rm -rf "$(WC_DIR)/$$i"; \
		ditto -rsrc "$(SOURCE_DIR)/$$i" "$(WC_DIR)/$$i"; \
	done
	#rm -f "$@"
	#hdiutil create -srcfolder "$(WC_DIR)" -format UDZO -imagekey zlib-level=9 "$@" -volname "$(NAME) $(VERSION)" -scrub
	WC_DEV=`hdiutil info | grep "$(WC_DIR)" | grep "Apple_HFS" | awk '{print $$1}'` && \
	hdiutil detach $$WC_DEV -force; echo $(WC_DEV)
	rm -f "$(MASTER_DMG)"
	hdiutil convert "$(WC_DMG)" -format UDZO -imagekey zlib-level=9 -o "$@"
	rm -rf $(WC_DIR)

.PHONY: clean
clean:
	-rm -rf $(TEMPLATE_DMG) $(MASTER_DMG) $(WC_DMG)
