.PHONY: all generate clean pristine


# Rule file.. This file is generates the rules that
# We make..
RULES=.rule_file

STRIP=strip --remove-section=.comment

#
#----------------------
# The encryption stuff
#----------------------
#

# Make ../gnupg a symlink to your (already compiled) gnupg source directory
# Eg. /usr/local/src/gnupg-1.0.1  or  $(HOME)/projects/gnupg-1.0.1
GNUPG=../gnupg

GNUPG_INCLUDES=$(GNUPG)/include
GNUPG_LIBS=$(GNUPG)/lib/lib*.a

PROGRAM=../qwfsv.real
OTHER_PROGRAMS=../qwfsv.real.libc5	# This is just a hack to make clean delete the libc5 server too.
CLIENTS=../qwfcl.svga ../qwfcl.x11 ../glqwfcl.glx ../qwfcl.svga.libc5 ../qwfcl.x11.libc5 ../glqwfcl.glx.libc5
CC=gcc
CFLAGS=-Wall -Dstricmp=strcasecmp -DSERVERONLY -I. -I../client -I$(GNUPG_INCLUDES)
LD_FLAGS=-lm $(GNUPG_LIBS)
RELEASE_FLAGS=-O3 -ffast-math -funroll-loops -fomit-frame-pointer -fexpensive-optimizations


# LDFLAGS for release
# This forces the end users to use the statically linked in libraries.
# For things like libvga, libGL, etc, this REALLY sucks.
# Seriously consider NOT statically linking.
#RELEASE_LD_FLAGS=-static
RELEASE_LD_FLAGS=


OBJECTS = pr_cmds.o pr_edict.o pr_exec.o sv_init.o \
	 sv_main.o sv_nchan.o sv_ents.o sv_send.o \
	 sv_move.o sv_phys.o sv_user.o sv_ccmds.o \
	 world.o sys_unix.o model.o

CLIENT_OBJECTS= cmd.o common.o crc.o cvar.o qwf.o \
	mathlib.o md4.o zone.o \
	pmove.o pmovetst.o \
	net_chan.o net_udp.o \
	GetKey0.o GetKey1.o GetKey2.o GetKey3.o \
	GetKey4.o GetKey5.o GetKey6.o GetKey7.o GetKey8.o \
	GetKey9.o GetKey10.o GetKey11.o GetKey12.o GetKey13.o \
	GetKey14.o GetKey15.o GetKey16.o GetKey17.o GetKey18.o \
	GetKey19.o GetKey20.o GetKey21.o GetKey22.o GetKey23.o \
	GetKey24.o GetKey25.o GetKey26.o GetKey27.o GetKey28.o \
	GetKey29.o GetKey30.o GetKey31.o \
	GeneratedData1.o GeneratedData2.o

GENERATECRC=generatecrc
QWFCRCH=qwfcrc.h



SRC=$(OBJECTS:%.o=%)
CLIENT_SRC=$(CLIENT_OBJECTS:%.o=%)


# Find out what libc we're running under.
LIBCTEST=../client/libctest.sh
ifneq ($(shell $(LIBCTEST)),6)
    PROGRAM_SUFFIX=.libc$(shell $(LIBCTEST))
endif
PROGRAM:=$(PROGRAM)$(PROGRAM_SUFFIX)


# If the rule file exists include it.
ifeq ($(RULES),$(wildcard $(RULES)))
include $(RULES)
endif


all:
	@echo Type make generate and then make

generate:
	@if [ "$(RELEASE)x" != "x" ]; then \
        echo "CFLAGS += \$$(RELEASE_FLAGS)" > $(RULES); \
        echo "LD_FLAGS += \$$(RELEASE_LD_FLAGS)" >> $(RULES); \
        else cat /dev/null > $(RULES); fi;

	@echo "$(PROGRAM): $(OBJECTS) $(CLIENT_OBJECTS)" >> $(RULES)
	@echo "	\$$(CC) \$$(CFLAGS) \$$(shell ../client/randargs \$$(OBJECTS) \$$(CLIENT_OBJECTS)) \$$(LD_FLAGS) -o \$$(PROGRAM)" >> $(RULES)
	@if [ "$(RELEASE)x" != "x" ]; then \
		echo "	\$$(STRIP) \$$(PROGRAM)" >> $(RULES); \
		fi;
	@echo "" >> $(RULES)

	# Make the rules for it's C source files.
	# The QWFCRCH below is a hack, but needed since this stupid crap
	# won't let me make it a dependency of sv_main.o alone.
	@for i in $(SRC); do echo "$${i}.o: $${i}.c $(QWFCRCH)" >> $(RULES); \
        echo "	\$$(CC) \$$(CFLAGS) -o $${i}.o -c $${i}.c" >> $(RULES); echo "" >> $(RULES); done

	@for i in $(CLIENT_SRC); do echo "$${i}.o: ../client/$${i}.c" >> $(RULES); \
	echo "	\$$(CC) \$$(CFLAGS) -o $${i}.o -c ../client/$${i}.c" >> $(RULES); echo "" >> $(RULES); done

clean:
	rm -f *.o

pristine: clean
	rm -f $(PROGRAM) $(OTHER_PROGRAMS)
	rm -f $(GENERATECRC)
	rm -f $(QWFCRCH) 

$(QWFCRCH):	$(GENERATECRC) $(CLIENTS)
	@if ! $(GENERATECRC) ; then \
	rm -f $(GENERATECRC); \
	$(MAKE) $(GENERATECRC); \
	fi;
	./$(GENERATECRC)

$(GENERATECRC):	$(GENERATECRC).c
	#make -C ../client crc.o
	$(CC) $(CFLAGS) -c $(GENERATECRC).c -o $(GENERATECRC).o
	$(CC) $(CFLAGS) $(GENERATECRC).o ../client/crc.o -o $(GENERATECRC)

# This is a hack which lets the rule for $(QWFCRCH) still be run
# even when not all of the $(CLIENTS) files exist.
# Handy when some can't be built, like glqwfcl.glx.libc5
$(CLIENTS)::

