#ARCH = MACOSX
ARCH = LINUX
#ARCH = MINGW32

PREFIX ?= /usr/local

ifeq "$(ARCH)" "MINGW32"
include makeinclude.mingw32
ARCH_OK = yes
endif

ifeq "$(ARCH)" "LINUX"
include makeinclude.linux
ARCH_OK = yes
endif

ifeq "$(ARCH)" "MACOSX"
include makeinclude.macosx
ARCH_OK = yes
endif

ifeq "$(ARCH_OK)" "yes"
#
# The given ARCH is correct
#

#
# Variables
#

CXXFLAGS = -g $(ARCHCXXFLAGS) -DENABLE_SOUND
LDFLAGS = -O2 $(ARCHLDFLAGS)

#
# List of all source files.
#
# Architecture-dependend files as defined in the makeinclude files are added
# automatically.
#
SOURCES = \
src/Loader.cxx \
src/Screen.cxx \
src/Texture.cxx \
src/main.cxx \
$(ARCHSOURCES)

# 
# The objects are named like the source files, but instead of in directory
# src/, they are placed in directory obj/, and the architecture gets added
# to the name, e.g. obj/main.LINUX.o 
#
OBJECTS=$(addsuffix .$(ARCH).o,$(basename $(subst src/,obj/,$(SOURCES))))

# 
# The dependency files are named like the source files, but instead of in
# directory src/, they are placed in directory obj/, and the architecture
# gets added to the name, e.g. dep/main.LINUX.d 
#
DEPENDS=$(addsuffix .$(ARCH).d,$(basename $(subst src/,dep/,$(SOURCES))))

#
# We need to create the directories for object and dependency files
# automatically. The following variables are lists of dummy files,
# one for each directory, and are used to in the rule to create the
# directories.
#
OBJECTDIRS=$(addsuffix .dir,$(sort $(dir $(OBJECTS))))
DEPENDDIRS=$(addsuffix .dir,$(sort $(dir $(DEPENDS))))

# 
# Targets / Rules
# 

#
# First target, depends on the target that builds the application
#
all: $(APP) README

#
# Targets / rules to make sure the directories for the object and
# dependency files exist.
#
.PRECIOUS: $(OBJECTDIRS) $(DEPENDDIRS)

%.dir:
	@mkdir $(dir $@)
	@touch $@

#
# Include the dependencies, but only if we are not doing a
# make clean or make depend
#
ifneq "$(MAKECMDGOALS)" "clean"
ifneq "$(MAKECMDGOALS)" "depend"
ifneq "$(MAKECMDGOALS)" ".depend"
-include $(DEPENDS)
endif
endif
endif

#
# Rule to build the application executable
#
$(APP): $(OBJECTS) $(ARCHAPPREQ)
	$(CXX) $(OBJECTS) $(CXXFLAGS) $(LDFLAGS) -o $@

#
# MacOSX applications go into a bundle. The rule creates
# the bundle directory structure and populates it
#
macosx_app: Info.plist
	mkdir -p jigzo.app/Contents/MacOS/
	mkdir -p jigzo.app/Contents/Resources/
	cp Info.plist jigzo.app/Contents/

#
# Compilation rules
#
obj/%.$(ARCH).o: src/%.c $(OBJECTDIRS)
	@echo Compiling $<
	$(CC) $(CXXFLAGS) -Wall -Weffc++ -c $< -o $@

obj/%.$(ARCH).o: src/%.cxx $(OBJECTDIRS)
	@echo Compiling $<
	$(CXX) $(CXXFLAGS) -Wall -c $< -o $@

obj/%.$(ARCH).o: src/%.cpp $(OBJECTDIRS)
	@echo Compiling $<
	$(CXX) $(CXXFLAGS) -Wall -c $< -o $@

#
# Dependency rules
#
dep/%.$(ARCH).d: src/%.c $(DEPENDDIRS)
	@echo Generating dependency information for $<
	$(CXX) -MM -MT $@ $(CXXFLAGS) $< -o $@.tmp
	@(cat $@.tmp ; cat $@.tmp | sed s/^dep/obj/ | sed s/\.d:/.o:/ ) > $@
	@rm $@.tmp

dep/%.$(ARCH).d: src/%.cxx $(DEPENDDIRS)
	@echo Generating dependency information for $<
	$(CXX) -MM -MT $@ $(CXXFLAGS) $< -o $@.tmp
	@(cat $@.tmp ; cat $@.tmp | sed s/^dep/obj/ | sed s/\.d:/.o:/ ) > $@
	@rm $@.tmp

dep/%.$(ARCH).d: src/%.cpp $(DEPENDDIRS)
	@echo Generating dependency information for $<
	$(CXX) -MM -MT $@ $(CXXFLAGS) $< -o $@.tmp
	@(cat $@.tmp ; cat $@.tmp | sed s/^dep/obj/ | sed s/\.d:/.o:/ ) > $@
	@rm $@.tmp

#
# Rules to force a rebuild of all dependency information
#
depend:
	@rm -f $(DEPENDS)
	@make ARCH=$(ARCH) .depend

.depend: $(DEPENDS)

#
# Rule to clean all generated files (objects, dependencies, executable)
#
clean:
	rm -rf obj/ dep/ $(APP) 

ifeq "$(ARCH)" "LINUX"

install: jigzo
	@echo Installing in $(PREFIX)
	rm -rf "$(PREFIX)/lib/jigzo/"
	mkdir -p "$(PREFIX)/lib/jigzo/"
	cp -r font image sound puzzles "$(PREFIX)/lib/jigzo/"
	cp jigzo $(PREFIX)/lib/jigzo/jigzo.bin
	echo -e '#!/bin/sh\n$(PREFIX)/lib/jigzo/jigzo.bin $$*\n' > $(PREFIX)/bin/jigzo
	chmod +x $(PREFIX)/bin/$(APP)

endif

#
# Rules end here
#
else
#
# The given ARCH is incorrect or missing
#

all: .DEFAULT

clean:
	rm -rf obj/ dep/ $(APP) 

.DEFAULT:
ifeq "$(ARCH)" ""
	@echo '#################################################################'
	@echo 'ARCH not defined. Use make ARCH=[ LINUX | MACOSX | MINGW32 ]'
	@echo 'or set the environment variable'
	@echo '#################################################################'
else
	@echo '#################################################################'
	@echo 'ARCH $(ARCH) invalid. Use make ARCH=[ LINUX | MACOSX | MINGW32 ]'
	@echo 'or set the environment variable'
	@echo '#################################################################'
endif

endif

README: index.html
	lynx -dump index.html | sed 's/^          *//' | sed 's/^   \([^ ]\)/      \1/g' | sed 's/\[[0123456789]\]//' | grep -v References | grep -v '^  *.\. ....://' > README


###### DISTRIBUTABLE_UNTIL_HERE ######
