.PHONY: clean release gtk

CC=gcc
CXX=g++
LINK=g++

FEATURES += CLUSTER_INSIDES
FEATURES += NEW_LIGHT_FORMAT
FEATURES += ZLIB
FEATURES += ZLIBW
#FEATURES += EYE_CANDY # Unstable

PLATFORM=-march=native
CWARN=-Wall -Wdeclaration-after-statement -Werror
CXXWARN=-Wall
EXTRA_INCLUDES=

OPTIONS = -DLINUX -DMAP_EDITOR $(foreach FEATURE, $(FEATURES), -D$(FEATURE)) -D_7ZIP_ST \
	$(shell pkg-config gtk+-2.0 --cflags) -DGTK2 \
	$(shell pkg-config sdl --cflags) \
	$(shell pkg-config SDL_image --cflags) \
	$(shell pkg-config libxml-2.0 --cflags) \
	$(shell pkg-config cal3d --cflags) \
	$(shell pkg-config gl --cflags) \
	$(shell pkg-config glu --cflags) \
	$(shell pkg-config zlib --cflags)

CFLAGS=$(PLATFORM) $(CWARN) -O0 -ggdb -pipe $(OPTIONS) -fno-strict-aliasing $(EXTRA_INCLUDES)

CXXFLAGS=$(PLATFORM) $(CXXWARN) -O0 -ggdb -pipe $(OPTIONS) -fno-strict-aliasing $(EXTRA_INCLUDES)

LDFLAGS=$(shell pkg-config sdl --libs) \
	$(shell pkg-config gtk+-2.0 --libs) \
	$(shell pkg-config libxml-2.0 --libs) \
	$(shell pkg-config SDL_image --libs) \
	$(shell pkg-config cal3d --libs) \
	$(shell pkg-config gl --libs) \
	$(shell pkg-config glu --libs) \
	$(shell pkg-config zlib --libs) \
	$(shell pkg-config x11 --libs) -lm -lstdc++

_CFLAGS=$(PLATFORM) -O2 -fomit-frame-pointer -ffast-math -pipe $(OPTIONS) -fno-strict-aliasing $(EXTRA_INCLUDES)
_LDFLAGS=$(XDIR) -lGL -lpthread -lstdc++
_CXXFLAGS=$(PLATFORM) -O3 -fomit-frame-pointer -ffast-math -pipe $(OPTIONS) -fno-strict-aliasing $(EXTRA_INCLUDES)

COBJS=2d_objects.o 3d_objects.o	\
	browser.o draw_scene.o	\
	e3d.o elwindows.o events.o \
	font.o frustum.o	\
	gl_init.o global.o gui.o gui_callbacks.o	\
	init.o interface.o lights.o \
	main.o map_io.o misc.o o3dow.o reflection.o	\
	shadows.o tile_map.o \
	replace_window.o edit_window.o particles_window.o

CLUSTER_INSIDES_ELC_COBJS = cluster.o

ELC_COBJS = asc.o colors.o elconfig.o errors.o load_gl_extensions.o md5.o \
	dds.o ddsimage.o el_memory.o particles.o queue.o textures.o translate.o hash.o \
	image.o image_loading.o cache.o \
	$(foreach FEATURE, $(FEATURES), $($(FEATURE)_ELC_COBJS))

ELC_IO_COBJS = fileutil.o e3d_io.o elc_io.o elpathwrapper.o half.o normal.o	\
	elfilewrapper.o unzip.o ioapi.o zip.o ziputil.o

ELC_EXCEPTION_CXXOBJS = extendedexception.o

EYE_CANDY_CXXOBJS = eye_candy_window.o

CXXOBJS = $(foreach FEATURE, $(FEATURES), $($(FEATURE)_CXXOBJS))

EYE_CANDY_ELC_CXXOBJS = eye_candy_wrapper.o

ELC_CXXOBJS = elloggingwrapper.o cal3d_wrapper.o \
	$(foreach FEATURE, $(FEATURES), $($(FEATURE)_ELC_CXXOBJS))

ELC_ENGINE_CXXOBJS = hardwarebuffer.o logging.o

EYE_CANDY_ELC_EC_CXXOBJS = effect_bag.o effect_breath.o effect_campfire.o \
	effect_cloud.o effect_candle.o effect_firefly.o effect_fountain.o \
	effect_glow.o effect_harvesting.o effect_impact.o effect_lamp.o \
	effect_ongoing.o effect_selfmagic.o effect_smoke.o \
	effect_summon.o effect_sword.o effect_targetmagic.o effect_teleporter.o\
	effect_wind.o eye_candy.o math_cache.o

ELC_EC_CXXOBJS = $(foreach FEATURE, $(FEATURES), $($(FEATURE)_ELC_EC_CXXOBJS))

ELC_XZ_CCOBJS = 7zCrc.o 7zCrcOpt.o Alloc.o Bra86.o Bra.o BraIA64.o \
	CpuArch.o Delta.o LzFind.o Lzma2Dec.o Lzma2Enc.o LzmaDec.o LzmaEnc.o \
	Sha256.o Xz.o XzCrc64.o XzDec.o XzEnc.o

OBJS = $(COBJS) $(CXXOBJS) $(ELC_COBJS) $(ELC_CXXOBJS) $(ELC_IO_COBJS) $(ELC_IO_CXXOBJS) \
	$(ELC_EC_CXXOBJS) $(ELC_EXCEPTION_CXXOBJS) 	$(ELC_ENGINE_CXXOBJS) $(ELC_XZ_CCOBJS)

DEP_FILES=$(foreach OBJ, $(COBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_COBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_IO_COBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_IO_CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_EXCEPTION_CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_EC_CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_ENGINE_CXXOBJS), .deps/$(OBJ).P) \
	$(foreach OBJ, $(ELC_XZ_CCOBJS), .deps/$(OBJ).P)

EXE=mapedit.x86.linux.bin

GENDEP=Makefile.linux

all: $(EXE)

$(EXE): $(OBJS)
	@echo "  LINK $(EXE)"
	@$(LINK) $(CFLAGS) -o $(EXE) $(OBJS) $(LDFLAGS)

release:
	@$(MAKE) -f Makefile.linux 'CFLAGS=$(_CFLAGS)' 'CXXFLAGS=$(_CXXFLAGS)'

DEPS_MAGIC := $(shell mkdir .deps > /dev/null 2>&1 || :)

$(COBJS): %.o: %.c $(GENDEP)
	@echo "  CC   $@"
	@if $(CC) $(CFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(CXXOBJS): %.o : %.cpp $(GENDEP)
	@echo "  CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_COBJS): %.o : ../%.c $(GENDEP)
	@echo "  ELC CC   $@"
	@if $(CC) $(CFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_IO_COBJS): %.o : ../io/%.c $(GENDEP)
	@echo "  ELC IO CC   $@"
	@if $(CC) $(CFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_IO_CXXOBJS): %.o : ../io/%.cpp $(GENDEP)
	@echo "  ELC IO CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_EXCEPTION_CXXOBJS): %.o : ../exceptions/%.cpp $(GENDEP)
	@echo "  ELC EXC CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_CXXOBJS): %.o : ../%.cpp $(GENDEP)
	@echo "  ELC CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_EC_CXXOBJS): %.o : ../eye_candy/%.cpp $(GENDEP)
	@echo "  ELC EC CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_ENGINE_CXXOBJS): %.o : ../engine/%.cpp $(GENDEP)
	@echo "  ELC ENG CXX   $@"
	@if $(CXX) $(CXXFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

$(ELC_XZ_CCOBJS): %.o : ../xz/%.c $(GENDEP)
	@echo "  ELC XZ CC   $@"
	@if $(CC) $(CFLAGS) -MT '$@' -MD -MP -MF '.deps/$@.pp' -c $< -o $@; then \
		mv ".deps/$@.pp" ".deps/$@.P"; \
	else rm -f ".deps/$@.pp"; exit 1; \
	fi

clean:
	-rm -f $(OBJS) $(EXE)

.depend: $(foreach HEADER_DIR, $(HEADER_DIRS), $(wildcard $(HEADER_DIR)/*.h))
	$(CC) $(CFLAGS) -MM $(patsubst %.o, %.c, $(COBJS)) >.depend
	$(CXX) $(CXXFLAGS) -MM $(patsubst %.o, %.cpp, $(CXXOBJS)) >>.depend

# Based on the main client static build, this works on Ubuntu 8.04 (LTS) at least
# Its only a little bit static but it may work
# Install these packages in addition to those required for a standard build
# libasound2-dev libaudio-dev libartsc0-dev libesd0-dev libpulse-dev libaa1-dev libgpmg1-dev libdirectfb-dev
LIBD=/usr/lib
STATICLIBS=$(LIBD)/libSDL_net.a $(LIBD)/libSDL.a $(LIBD)/libSDL_image.a \
	$(LIBD)/libopenal.a $(LIBD)/libvorbisfile.a $(LIBD)/libvorbis.a \
	$(LIBD)/libogg.a $(LIBD)/libxml2.a $(LIBD)/libcal3d.a \
	$(LIBD)/libpng.a $(LIBD)/libasound.a $(LIBD)/libaudio.a \
	$(LIBD)/libartsc.a $(LIBD)/libesd.a $(LIBD)/libpulse-simple.a \
	$(LIBD)/libpulse.a $(LIBD)/libaa.a $(LIBD)/libgpm.a \
	$(LIBD)/libcurses.a $(LIBD)/libslang.a $(LIBD)/libdirectfb.a \
	$(LIBD)/libdirect.a $(LIBD)/libfusion.a $(LIBD)/librt.a \
	$(LIBD)/libtiff.a $(LIBD)/libjpeg.a $(LIBD)/libGLU.a $(LIBD)/libz.a \
	-lGL

static: $(OBJS)
	@$(MAKE) -f Makefile.linux 'LIBS=$(STATICLIBS)'

-include $(DEP_FILES)
