PLATFORM = $(shell file /bin/ls | cut -d' ' -f3 | cut -d'-' -f1)

GCC_VERSION = $(shell $(CC) --version 2>&1 | grep "(GCC)" | cut -d' ' -f3  | cut -d'.' -f1)
BSD_VERSION = $(shell uname -v 2>&1 | cut -d' ' -f2 | cut -d'.' -f1)
SVN_VERSION = 40250

### LINUX-BLOCK-BEGIN (Makefile uname -s split) #########################
# This Makefile used to be FreeBSD-only.  Everything below is now selected on
# `uname -s'; the non-Linux side is byte-for-byte what shipped, so the FreeBSD
# production build is unaffected.
UNAME_S = $(shell uname -s)
### LINUX-BLOCK-END (Makefile uname -s split) ###########################

INCDIR =
LIBDIR =
BINDIR = ..
OBJDIR = OBJDIR
$(shell if [ ! -d $(OBJDIR) ]; then mkdir $(OBJDIR); fi)

ifeq ($(UNAME_S),Linux)
### LINUX-BLOCK-BEGIN (Makefile Linux toolchain/paths) ##################
# 32-bit Linux build.
#   * g++ 13 rather than clang++-devel: it is what the port is validated on
#     and it supports -std=c++23 (needed by the existing CFLAGS).
#   * The 32-bit dependency set lives in /opt/m2port/extern/Linux40250; the
#     FreeBSD absolute paths (/usr/local/lib/mysql, /usr/lib/libssl.a, ...)
#     have no Linux equivalent and would silently pick up 64-bit host copies.
#   * M2_EXTERN_LINUX can be overridden from the command line.
#   * -DBOOST_BIND_GLOBAL_PLACEHOLDERS silences boost/bind.hpp's global
#     placeholder deprecation #pragma message (main.cpp includes it).
#   * NOT defined, deliberately: _TIME_BITS=64.  sizeof(time_t) must stay 4
#     under -m32 - see the static_asserts in packet.h.  -D_FILE_OFFSET_BITS=64
#     alone would be safe but nothing here needs it, so it is left off too.
CC = g++

# The 32-bit dependency set.  NOTE the path depth: the original
# "../../../extern" resolves to port40250/extern, whose libIL.a / libcryptopp.a
# are FreeBSD objects ("ELF ... version 1 (FreeBSD)") and will not link here.
# The Linux set is one level further up.  Override on the command line if the
# tree is relocated:  make M2_EXTERN_LINUX=/somewhere/else
M2_EXTERN_LINUX ?= ../../../../extern/Linux40250

# Install destination.  Overridable, and NOT touched by the default target -
# `make' only builds and links; `make install' is the only thing that writes
# outside this directory.  /usr/metin2/... does not exist on the Linux porting
# host, and the real drop-in location is chosen by whoever deploys.
GAMEDIR ?= /usr/metin2/server/share/bin

CFLAGS = -g -Wall -w -O2 -m32 -pipe -std=c++23 -fexceptions -D_THREAD_SAFE -DNDEBUG # -D_USE_SERVER_KEY_
CFLAGS += -mtune=i686 -fstack-protector-all
# boost/bind.hpp (included by main.cpp) otherwise emits a global-placeholder
# deprecation #pragma message on every TU that reaches it.
CFLAGS += -DBOOST_BIND_GLOBAL_PLACEHOLDERS
# Include paths.  $(M2_EXTERN_LINUX)/include carries boost 1.72 (matching the
# version the tree ships), cryptopp, IL - and mysql/mysql.h, which
# libsql/AsyncSQL.h:11 includes and which FreeBSD found under
# /usr/local/include.  Without that last one, 60 TUs fail with
# "AsyncSQL.h:11: fatal error: mysql/mysql.h: No such file or directory".
INCDIR += -I$(M2_EXTERN_LINUX)/include
INCDIR += -I../../liblua/include
INCDIR += -I../../libserverkey

# Link.  Project archives must precede the external ones they draw symbols
# from (libgame/libserverkey reference cryptopp and OpenSSL), and -ldl is
# mandatory on glibc where FreeBSD needed no such thing.
LIBDIR += -L../../libthecore/lib -L../../libpoly -L../../libsql -L../../libgame/lib
LIBDIR += -L../../liblua/lib -L../../libserverkey -L$(M2_EXTERN_LINUX)/lib
LIBS  = -lthecore -lpoly -llua -llualib -lsql -lgame -lserverkey
LIBS += $(M2_EXTERN_LINUX)/lib/libcryptopp.a $(M2_EXTERN_LINUX)/lib/libIL.a
LIBS += $(M2_EXTERN_LINUX)/lib/libmysqlclient.a
LIBS += $(M2_EXTERN_LINUX)/lib/libssl.a $(M2_EXTERN_LINUX)/lib/libcrypto.a
LIBS += $(M2_EXTERN_LINUX)/lib/libz.a
LIBS += -lmd -pthread -lm -ldl

USE_STACKTRACE = 0
ifeq ($(USE_STACKTRACE), 1)
# glibc has backtrace()/backtrace_symbols() in libc itself; there is no
# separate libexecinfo to link against.
endif
### LINUX-BLOCK-END (Makefile Linux toolchain/paths) ####################
else

CC = clang++-devel

GAMEDIR = /usr/metin2/server/share/bin

# Standard Setting
LIBS = -pthread -lm -lmd
# Removed -fno-rtti
CFLAGS = -g -Wall -w -O2 -m32 -pipe -std=c++23 -fexceptions -D_THREAD_SAFE -DNDEBUG # -D_USE_SERVER_KEY_
CFLAGS += -mtune=i686 -fstack-protector-all

# CryptoPP
LIBS += ../../../extern/lib/libcryptopp.a

# DevIL
LIBS += ../../../extern/lib/libIL.a

# MySQL
LIBS += /usr/local/lib/mysql/libmysqlclient.a /usr/lib/libz.a

# OpenSSL
LIBS += /usr/lib/libssl.a /usr/lib/libcrypto.a

# Miscellaneous external libraries
INCDIR += -I../../../extern/include
LIBDIR += -L../../../extern/lib

# Project Library
INCDIR += -I../../liblua/include
INCDIR += -I../../libserverkey
INCDIR += -I/usr/local/include
INCDIR += -I/usr/include
LIBDIR += -L../../libthecore/lib -L../../libpoly -L../../libsql -L../../libgame/lib -L../../liblua/lib -L../../libserverkey
LIBDIR += -L/usr/local/lib
LIBS += -lthecore -lpoly -llua -llualib -lsql -lgame -lserverkey
USE_STACKTRACE = 0
ifeq ($(USE_STACKTRACE), 1)
LIBS += /usr/local/lib/libexecinfo.a
endif

endif

# TARGET  = $(BINDIR)/game_r$(SVN_VERSION)_$(PLATFORM)
TARGET  = $(BINDIR)/game

CFILE	= minilzo.c

CPPFILE = BattleArena.cpp FSM.cpp MarkConvert.cpp MarkImage.cpp MarkManager.cpp OXEvent.cpp TrafficProfiler.cpp ani.cpp\
		  arena.cpp banword.cpp battle.cpp blend_item.cpp block_country.cpp buffer_manager.cpp building.cpp castle.cpp\
		  char.cpp char_affect.cpp char_battle.cpp char_change_empire.cpp char_horse.cpp char_item.cpp char_manager.cpp\
		  char_quickslot.cpp char_resist.cpp char_skill.cpp char_state.cpp PetSystem.cpp cmd.cpp cmd_emotion.cpp cmd_general.cpp\
		  cmd_gm.cpp cmd_oxevent.cpp config.cpp constants.cpp crc32.cpp cube.cpp db.cpp desc.cpp\
		  desc_client.cpp desc_manager.cpp desc_p2p.cpp dev_log.cpp dungeon.cpp empire_text_convert.cpp entity.cpp\
		  entity_view.cpp event.cpp event_queue.cpp exchange.cpp file_loader.cpp fishing.cpp gm.cpp guild.cpp\
		  guild_manager.cpp guild_war.cpp horse_rider.cpp horsename_manager.cpp input.cpp input_auth.cpp input_db.cpp\
		  input_login.cpp input_main.cpp input_p2p.cpp input_teen.cpp input_udp.cpp ip_ban.cpp\
		  item.cpp item_addon.cpp item_attribute.cpp item_manager.cpp item_manager_idrange.cpp locale.cpp\
		  locale_service.cpp log.cpp login_data.cpp lzo_manager.cpp marriage.cpp matrix_card.cpp\
		  messenger_manager.cpp mining.cpp mob_manager.cpp monarch.cpp motion.cpp over9refine.cpp p2p.cpp packet_info.cpp\
		  party.cpp passpod.cpp pcbang.cpp polymorph.cpp priv_manager.cpp pvp.cpp\
		  questevent.cpp questlua.cpp questlua_affect.cpp questlua_arena.cpp questlua_ba.cpp questlua_building.cpp\
		  questlua_danceevent.cpp questlua_dungeon.cpp questlua_forked.cpp questlua_game.cpp questlua_global.cpp\
		  questlua_guild.cpp questlua_horse.cpp questlua_pet.cpp questlua_item.cpp questlua_marriage.cpp questlua_mgmt.cpp\
		  questlua_monarch.cpp questlua_npc.cpp questlua_oxevent.cpp questlua_party.cpp questlua_pc.cpp\
		  questlua_quest.cpp questlua_target.cpp questmanager.cpp questnpc.cpp questpc.cpp\
		  refine.cpp regen.cpp safebox.cpp sectree.cpp sectree_manager.cpp sequence.cpp shop.cpp\
		  skill.cpp start_position.cpp target.cpp text_file_loader.cpp trigger.cpp utils.cpp vector.cpp war_map.cpp\
		  wedding.cpp xmas_event.cpp version.cpp panama.cpp threeway_war.cpp map_location.cpp auth_brazil.cpp\
		  BlueDragon.cpp BlueDragon_Binder.cpp DragonLair.cpp questlua_dragonlair.cpp\
		  skill_power.cpp affect.cpp SpeedServer.cpp questlua_speedserver.cpp\
		  auction_manager.cpp FileMonitor_FreeBSD.cpp ClientPackageCryptInfo.cpp cipher.cpp\
		  buff_on_attributes.cpp check_server.cpp dragon_soul_table.cpp DragonSoul.cpp\
		  group_text_parse_tree.cpp char_dragonsoul.cpp questlua_dragonsoul.cpp\
		  shop_manager.cpp shopEx.cpp item_manager_read_tables.cpp

ifeq ($(UNAME_S),Linux)
### LINUX-BLOCK-BEGIN (Makefile drop FileMonitor_FreeBSD) ###############
# FileMonitor_FreeBSD.cpp is a kqueue(2) client and cannot compile here
# (<sys/event.h> does not exist).  It is dead code on every platform - the
# __FILEMONITOR__ master switch at main.cpp:71 is commented out and every
# AddWatch() call site is commented out too - so it is dropped rather than
# reimplemented on inotify.  See the block comment in FileMonitor_FreeBSD.h.
CPPFILE := $(filter-out FileMonitor_FreeBSD.cpp,$(CPPFILE))
### LINUX-BLOCK-END (Makefile drop FileMonitor_FreeBSD) #################
endif

COBJS	= $(CFILE:%.c=$(OBJDIR)/%.o)
CPPOBJS	= $(CPPFILE:%.cpp=$(OBJDIR)/%.o)

MAINOBJ = $(OBJDIR)/main.o
MAINCPP = main.cpp

default: $(TARGET)

$(OBJDIR)/minilzo.o: minilzo.c
	@$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@
	@echo compile $<

$(OBJDIR)/version.o: version.cpp
	@$(CC) $(CFLAGS) -D__SVN_VERSION__="$(SVN_VERSION)" -c $< -o $@
	@echo compile $<

$(OBJDIR)/%.o: %.cpp
	@echo compile $<
	@$(CC) $(CFLAGS) $(INCDIR) -c $< -o $@

limit_time:
	@echo update limit time
	@python2.7 update_limit_time.py

ifeq ($(UNAME_S),Linux)
### LINUX-BLOCK-BEGIN (Makefile link/install/deps) ######################
# Link only - the binary is left in $(BINDIR) and NOT moved into $(GAMEDIR).
# `make install' does that explicitly.  The FreeBSD rule below still does the
# unconditional mv, exactly as it shipped.
$(TARGET): $(CPPOBJS) $(COBJS) $(MAINOBJ)
	@echo linking $(TARGET)....
	@$(CC) $(CFLAGS) $(LIBDIR) $(COBJS) $(CPPOBJS) $(MAINOBJ) $(LIBS) -o $(TARGET)
	@echo "Built $(TARGET) (run 'make install' to copy it to $(GAMEDIR))"

# Compile every translation unit without linking and without installing.
# Useful while the sibling libraries are still being built.
objs: $(CPPOBJS) $(COBJS) $(MAINOBJ)
	@echo "Built $(words $(CPPOBJS) $(COBJS) $(MAINOBJ)) objects in $(OBJDIR)/ (no link, no install)"

install: $(TARGET)
	@install -d $(GAMEDIR)
	@install -m 0755 $(TARGET) $(GAMEDIR)
	@echo "Installed $(TARGET) to $(GAMEDIR)"
.PHONY: install objs
### LINUX-BLOCK-END (Makefile link/install/deps) ########################
else

$(TARGET): $(CPPOBJS) $(COBJS) $(MAINOBJ)
	@echo linking $(TARGET)....
	@$(CC) $(CFLAGS) $(LIBDIR) $(COBJS) $(CPPOBJS) $(MAINOBJ) $(LIBS) -o $(TARGET)
	@echo "Moving $(TARGET) to $(GAMEDIR)"
	@mv $(TARGET) $(GAMEDIR)
	@echo "All done! Your game file moved to your server directory."

endif

clean:
	@rm -f $(COBJS) $(CPPOBJS) $(MAINOBJ)
	@rm -f $(OBJDIR)/*.d
	@rm -f $(BINDIR)/game_r* $(BINDIR)/conv

tag:
	ctags *.cpp *.h *.c

dep:
	makedepend -f Depend $(INCDIR) -I/usr/include/c++/3.3 -I/usr/include/c++/4.2 -p$(OBJDIR)/ $(CPPFILE) $(CFILE) $(MAINCPP) 2> /dev/null > Depend

ifeq ($(UNAME_S),Linux)
### LINUX-BLOCK-BEGIN (Makefile deps) ###################################
# `Depend' is a checked-in, FreeBSD-generated makedepend graph.  It names
# /usr/include/machine/_types.h, /usr/include/sys/event.h, sys/queue.h and so
# on as prerequisites; none of those exist on Linux, so `sinclude Depend'
# aborts the build with "No rule to make target ...".  It is left on disk
# untouched so the FreeBSD build keeps using it, and simply not included here.
# Instead the compiler emits real per-object dependency files (-MMD -MP).
CFLAGS += -MMD -MP
sinclude $(wildcard $(OBJDIR)/*.d)
### LINUX-BLOCK-END (Makefile deps) #####################################
else

sinclude Depend

endif
