
WINCHECK := $(shell which msys-1.0.dll >/dev/null 2>&1; if [ $$? -eq 0 ]; then echo MSYS; fi)
ifeq ($(strip $(WINCHECK)),)
WINDOWS  := 0
else
WINDOWS  := 1
endif

TARGET   := anaglym
COBJS    := $(patsubst %.c,%.o,$(wildcard *.c))
CXXOBJS  := $(subst sdl_keymap.o,,$(patsubst %.cc,%.o,$(wildcard *.cc))) sdl_keymap.o
OBJS     := $(COBJS) $(CXXOBJS)
CDEPS    := $(COBJS:.o=.dep)
CXXDEPS  := $(CXXOBJS:.o=.dep)
DEPS     := $(CDEPS) $(CXXDEPS)

LUAINCLUDE := $(shell which lua-config >/dev/null 2>&1; if [ $$? -eq 0 ]; then lua-config --include; fi)
ifeq ($(strip $(LUAINCLUDE)),)
LUAINCLUDE := -I/usr/include/lua5.1
endif
LUALIBS  := $(shell which lua-config >/dev/null 2>&1; if [ $$? -eq 0 ]; then lua-config --libs; fi)
ifeq ($(strip $(LUALIBS)),)
LUALIBS  := -llua5.1
endif

FTGLINCLUDE := $(shell pkg-config --cflags ftgl)
FTGLLIBS    := $(shell pkg-config --libs ftgl)

SDLINCLUDE  := $(shell sdl-config --cflags)
SDLLIBS     := $(shell sdl-config --libs) -lSDL_image

ODEINCLUDE  := $(shell ode-config --cflags)
ODELIBS     := $(shell ode-config --libs)

TOPLEVEL    := $(shell pwd)

ifeq ($(WINDOWS),1)
GLLIBS      := -lopengl32 -lglu32
WINDOWSLIBS := -lmingw32
TARGET      := $(TARGET).exe
else
GLLIBS      := -lGL -lGLU
endif

export CPPFLAGS := $(LUAINCLUDE) $(SDLINCLUDE) $(ODEINCLUDE) $(FTGLINCLUDE) -I$(TOPLEVEL)
export CFLAGS   := $(CPPFLAGS) -O2 -Wall
export CXXFLAGS := $(CFLAGS)
export LDFLAGS  := $(LUALIBS) $(ODELIBS) $(GLLIBS) $(WINDOWSLIBS) $(SDLLIBS) $(FTGLLIBS)

ifdef DEBUG
	CFLAGS += -g
	CXXFLAGS += -g
endif

all: $(TARGET)

$(TARGET): $(OBJS) wfobj/WFObj.o OdeWorld/OdeWorld.o TextureCache/TextureCache.o
	$(CXX) -o $@ $^ $(LDFLAGS)

.PHONY: wfobj/WFObj.o
wfobj/WFObj.o:
	$(MAKE) -C wfobj

.PHONY: OdeWorld/OdeWorld.o
OdeWorld/OdeWorld.o:
	$(MAKE) -C OdeWorld

.PHONY: TextureCache/TextureCache.o
TextureCache/TextureCache.o:
	$(MAKE) -C TextureCache

%.o: %.c
	$(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $<

%.o: %.cc
	$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<

sdl_keymap.o: sdl_keymap.cc

sdl_keymap.cc: gen-sdl-keymap.pl
	perl $<

# Make dependency files
%.dep: %.c
	@set -e; rm -f $@; \
	  $(CC) -MM $(CPPFLAGS) $< | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' > $@

%.dep: %.cc
	@set -e; rm -f $@; \
	  $(CXX) -MM $(CPPFLAGS) $< | sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' > $@

.PHONY: dist
dist: all
	-mkdir -p $@
	svn export lib $@/lib
	svn export doc $@/doc
	cp $(TARGET) $@
ifeq ($(WINDOWS),1)
# copy DLLs to distribution directory
	cp -a $(shell which SDL.dll) $@
	cp -a $(shell which SDL_image.dll) $@
# jh 2009-10-29 statically linking with ODE now
#	cp -a $(shell which ODE.dll) $@
	cp -a $(shell which jpeg.dll) $@
	cp -a $(shell which libpng12-0.dll) $@
	cp -a $(shell which zlib1.dll) $@
	cp -a $(shell which libtiff-3.dll) $@
	cp -a $(shell which libftgl-2.dll) $@
	cp -a $(shell which lua5.1.dll) $@
	cp -a $(shell which freetype6.dll) $@
endif

clean:
	-rm -f *.o *.dep *~ $(TARGET) dist stdout.txt stderr.txt sdl_keymap.h sdl_keymap.cc
	$(MAKE) -C wfobj clean
	$(MAKE) -C TextureCache clean
	$(MAKE) -C OdeWorld clean

-include $(DEPS)
