# OPTIONS.
DEBUG := 1
#DEBUG_PARANOID := 1

OS := LINUX
#OS := WIN32
#OS := MACOS
export OS

# Data path.
#DATA_DEF := \"ldata\"

#CC = clang

# VERSION.
VMAJOR			:= 0
VMINOR			:= 3
VREV				:= 3
VERSION			= -DVMAJOR=$(VMAJOR) -DVMINOR=$(VMINOR) -DVREV=$(VREV)
VERSIONFILE	= VERSION

# OBJECTS.
APPNAME := Lephisto
OBJS		:= $(patsubst %.c, %.o, $(wildcard ../src/*.c))

# CFLAGS
CLUA := -I../lib/lua
CSDL := $(shell sdl-config --cflags)
CXML := $(shell xml2-config --cflags)
CTTF := $(shell freetype-config --cflags)
CPNG := #$(shell libpng-config libpng --cflags)
CGL  :=
CFLAGS := $(CLUA) $(CSDL) $(CXML) $(CTTF) $(CPNG) $(CGL) $(VERSION) -D$(OS)
ifdef DATA_DEF
CFLAGS += -DDATA_DEF=$(DATA_DEF)
endif

# OS Stuff.
ifeq ($(OS),LINUX)
CFLAGS += -D_POSIX_SOURCE
endif

# Debug stuff.
ifdef DEBUG
CFLAGS += -W -Wall -Wextra -Wunused -Wshadow -Wpointer-arith -Wmissing-prototypes \
					-Winline -Wcast-align -Wmissing-declarations -fstack-protector \
					-fstack-protector-all -g -DDEBUG -DLUA_USE_APICHECK -std=c99

ifdef DEBUG_PARANOID
CFLAGS += -DDEBUG_PARANOID
endif

# Handle OS Debug stuff here.
ifeq ($(OS), LINUX)
LDFLAGS += -rdynamic
endif # Linux.

else # DEBUG
CFLAGS += -O2 -funroll-loops -pipe -std=c99
endif

# LDFLAGS.
LDLUA := ../lib/lua/liblua.a
LDSDL := $(shell sdl-config --libs) -lSDL_image -lSDL_mixer -lpng
LDXML := $(shell xml2-config --libs)
LDTTF := $(shell freetype-config --libs)
CPNG 	:= #$(shell libpng-config libpng --libs)
LDGL	:= -lGL
LDFLAGS := -lm $(LDLUA) $(LDSDL) $(LDXML) $(LDTTF) $(CPNG) $(LDGL) $(LDPNG)

# This is just for gstat to run some analysis on performance.
ifdef DEBUG
LDFLAGS += -pg
endif

# DATA.
DATA_AI   := 	$(wildcard ../scripts/ai/*.lua \
										../scripts/ai/include/*.lua \
										../scripts/ai/tpl/*.lua)
DATA_GFX  := 	$(wildcard ../gfx/*.png \
										../gfx/loading/*.png \
										../gfx/gui/*.png \
										../gfx/logo/*.png \
										../gfx/outfit/space/*.png \
										../gfx/outfit/store/*.png \
										../gfx/planet/exterior/*.png \
										../gfx/planet/space/*.png \
										../gfx/ship/*.png \
										../gfx/spfx/*.png)
DATA_XML  := 	$(wildcard ../dat/*.xml ../dat/*.ttf ../dat/intro)
DATA_SND  := 	$(wildcard ../snd/music/*.ogg \
										../snd/sounds/*.wav) ../snd/music.lua
DATA_MISN := 	$(wildcard ../dat/missions/*.lua)
DATA      := 	ldata
DATAFILES := 	$(DATA_AI) $(DATA_GFX) $(DATA_XML) $(DATA_SND) $(DATA_MISN)

# TARGETS.
.PHONY: all help lua utils docs clean distclean

%.o: %.c %.h
	@$(CC) -c $(CFLAGS) -o $@ $<
	@echo "		CC $@"

all: utils ldata lua Lephisto

help:
	@echo "Possible targets are:":
	@echo "		lua 			- Builds Lua support."
	@echo "		lephisto 	- Builds the Lephisto binary."
	@echo "		mkspr			- Builds the mkspr utility."
	@echo "		ldata			- Creates the ldata file."
	@echo "		utils			- Builds all the utilities."
	@echo "		docs			- Creates the doxygen documentation."
	@echo "		clean			- Removes Lephisto's main binary and ldata file."
	@echo " 	distclean	- Removes everything done."

$(APPNAME): $(OBJS)
	@$(CC) $(LDFLAGS) -o $(APPNAME) $(OBJS) ../lib/lua/liblua.a
	@echo "		LD $(APPNAME)"

lua:
	@if [ ! -e ../lib/lua/liblua.a ]; then $(MAKE) -C ../lib/lua a; fi


pack: ../src/md5.c ../src/pack.c ../utils/pack/main.c
	@$(MAKE)  -C ../utils/pack

mksprite: ../utils/mkspr/main.c
	@$(MAKE) -C ../utils/mkspr

$(VERSIONFILE):
	@echo -n "$(VMAJOR).$(VMINOR).$(VREV)" > $(VERSIONFILE)

ldata: pack $(DATAFILES)
	@echo -n "$(VMAJOR).$(VMINOR).$(VREV)" > $(VERSIONFILE)
	@echo "		Creating ldata..\n"
	@./pack $(DATA) $(DATAFILES) $(VERSIONFILE)

utils: pack mksprite

docs:
	@(cd docs/doxygen; doxygen)

clean:
	@echo "		Removing ldata"
	@$(RM) $(DATA)
	@echo "		Removing object files"
	@$(RM) $(OBJS)
	@echo "		Removing main binary ($(APPNAME))"
	@$(RM) $(APPNAME)

distclean: clean
	@echo "		Cleaning utilites"
	@$(MAKE) -C ../utils/pack clean
	@$(MAKE) -C ../utils/mkspr clean
	@echo "		Cleaning Lua"
	@$(MAKE) -C ../lib/lua clean
	@echo "		Removing build tool binaries."
	@$(RM) Lephisto mksprite ldata pack gmon.out VERSION

