## Uncomment if you have gcc
#CC      = gcc
#CFLAGS  = -Wall -g -O2
#LDFLAGS =
#LDLIBS  =

#CFLAGS += -Dsysconfdir=\"/etc\"
CFLAGS = -DDEBUG=1 -DENABLE_DUMP=1

## NOTE: if you get errors when linking qstat (missing symbols or
## libraries), then modify LDFLAGS or LDLIBS

SRC = \
	utils.c \
	xform.c \
	config.c \
	debug.c \
	hcache.c \
	md5.c \
	qserver.c \
	qstat.c \
	template.c \
	display_json.c \
	ut2004.c \
	a2s.c \
	packet_manip.c \
	gs3.c \
	gs2.c \
	gps.c \
	ts2.c \
	doom3.c \
	tm.c \
	haze.c \
	wic.c \
	ottd.c \
	fl.c \
	tee.c \
	ts3.c \
	bfbc2.c \
	ventrilo.c \
	cube2.c \
	mumble.c \
	terraria.c \
	crysis.c \
	dirtybomb.c \
	starmade.c \
	farmsim.c \
	ksp.c \
	tf.c \
	armyops.c

SOURCES = \
	version.h \
	$(SRC)

OBJ = $(SRC:.c=.obj)
O   = $(SRC:.c=.o)

SOLARIS_LIBS = -lsocket -lnsl
WINDOWS_LIBS = wsock32.lib
OS2_LIBS     = so32dll.lib tcp32dll.lib
EMX_LIBS     = -lsocket

ifdef MAKEDIR: # gmake: false; nmake: unused target
!ifdef MAKEDIR # gmake: not seen; nmake: true
# nmake specific code

LINK = link.exe
LFLAGS = /nologo
CFLAGS = $(CFLAGS) /nologo
PHONY = .phony

.phony:

.version: $(PHONY)
	call scripts/gen-version.cmd

version.h: .version

!else # and now the other
else
# gnu make targets
.PHONY: .compiler_flags
.compiler_flags:
	echo '$(CFLAGS)' | cmp -s - $@ || echo '$(CFLAGS)' > $@

.PHONY: .version
.version:
	@ver=$$(./scripts/version.sh); \
	echo "$$ver" | cmp -s - $@ || echo "$$ver" > $@

version.h: .version
	@ver=$$(cat .version); \
	file=$$(sed "s/CHANGEME/$$ver/g" ./version.h.tmpl); \
	echo "$$file" | cmp -s - $@ || echo "$$file" > $@

endif    # gmake: close condition; nmake: not seen
!endif : # gmake: unused target; nmake close conditional

all: qstat

qstat: .compiler_flags version.h $(O)
	$(CC) $(CFLAGS) -o qstat $(O) $(LDFLAGS) $(LDLIBS)

solaris: .compiler_flags $(SOURCES)
	$(CC) $(CFLAGS) -o qstat $(SRC) $(LDFLAGS) $(LDLIBS) $(SOLARIS_LIBS)

aix sgi freebsd macosx osx openbsd irix linux: qstat

hp hpux: .compiler_flags $(SOURCES)
	$(CC) $(CFLAGS) -Ae -o qstat $(SRC) $(LDFLAGS) $(LDLIBS)

win32: windows

.c.obj:
	$(CC) $(CFLAGS) -c $<

# windows dynamic rebuild but doesn't handle a QSTAT_VERSION change.
# use the clean target to force a rebuild.
qstat.exe: version.h $(OBJ)
	$(LINK) $(LFLAGS) $(OBJ) $(WINDOWS_LIBS) /out:$@

# windows release forced
windows: $(SOURCES)
	$(CC) $(CFLAGS) $(SRC) /Feqstat.exe $(WINDOWS_LIBS)

# windows debug forced
windows_debug: $(SOURCES)
	$(CC) $(CFLAGS) /Zi $(SRC) /Feqstat.exe $(WINDOWS_LIBS) /link /fixed:no /incremental:no

clean:
	-rm -f qstat core qstat.exe *.pdb .compiler_flags .version version.h $(O) $(OBJ)
