################################################################################
# Compiling TW-Light: make {debug=1}                                           #
#                          {xmingw=1}                                          #
#                          {win32=1}                                           #
#                          {DATAPATH=PATH}                                     #
#                          {EXECPATH=PATH}                                     #
#                          {TEST=1}        				       #
#                                                                              #
# Define win32=1    when compiling with Mingw32 gcc compiler for windows       #
# Define debug=1    when you want to build debug version of TimeWarp           #
# Define xmingw=1   when compiling  win32 binary with Mingw gcc crosscompiler  #
# Define DATAPATH   to specify where datafile stored (*nix version only)       #
# Define EXECPATH   to specify where executable should be placed               #
# Define TEST=1     to run unit test after compilation (require cppunit        #
#                   library). Default for debug configuration                  #
#                                                                              #
# Running just make builds the release version of TW-Light for *nix            #
# (Linux, FreeBSD, ...)                                                        #
#                                                                              #
# The game depends on Allegro (4.0.x), SDL, SDL_mixer with ogg support,        # 
# libraries, so you need to install them before running. cppunit 1.10.*        #
# required for TEST=1 configuration                                            #
#                                                                              #
################################################################################

PRODUCTVERSION = 0.3

SVNVERSION := ${shell svnversion .}

CC = gcc
CX = g++
RC = windres
LD = g++

CFLAGS = -fsigned-char -Wall -Wno-deprecated-declarations
OBJDIR = obj
NAME = tw-light

ifdef WINDIR
    win32 = 1
endif

ifdef  xmingw
    CX = i386-mingw32msvc-g++
    CC = i386-mingw32msvc-gcc
    RC = i386-mingw32msvc-windres
    LD = i386-mingw32msvc-g++
    win32 = 1
endif

ifndef DATAPATH
ifdef win32 
DATAPATH=gamedata
else
ifdef debug
DATAPATH=gamedata
else
DATAPATH=/usr/local/share/games/tw-light
endif
endif
endif

ifndef EXECPATH
ifndef win32
EXECPATH=/usr/local/bin
endif
endif

CFLAGS += -I./sources -I./source/libraries

ifdef DATAPATH
CFLAGS += -DDATAFILE_PATH=\"$(DATAPATH)\"
endif

CFLAGS += -DTW_MAJOR_VERSION=\"$(PRODUCTVERSION)\"
CFLAGS += -DTW_SVNVERSION=\"$(SVNVERSION)\"

VPATH = tests source source/libraries/agup source/ais source/games \
        source/melee source/tests source/xslt \
        source/other source/ships source/sc1ships source/sc2ships \
        source/util source/libraries/alogg \
        source/libraries/jpgalleg source/libraries/jgmod source/libraries/cppunit \
        source/libraries/raknet source/python source/generated 

#FILELIST := ${shell find source -type f "(" -name "*.c" -o -name "*.cpp" ")"}
FILELIST= ${shell cat sources.lst} 

BASE_NAMES = $(basename $(notdir $(FILELIST)))
POBJS = $(addsuffix .o,$(BASE_NAMES))
PDEPS = $(addsuffix .d,$(BASE_NAMES))


EXPDIR=tw-light-$(PRODUCTVERSION)r$(SVNVERSION)

#################################################################################################################
ifndef win32
	ARCH := $(shell echo `arch`)
	ifeq ($(ARCH),ppc)
		ARCH    = powerpc
endif
else
	ARCH := i586
endif

ifdef debug
	CFLAGS += -g -DDEBUGMODE
	OBJDIR := ${addsuffix -debug,$(OBJDIR)}
	NAME   := ${addsuffix -debug,$(NAME)}
	TEST=1
else
	CFLAGS += -Os -mcpu=$(ARCH) -s
endif

ifdef win32
	OBJDIR := ${addsuffix -win32,$(OBJDIR)}
	NAME := ${addsuffix .exe,$(NAME)}
	CFLAGS += -DWIN32 -I mingw-libs/include -I mingw-libs/include/SDL 
	LIBS += -L mingw-libs/lib -lalleg -lwinmm -lws2_32 -lSDL -lSDLmain -lpython24 -lz 
else
	TIMESTAMP = ${shell LC_TIME="POSIX" date +%c}
	EXPDIR=tw-light-$(PRODUCTVERSION)r$(SVNVERSION)
	CFLAGS += -DLINUX
	INCLUDES := ${shell allegro-config --cflags} ${shell sdl-config --cflags} 
	CFLAGS += $(INCLUDES)
	LIBS_CONFIG := ${shell allegro-config --libs} -pthread ${shell sdl-config --libs} ${shell python-config} 
	LIBS += $(LIBS_CONFIG) -lc -lXt -L/usr/X11R6/lib -lX11 -lm -lz
endif

CFLAGS += -I./source -I./source/libraries -I/usr/include/python2.3

ifdef TEST

ifdef win32
	CPPUNIT_LIBS := -lcppunit
else
	CPPUNIT_LIBS := ${shell cppunit-config --libs }
	CPPUNIT_INCLUDE := ${shell cppunit-config --cflags }
endif

CFLAGS += -DTW_CPPUNIT_TESTS $(CPPUNIT_INCLUDE)
LIBS   += $(CPPUNIT_LIBS)
endif

LIBS += -lSDL_mixer

OBJS = $(addprefix $(OBJDIR)/,$(POBJS))
DEPS = $(addprefix $(OBJDIR)/,$(PDEPS))

ifdef win32
ifndef debug
SUBSYSTEM=-Wl,--subsystem=windows
endif
endif
ifdef win32
OBJS += $(OBJDIR)/winicon.o
endif


##############################################################################

all: $(OBJDIR) $(OBJS) $(NAME)

$(OBJDIR):
	mkdir $(OBJDIR)


$(NAME): $(OBJDIR) $(OBJS)
	$(LD) $(CFLAGS) $(SUBSYSTEM) -o $@ $(OBJDIR)/*.o $(LIBS) 
	./$(NAME) -test


source/generated/ggob_wrap.cpp: source/games/ggob.i source/games/ggob.h
	swig -c++ -python -o source/generated/ggob_wrap.cpp source/games/ggob.i
	cp source/generated/ggob.py $(DATAPATH)/python/


$(OBJDIR)/%.o: %.cpp
	$(CX) -MMD $(CFLAGS) -c $< -o $@

$(OBJDIR)/%.o: %.c
	$(CC) -MMD $(CFLAGS) -c $< -o $@

$(OBJDIR)/winicon.o: tw-light.rc tw-light.ico
	$(RC) tw-light.rc -o $(OBJDIR)/winicon.o


clean:
	$(RM) $(OBJDIR)/*.o
	$(RM) $(OBJDIR)/*.d
	$(RM) $(NAME)

dist:
	svn export . $(EXPDIR)
	$(RM) $(EXPDIR)/web/*
	rmdir $(EXPDIR)/web
	$(RM) $(EXPDIR)/rawgraphic/*
	rmdir $(EXPDIR)/rawgraphic
	$(RM) -r $(EXPDIR)/doc/plot
	echo "TW-Light $(PRODUCTVERSION)r$(SVNVERSION) - $(TIMESTAMP)" >$(EXPDIR)/gamedata/version.txt
	echo "!define VER_VERSION $(PRODUCTVERSION)" >$(EXPDIR)/Util/ver
	echo "!define VER_SVNVERSION $(SVNVERSION)"  >$(EXPDIR)/Util/svnver
	tar -czf $(EXPDIR).tar.gz $(EXPDIR)
	cp tw-light.exe $(EXPDIR)/tw-light.exe
	makensis $(EXPDIR)/Util/timewarp.nsi

update_source_list:
	find source -type f "(" -name "*.c" -o -name "*.cpp" ")" >sources.lst

add_stacktrace:
	find source -name '*.cpp'|grep -v 'libraries'|grep -v 'errors.cpp'|grep -v 'utils.cpp'|grep -v 'frame.cpp'|xargs ./Util/addStackTrace.pl 

install:
	./install-sh -c -m 755 $(NAME) $(EXECPATH)/$(NAME)
	CPPROG="cp -r" ./install-sh -c gamedata $(DATAPATH)

uninstall:
	$(RM) $(EXECPATH)/$(NAME)
	$(RM) -r $(DATAPATH)


-include $(DEPS)
