# Makefile
#
# Copyright (C) 2008 MikuInstaller Project. All rights reserved.
# http://mikuinstaller.sourceforge.jp/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

VERSION=20080720

# abbreviations
BUNDLE = build/Wine.bundle/Contents

######### miscellaneous ########

all: _MIKUINSTALLER

clean:
	-rm -rf StartApp/build
	-rm -rf MikuInstaller/build
	-rm -rf build
	-rm -f _*

_VERSION: Makefile
	touch _VERSION

_FRONTEND:  # for convenience
	touch _FRONTEND

######## Build extra tools ########

_LIB: _FRONTEND
	-rm -rf $(BUNDLE)/Resources/lib
	mkdir -p $(BUNDLE)/Resources/lib
	prefix=`pwd`; \
	  $(MAKE) -C $(BUNDLE)/Resources/lib \
	          -f "$$prefix/lib/Makefile" \
	          "srcdir=$$prefix/lib/"
	strip -S -x $(BUNDLE)/Resources/lib/*.exe.so
	touch _LIB

######## Build StartApp.apptemplate ########

_STARTAPP: _FRONTEND
	-rm -rf $(BUNDLE)/SharedSupport/StartApp.apptemplate
	cd StartApp && xcodebuild -configuration Release
	mkdir -p $(BUNDLE)/Resources/StartApp.apptemplate
	cp -R StartApp/build/Release/StartApp.app/Contents \
	      $(BUNDLE)/Resources/StartApp.apptemplate
	touch _STARTAPP

######## Setup scripts ########

_SCRIPT: _FRONTEND _VERSION
	-rm -rf $(BUNDLE)/Resources/script $(BUNDLE)/MacOS
	mkdir -p $(BUNDLE)/Resources/script
	for i in script/*; do \
	  sed 's/MIKUVERSION=@VERSION@/MIKUVERSION=$(VERSION)/' "$$i" \
	    > $(BUNDLE)/Resources/script/`basename "$$i"`; \
	done
	chmod +x $(BUNDLE)/Resources/script/*.sh
	mkdir -p $(BUNDLE)/MacOS
	mv $(BUNDLE)/Resources/script/start.sh $(BUNDLE)/MacOS/wine
	touch _SCRIPT

######## Setup inf files ########

_INF: _FRONTEND
	-rm -rf $(BUNDLE)/Resources/inf
	mkdir -p $(BUNDLE)/Resources/inf
	cp inf/* $(BUNDLE)/Resources/inf
	touch _INF

######## Make Wine.bundle ########

_BUNDLE: _FRONTEND _VERSION _STARTAPP _SCRIPT _INF _LIB
	-rm -f $(BUNDLE)/Info.plist $(BUNDLE)/bin $(BUNDLE)/lib \
	       $(BUNDLE)/include $(BUNDLE)/share
	{ \
	  wine_version=`PATH='$(BUNDLE)/SharedSupport/bin'":$PATH";wine --version`; \
	  echo '<?xml version="1.0" encoding="UTF-8"?>'; \
	  echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'; \
	  echo '<plist version="1.0">'; \
	  echo '<dict>'; \
	  echo '	<key>CFBundleInfoDictionaryVersion</key>'; \
	  echo '	<string>6.0</string>'; \
	  echo '	<key>CFBundleGetInfoString</key>'; \
	  echo '	<string>MikuInstaller $(VERSION), '"$$wine_version"'</string>'; \
	  echo '	<key>CFBundleName</key>'; \
	  echo '	<string>MikuInstaller</string>'; \
	  echo '	<key>CFBundleVersion</key>'; \
	  echo '	<string>$(VERSION)</string>'; \
	  echo '	<key>CFBundlePackageType</key>'; \
	  echo '	<string>thng</string>'; \
	  echo '	<key>CFBundleIdentifier</key>'; \
	  echo '	<string>jp.sourceforge.mikuinstaller.Wine</string>'; \
	  echo '</dict>'; \
	  echo '</plist>'; \
	} > $(BUNDLE)/Info.plist
	touch _BUNDLE

######## Build MikuInstaller ########

LINKTREE = \
  @linktree () { \
    set -e; \
    echo "Linking $$2 to $$1..."; \
    mkdir -p "$$2"; \
    (cd "$$1" && find * -type d) \
      | (set +x; while read i; do mkdir -p "$$2/$$i" || exit $?; done); \
    (cd "$$1" && find * -type l -print0 | pax -0 -w) \
      | (cd "$$2" && pax -r); \
    (cd "$$1" && find * -type f) \
      | (set +x; while read i; do ln "$$1/$$i" "$$2/$$i" || exit $?; done); \
  }; linktree

_MIKUINSTALLER: _FRONTEND _VERSION _BUNDLE
	-rm -rf build/MikuInstaller.app
	cd MikuInstaller && xcodebuild -configuration Release
	mv MikuInstaller/build/Release/MikuInstaller.app build
	perl -pi -e 's/\@VERSION\@/$(VERSION)/g' \
	  build/MikuInstaller.app/Contents/Info.plist
	$(LINKTREE) $(BUNDLE) \
	  build/MikuInstaller.app/Contents/Resources/Wine.bundle/Contents
	touch _MIKUINSTALLER
