# Makefile: otx for Mac OS X 10.4 Tiger, built on Tiger itself with Xcode 2.5 (gcc 4.0.1, GNU make 3.80), in place
# of upstream's Xcode project (format 46, which Xcode 2.5 cannot open). build.sh puts the patched source in src/
# beside this file on the G4 and runs make there.
#
#   make            build/ppci386/otx.app and build/ppci386/otx (the command), Universal (ppc, i386)
#   make ARCHS="-arch ppc"      PowerPC only in build/ppc, quicker while trying a change
#   make install    the app in /Applications and the command in /usr/local/bin, from what make built (as root)
#
# Both targets compile the same sources; the command's with OTX_CLI, as upstream's "otx cli" target.

VERSION = 0.16b
SDK = /Developer/SDKs/MacOSX10.4u.sdk
ARCHS = -arch ppc -arch i386
CC = gcc
S = src
CFLAGS = $(ARCHS) -isysroot $(SDK) -mmacosx-version-min=10.4 -Os -g -std=gnu99 \
	-Wall -Wno-unused-parameter -Werror-implicit-function-declaration -fobjc-exceptions \
	-include $(S)/otx_Prefix.pch -I$(S)/source -I$(S)/source/Categories -I$(S)/source/Processors \
	-I$(S)/source/Protocols
FRAMEWORKS = -framework Cocoa -framework Carbon -framework Security
empty :=
space := $(empty) $(empty)
ARCHDIR = $(subst $(space),,$(subst -arch,,$(ARCHS)))
B = build/$(ARCHDIR)
APP = $(B)/otx.app
PREFIX = /usr/local

COMMON = main \
	source/Processors/ExeProcessor source/Processors/Exe32Processor source/Processors/Exe64Processor \
	source/Processors/PPCProcessor source/Processors/PPC64Processor \
	source/Processors/X86Processor source/Processors/X8664Processor \
	source/Categories/ObjectLoader source/Categories/Object64Loader \
	source/Categories/ListUtils source/Categories/List64Utils \
	source/Categories/Searchers source/Categories/Searchers64 \
	source/Categories/ObjcAccessors source/Categories/Objc64Accessors \
	source/Categories/ArchSpecifics source/Categories/Arch64Specifics \
	source/Categories/SysUtils source/ObjcTypes
GUI_SRC = $(COMMON) source/AppController source/SmoothViewAnimation source/DropBox
CLI_SRC = $(COMMON) source/CLIController
GUI_OBJ = $(patsubst %,$(B)/gui/%.o,$(GUI_SRC))
CLI_OBJ = $(patsubst %,$(B)/cli/%.o,$(CLI_SRC))
HEADERS := $(shell find $(S) -name '*.h') $(S)/otx_Prefix.pch

.PHONY: all app cli install clean

all: app cli

cli: $(B)/otx

# make takes no spaces in a target's name: the bundle is put together by hand
app: $(B)/gui/otx
	rm -rf $(APP)
	mkdir -p $(APP)/Contents/MacOS $(APP)/Contents/Resources/English.lproj/MainMenu.nib
	cp $(B)/gui/otx $(APP)/Contents/MacOS/otx
	sed -e 's/$${EXECUTABLE_NAME}/otx/g' -e 's/$${PRODUCT_NAME}/otx/g' $(S)/Info.plist > $(APP)/Contents/Info.plist
	printf 'APPLot++' > $(APP)/Contents/PkgInfo
	cp $(S)/images/*.icns $(S)/images/*.tif $(APP)/Contents/Resources/
	cp $(S)/English.lproj/MainMenu.nib/keyedobjects.nib $(S)/English.lproj/MainMenu.nib/classes.nib \
		$(S)/English.lproj/MainMenu.nib/info.nib $(APP)/Contents/Resources/English.lproj/MainMenu.nib/
	cp -R $(S)/English.lproj/Help $(APP)/Contents/Resources/English.lproj/Help
	@echo "made $(APP) ($(VERSION))"

$(B)/gui/otx: $(GUI_OBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(FRAMEWORKS)

$(B)/otx: $(CLI_OBJ)
	$(CC) $(CFLAGS) -o $@ $^ $(FRAMEWORKS)

$(B)/gui/%.o: $(S)/%.m $(HEADERS)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c -o $@ $<

$(B)/cli/%.o: $(S)/%.m $(HEADERS)
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -DOTX_CLI -c -o $@ $<

# no prerequisites: it runs as root (sudo), and the build stays the build user's
install:
	test -x $(B)/otx -a -x $(APP)/Contents/MacOS/otx || { echo "make first" >&2; exit 1; }
	rm -rf /Applications/otx.app.new
	ditto $(APP) /Applications/otx.app.new
	chmod -R u=rwX,go=rX /Applications/otx.app.new
	if [ -d /Applications/otx.app ]; then mv /Applications/otx.app /Applications/otx.app.old; fi
	mv /Applications/otx.app.new /Applications/otx.app
	rm -rf /Applications/otx.app.old
	mkdir -p $(PREFIX)/bin
	install -m 755 $(B)/otx $(PREFIX)/bin/otx

clean:
	rm -rf $(B)
