.PHONY: all generate clean


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

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

#
#----------------------
# 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
CC=gcc
CFLAGS=-Wall -Dstricmp=strcasecmp -DSERVERONLY -I. -I../client -I$(GNUPG_INCLUDES)
LD_FLAGS=-lm -ldl $(GNUPG_LIBS)
RELEASE_FLAGS=-O3 -ffast-math -funroll-loops -fomit-frame-pointer -fexpensive-optimizations

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 GeneratedData.o

GENERATECRC=generatecrc
QWFCRCH=qwfcrc.h



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

all:
	@echo Type make generate and then make

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

	@echo "$(PROGRAM): $(OBJECTS) $(CLIENT_OBJECTS)" >> $(RULES)
	@echo "	\$$(CC) \$$(CFLAGS) \$$(OBJECTS) \$$(CLIENT_OBJECTS) \$$(LD_FLAGS) -o \$$(PROGRAM)" >> $(RULES)
	@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:
	# Ensure that all programs and generated .c and .h files are removed here!
	rm -f *.o $(QWFCRCH) $(GENERATECRC)

$(QWFCRCH):	$(GENERATECRC)
	./$(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)

