DEPTH = .
SUBDIR = .
PROGNAME = FreeDoko

# for Windows under Linux:
# export OSTYPE=Linux_to_Windows

include $(DEPTH)/Makefile.os

include Makefile.objects

DIRECTORIES = party game player card misc ui os class utils text network
OBJECTS_ALL = $(OBJECTS) \
		$(OBJECTS_PARTY) $(OBJECTS_GAME) $(OBJECTS_PLAYER) \
		$(OBJECTS_CARD) $(OBJECTS_MISC) \
		$(OBJECTS_UI) $(OBJECTS_OS) \
		$(OBJECTS_CLASS) $(OBJECTS_UTILS)
INCLUDE_ALL = $(INCLUDE)
LIBS_ALL = $(LIBS)
ifeq ($(USE_UI_GTKMM), true)
	OBJECTS_ALL += $(OBJECTS_GTKMM)
	INCLUDE_ALL += $(INCLUDE_GTKMM)
	LIBS_ALL += $(LIBS_GTKMM)
endif
ifeq ($(USE_NETWORK), true)
	OBJECTS_ALL += $(OBJECTS_NETWORK)
	INCLUDE_ALL += $(INCLUDE_NETWORK)
	LIBS_ALL += $(LIBS_NETWORK)
endif


.PHONY: all
all :
	$(MAKE) check
	$(MAKE) $(PROGNAME)

.PHONY: check
check : os_check libraries_check

#	The following is a check, whether the ostype was detected correctly
.PHONY: os_check
os_check :
ifeq ($(OPERATING_SYSTEM), Windows)
 ifneq ($(OSTYPE), Cygwin)
	@REM \
	  ; (echo -e \\n\
	  	      "***** Error *****\\n"\
	              "If you see these lines, the operating system was not detected correctly.\\n"\
	              "Detected operating system: $(OPERATING_SYSTEM)\\n"\
	              "Try an 'export OSTYPE' before compiling.\\n"\
	      && false)
 endif
else
	@# echo "***** Error ***** If you see this line, the operating system was not detected correctly. Detected operating system: $(OPERATING_SYSTEM)"
endif

#	The following is a check, whether the needed libraries are installed
.PHONY: libraries_check
libraries_check :
ifeq ($(SHELLTYPE), sh)
  ifeq ($(USE_UI_GTKMM), true)
	@pkg-config --exists gtkmm-2.4 \
	  || (echo "gtkmm 2.4 (user interface) is not found."; \
	      export problem=true)
  endif
  ifeq ($(USE_NETWORK), true)
	@pkg-config --exists glib-2.0 \
	  || echo "glib 2.0 (needed for gnet) is not found."
	@pkg-config --exists gnet-2.0 \
	  || echo "gnet 2.0 (network library) is not found."
  endif
endif
ifeq ($(SHELLTYPE), COMMAND.COM)
#       ToDo
endif

.PHONY: watch
watch :
	# Does not work, too many files?
	# auto_execute -e 'nice make -k' `find . -name "*.cpp" -o -name "*.h"`
	rm -f commands.sh
	echo '#!/bin/bash' >> commands.sh
	echo 'if [ -e ./FreeDoko ]; then' >> commands.sh
	echo '  for f in $$(find . -name "*.cpp"); do' >> commands.sh
	echo '    if [ $$f -nt ./FreeDoko ]; then rm ./FreeDoko; break; fi' >> commands.sh
	echo '  done' >> commands.sh
	echo 'fi' >> commands.sh
	echo 'nice -n 5 make -s -k' >> commands.sh
	auto_execute -e 'bash ./commands.sh' `find . -name "*.cpp"`
	rm -f commands.sh

seed.out : $(PROGNAME)
	./$(PROGNAME) > seed.out

.PHONY: show_flags
show_flags :
	@echo 'Flags:'
	@echo '  CXX     : $(CXX)'
	@echo '  CPPFLAGS: $(CPPFLAGS)'
	@echo '  CXXFLAGS: $(CXXFLAGS)'
	@echo '  INCLUDE : $(INCLUDE_ALL)'
	@echo '  LIBS    : $(LIBS_ALL)'

$(PROGNAME) : show_flags objects $(DIRECTORIES)
	@echo "compiling $(PROGNAME)..."
# Gentoo users do want to see the real compile line.
# So remove the next five lines (the echo lines)
# and remove the '@' in the line after.
	@echo '$(CXX) $(CXXFLAGS)'
	@echo '        $$(OBJECTS)'
	@echo '        $(INCLUDE_ALL)'
	@echo '        $(LIBS_ALL)'
	@echo '        -o $@'
	@$(CXX) $(CXXFLAGS) \
	  $(OBJECTS_ALL) \
	  $(INCLUDE_ALL) \
	  $(LIBS_ALL) \
	  -o $@
	@echo "$(PROGNAME) compiled successfully"

.PHONY: release_bin
release_bin :
ifeq ($(SHELLTYPE), sh)
	if [ ! -e Makefile.local.bak ]; then\
	  mv Makefile.local Makefile.local.bak; \
	fi
	echo "CPPFLAGS += -DRELEASE" \
	  > Makefile.local
	echo "CPPFLAGS += -DVERSION_DESCRIPTION='\"$(OPERATING_SYSTEM) binary\"'" \
	  >> Makefile.local
	echo "CXXFLAGS = -Wall -Werror -Wno-unused-value -pipe -O2" \
	  >> Makefile.local
	$(MAKE) $(PROGNAME)
	strip $(PROGNAME)
	mv Makefile.local.bak Makefile.local
endif
ifeq ($(SHELLTYPE), COMMAND.COM)
	$(RM) Makefile.local.bak
	ren Makefile.local Makefile.local.bak
	echo override CPPFLAGS += -DRELEASE -DVERSION_DESCRIPTION='"Microsoft Windows binary"' \
	> Makefile.local
	$(MAKE) $(PROGNAME)
	strip $(PROGNAME).exe
	$(RM) Makefile.local
	ren Makefile.local.bak Makefile.local
endif

.PHONY: one_compile
one_compile : show_flags
	@echo "compiling $(PROGNAME)..."
	# Gentoo users do want to see the real compile line.
	# So remove the following echo lines
	# and remove the '@' in the lines after.
	@echo '$(CXX) $(CPPFLAGS)'
	@echo '        $$(OBJECTS).cpp'
	@echo '        $(INCLUDE_ALL)'
	@echo '        -E -o $(PROGNAME).ii'
	@$(CXX) $(CPPFLAGS) \
	$(patsubst %.o,%.cpp,$(OBJECTS_ALL)) \
	$(INCLUDE_ALL) \
	-E > FreeDoko.ii
	@echo '$(CXX) $(CXXFLAGS)'
	@echo '        -c $(PROGNAME).ii'
	# errors
	@$(CXX) $(CXXFLAGS) -c -x c++-cpp-output $(PROGNAME).ii
	# $(LIBS_ALL)
	#-o $(PROGNAME)
	@echo '$(CXX) $(LIBS_ALL)'
	@echo '       $(PROGNAME).o'
	@echo '        -o $(PROGNAME)'
	@$(CXX) $(LIBS_ALL) $(PROGNAME).o -o $(PROGNAME)
	@echo "$(PROGNAME) compiled successfully"

ifeq ($(SHELLTYPE), sh)
.PHONY: fast
fast :
	nice $(MAKE) -k -C ui &
	( nice $(MAKE) -k -C party; \
	nice $(MAKE) -k -C game; \
	nice $(MAKE) -k -C card ) &
	( nice $(MAKE) -k -C player; \
	nice $(MAKE) -k objects; \
	nice $(MAKE) -k -C misc; \
	nice $(MAKE) -k -C os; \
	nice $(MAKE) -k -C class; \
	nice $(MAKE) -k -C utils )
endif

.PHONY : objects
objects : $(OBJECTS)

ifeq ($(OPERATING_SYSTEM), Linux)
.PHONY : doc
doc :
	doxygen

.PHONY : tags
tags :
	ctags -R
endif

include $(DEPTH)/Makefile.subdir
include $(DEPTH)/Makefile.rules
