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

PROJECT_DIR  = @top_srcdir@
SRC_DIR      = @srcdir@

DESTDIR      =

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

COMMON_DIR=$(PROJECT_DIR)/common
QW_COMMON_DIR= $(PROJECT_DIR)/qw_common
BUILD_DIR = ../targets/qw_server

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

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

MAKE_SURE_DIR = if test -d "$(BUILD_DIR)/$$DIR"; \
                then \
                   true; \
                else \
		   echo "Creating directory $(BUILD_DIR)/$$DIR"; \
		   mkdir -p $(BUILD_DIR)/$$DIR; \
                fi


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

#
# System specific source files
#

QW_SRV_SYS_SRC = @QW_SRV_SYS_SRC@

#
# Networking source files
#
# XXX - Should not assume UNIX
QW_NET_SRC     = net_udp.c net_com.c mdfour.c


#
# Server source files
#
# Server-related source used by all
SRV_SRC     = sv_main.c sv_user.c sv_move.c sv_phys.c 
# Server-related source used just by QW server
QW_SRV_SRC  = sv_ccmds.c sv_ents.c sv_init.c sv_send.c sv_nchan.c world.c
# Server-related source used just by UQ
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_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
# XXX - add dos/win specifc source

x: Makefile
	@echo binaries: $(targets)
	@echo other targets: distclean 

all: $(targets)

###########################################################################
#
# qw-server
#
OBJSqw-server = $(patsubst %,$(BUILD_DIR)/srv/%, \
        $(addsuffix .@OBJEXT@, $(basename $(ALL_QW_SRV_SRC) .c .s)))

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

MAKE_SURE_srv_DIR = @DIR=srv; $(MAKE_SURE_DIR)
$(BUILD_DIR)/srv/%.o: $(SRC_DIR)/%.c
	$(MAKE_SURE_srv_DIR)
	$(CC) $(CFLAGS) $(SRV_CFLAGS) -o $@ -c $<

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

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

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

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

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

$(BUILD_DIR)/../$(SRVQUAKE): $(OBJSqw-server)
	$(CC) $(CFLAGS) $(OBJSqw-server) $(SRV_LDFLAGS) $(LDFLAGS) -o $(BUILD_DIR)/../$(SRVQUAKE)
# XXX - Can this be automated?
clean-$(SRVQUAKE):
	rm -f $(OBJSqw-server)

clean: clean-$(SRVQUAKE)

###########################################################################
#
# cleaning (clean, distclean)
#
distclean: clean
	rm -f config.cache config.log config.status Makefile

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

check:
	@echo check not implemented
