SERVER_PATH=../server
SERVER_BINARY=hop_server.native
SERVER_WEB=web
APP_NAME="Hop Server"

RELEASE_RESOURCES=build/Release/$(APP_NAME).app/Contents/Resources
DEBUG_RESOURCES=build/Debug/$(APP_NAME).app/Contents/Resources

BUILD_STAMP:=$(shell date +%Y%m%d-%H%M%S)
SERVER_VERSION:=git-$(shell cd $(SERVER_PATH); git branch -v --abbrev=8 | grep '^\*' | awk '{print $$3}')

release: $(SERVER_PATH)/$(SERVER_BINARY)
	xcodebuild -configuration Release
	cp $(SERVER_PATH)/$(SERVER_BINARY) $(RELEASE_RESOURCES)/.
	sh ./fixup_shlibs.sh $(RELEASE_RESOURCES) $(SERVER_BINARY)
	cp -rp $(SERVER_PATH)/$(SERVER_WEB) $(RELEASE_RESOURCES)/.
	sed \
		-e 's:@BUILD_STAMP@:$(BUILD_STAMP):g' \
		-e 's:@SERVER_VERSION@:$(SERVER_VERSION):g' \
		$(RELEASE_RESOURCES)/English.lproj/Credits.html \
		> $(RELEASE_RESOURCES)/English.lproj/Credits.html.tmp
	mv \
		$(RELEASE_RESOURCES)/English.lproj/Credits.html.tmp \
		$(RELEASE_RESOURCES)/English.lproj/Credits.html
	/usr/libexec/PlistBuddy \
		-c "Set CFBundleVersion $(SERVER_VERSION)" \
		$(RELEASE_RESOURCES)/../Info.plist

# /usr/libexec/PlistBuddy \
# 	-c "Set CFBundleShortVersionString $(GUI_VERSION)" \
# 	$(RELEASE_RESOURCES)/../Info.plist

debug: $(SERVER_PATH)/$(SERVER_BINARY)
	xcodebuild -configuration Debug
	ln -sf $(CURDIR)/$(SERVER_PATH)/$(SERVER_BINARY) $(DEBUG_RESOURCES)/
	ln -sf $(CURDIR)/$(SERVER_PATH)/$(SERVER_WEB) $(DEBUG_RESOURCES)/

$(SERVER_PATH)/$(SERVER_BINARY):
	make -C $(SERVER_PATH)

clean:
	rm -rf build
