#######################################################################################################
# Configure guessed variables ...
#######################################################################################################

CC=x86_64-alt-linux-gcc
CFLAGS=-O2
CPPFLAGS=
# MKDIR_P=@MKDIR_P@
WINDRES=
LIBS=-ldl 
LDFLAGS=

TARGET_OS=linux-gnu
TARGET_CPU=x86_64
EXE_EXT=

SDL_CFLAGS=-I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT
SDL_LIBS=-lSDL -lpthread

CLIENT_LIBS=  -ljpeg -lpng -lSDL_ttf -lSDL_mixer  -lz -lcurl -lm  -ldl 
SERVER_LIBS=  -lcurses  -lz -lcurl -lm  -ldl 
GAME_LIBS=  -lm 
TOOLS_LIBS=  -ljpeg -lz -lm 
MASTER_LIBS=
LIBDIR=/usr/lib64
CFLAGS   += -DLIBDIR='"$(LIBDIR)/ufoai"'

MMX=0

PROFILING=0
PARANOID=
BUILD_CLIENT=1
BUILD_MASTER=1
BUILD_DEDICATED=1
BUILD_UFO2MAP=1
BUILD_DEBUG=1

HAVE_SHADERS=

# assign parameters from configure
prefix=/usr
exec_prefix=/usr
BINDIR=/usr/bin
datarootdir=@datarootdir@
DATADIR=/usr/share/ufoai
INSTALL=./install-sh

VERSION=$(shell grep UFO_VERSION src/common/common.h | sed -e 's/.*UFO_VERSION \"\(.*\)\"/\1/')

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

.PHONY: all all2 clean echo_info

include build/platform_specific.mk
include build/defs.mk

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

# define all the modules that need to be compiled
MODULES = client server tools game sound masterserver

all: all2

# include the make files for each module
-include $(patsubst %, build/%.mk, $(MODULES))

ALL_DEPS = $(patsubst %.o, %.d, $(ALL_OBJS))

all2: Makefile $(TARGETS) echo_info

# check that the makefile is up to date
Makefile: Makefile.in
	@echo "ERROR: You need to re-run configure"
	@exit 1

# Say how to build the build directories
$(BUILDDIR)/.dirs:
	@mkdir -p $(foreach i, $(ALL_OBJS), $(dir $i))
	@touch $@

clean:
	@echo "Making clean"
	@rm -f $(ALL_OBJS) $(ALL_DEPS) $(BUILDDIR)/.dirs $(TARGETS)

echo_info:
	@echo "CFLAGS"
	@echo "-----------------------"
	@echo $(CFLAGS)
	@echo $(CLIENT_CFLAGS) $(SDL_CFLAGS)
	@echo
	@echo "LDFLAGS"
	@echo "-----------------------"
	@echo $(LDFLAGS)
	@echo $(CLIENT_LIBS) $(SDL_LIBS)
	@echo
	@echo "Gettext"
	@echo "-----------------------"
	@echo "Type '$(MAKE) lang' to compile the gettext translation files."
	@echo
	@echo "Maps"
	@echo "-----------------------"
	@echo "Type '$(MAKE) maps' to compile the maps"
	@echo
	@echo "Built for $(BUILDDIR)"
	@echo

include build/lang.mk
include build/install.mk
include build/various.mk

maps: $(UFO2MAP_TARGET)
	@$(MAKE) -C base/maps all

#
# Target to install the whole game - executables and game data.
#
# Use configure's parameters --bindir= and --datadir= to specify destination
# directories, "bindir" for run scripts and "datadir" for the whole game.
# "ufoai/" will be appended to the end of datadir path.
#
install_complete: install_data install_exec

#
# Target to install only the executables, language files and the run script.
# Other game data has to be installed from the ufoai-VERSION-data.tar archive.
#
# For destination directories see install_complete above.
#
install: install_exec
	@echo
	@echo Please unpack file ufoai-$(VERSION)-data.tar into $(DATADIR)
	@echo
	@echo

#
# Target to install only the game data.
#
install_data:
	@$(MAKE) maps
# Create all pk3 files
	@cd base && ./archives.sh
	$(INSTALL) -d $(DATADIR)/base
# Copy them to datadir
	$(INSTALL) -m 644 base/*.pk3 $(DATADIR)/base

#
# Target to install the game executables and language files. It also creates a
# shell script to run the game and dedicated server from directory other than
# datadir.
#
install_exec: $(TARGETS)
	@$(MAKE) lang
	$(INSTALL) -d $(DATADIR)/base
# Copy each compiled file to datadir (with path)
	for file in $(TARGETS); do $(INSTALL) -m 755 $$file $(DATADIR)/$$file; done
# Copy all language files
	for dir in base/i18n/*; do $(INSTALL) -m 644 $$dir/LC_MESSAGES/ufoai.mo $(DATADIR)/$$dir/LC_MESSAGES/ufoai.mo; done
# Create and install run scripts
	@echo "#!/bin/sh" > ufo.sh
	@echo "cd $(DATADIR); ./ufo \$$*; exit \$$?" >> ufo.sh
	$(INSTALL) -m 755 ufo.sh $(BINDIR)/ufo
	@echo "#!/bin/sh" > ufoded.sh
	@echo "cd $(DATADIR); ./ufoded \$$*; exit \$$?" >> ufoded.sh
	$(INSTALL) -m 755 ufoded.sh $(BINDIR)/ufoded

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

-include $(ALL_DEPS)
