########################################################################
#
# Quake general stuff
#

PROJECT_DIR	= @top_srcdir@
PROJECT_ODIR	= ..
SRC_DIR		= @srcdir@
QW_COMMON_DIR	= $(PROJECT_DIR)/qw_common
COMMON_DIR	= $(PROJECT_DIR)/common
COMMON_ODIR	= $(PROJECT_ODIR)/common
BIN_PREFIX	= qw-server

DESTDIR		=

prefix		= @prefix@
exec_prefix	= @exec_prefix@
bindir		= @bindir@
mandir		= @mandir@

BUILD_DIR	= $(PROJECT_ODIR)/targets/qw_server

LDFLAGS = @LDFLAGS@ @NET_LIBS@ -lm
LIBS = @LIBS@
CC = @CC@
INTEL_ARCH = @INTEL_ARCH@

ifneq ($(SRC_DIR),.)
SRC_DIR_INC = -I$(SRC_DIR)
endif


########################################################################
#
# Source files
#

QW_SRV_SYS_SRC = sys_common.c @QW_SRV_SYS_SRC@

# Networking source files
# FIXME: Should not assume UNIX
#
# Uncomment the second line for IPv6 support 

QW_NET_SRC     = net_udp.c net_com.c mdfour.c
#QW_NET_SRC	= net_udp6.c net_com.c mdfour.c

# Server source files

SRV_SRC     = sv_main.c sv_user.c sv_move.c sv_phys.c 
QW_SRV_SRC  = sv_ccmds.c sv_ents.c sv_init.c sv_send.c sv_nchan.c world.c
SRV_PR_SRC  = pr_cmds.c pr_edict.c pr_exec.c


QW_GENERAL_SRC = pmove.c pmovetst.c


########################################################################
#
# Directory specific stuff
#
CFLAGS = @CFLAGS@ -DQUAKEWORLD -DSERVERONLY $(OPTFLAGS) $(DEFS) -I. \
$(SRC_DIR_INC) -I$(QW_COMMON_DIR) -I$(COMMON_ODIR) -I$(COMMON_DIR)
DEFS = @DEFS@

SRVQUAKE = qw-server
targets = $(SRVQUAKE)
.PHONY: $(SRVQUAKE)

GENERAL_SRC = common.c crc.c cvar.c cmd.c mathlib.c wad.c zone.c \
	$(QW_NET_SRC) net_chan.c $(SRV_SRC) $(QW_SRV_SRC) \
        $(SRV_PR_SRC) $(QW_SRV_SYS_SRC) $(QW_GENERAL_SRC) \
	register_check.c
ALL_QW_SRV_SRC = $(GENERAL_SRC) model.c
# FIXME: add dos/win specifc source


all: $(targets)


###########################################################################
#
# QuakeWorld server target
#

ALL_SRV_OBJS = $(patsubst %,$(BUILD_DIR)/%, $(addsuffix .@OBJEXT@,\
               $(basename $(ALL_QW_SRV_SRC) .c .s)))

SRV_CFLAGS = -DSRV $(X_CFLAGS)
# FIXME: Don't use X_EXTRA_LIBS below
SRV_LDFLAGS = @X_EXTRA_LIBS@

srv_DIR:
	mkdir -p $(BUILD_DIR)

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c
	$(CC) $(CFLAGS) $(SRV_CFLAGS) -o $@ -c $<

$(BUILD_DIR)/%.o: $(SRC_DIR)/%.s
	$(CC) $(CFLAGS) -x assembler-with-cpp -o $@ -c $<

$(BUILD_DIR)/%.o: $(COMMON_DIR)/%.c
	$(CC) $(CFLAGS) $(SRV_CFLAGS) -o $@ -c $<

$(BUILD_DIR)/%.o: $(QW_COMMON_DIR)/%.c
	$(CC) $(CFLAGS) $(SRV_CFLAGS) -o $@ -c $<

$(BUILD_DIR)/%.o: $(COMMON_DIR)/%.s
	$(CC) $(CFLAGS) -x assembler-with-cpp -o $@ -c $<

$(SRVQUAKE): srv_DIR $(BUILD_DIR)/../$(SRVQUAKE)

$(BUILD_DIR)/../$(SRVQUAKE): $(ALL_SRV_OBJS)
	$(CC) $(CFLAGS) $(ALL_SRV_OBJS) $(SRV_LDFLAGS) $(LDFLAGS) $(LIBS) \
	-o $(BUILD_DIR)/../$(SRVQUAKE)


###########################################################################
#
# Misc Targets
#

distclean: clean
	rm -f config.cache config.log config.status Makefile

clean-qw-server:
	rm -f $(BUILD_DIR)/../qw-server

clean: clean-qw-server
	rm -rf $(BUILD_DIR)

install: $(targets)
	$(PROJECT_DIR)/mkinstalldirs $(DESTDIR)/$(bindir)
	for i in $(targets); do \
	   $(PROJECT_DIR)/install-sh -m 755 $(BUILD_DIR)/../$$i \
	     $(DESTDIR)/$(bindir)/$$i; \
	done

check:
	@echo check not implemented

