CC   = gcc
STRIP= strip

# whether to use WatTCP for DOS UDP networking
USE_WATT32=yes
# whether to use Beame & Whiteside for DOS networking
USE_BWTCP=yes
# whether to use MPATH for DOS UDP networking under Win9x
USE_MPATH=no
# whether to use OGG VORBIS
USE_OGG=yes
# use tremor library for ogg/vorbis:
USE_TREMOR=1
# pci sound card support:
USE_SNDPCI=1
# whether to use BSP2 Support (needs further testing, but appears OK)
USE_BSP2=yes

CFLAGS = -g -Wall -Werror -O0 -fno-strict-aliasing -fomit-frame-pointer
LIBS   =

ifeq ($(USE_SNDPCI),1)
CFLAGS+= -DUSE_SNDPCI
CFLAGS+= -I../3rdparty/include
LIBS += -L../3rdparty/lib -lau
endif

ifeq ($(USE_BWTCP),yes)
CFLAGS+= -DUSE_BWTCP
endif

ifeq ($(USE_MPATH),yes)
CFLAGS+= -DUSE_MPATH
endif

ifeq ($(USE_WATT32),yes)
CFLAGS+= -DUSE_WATT32 -DWATT32_NO_OLDIES
CFLAGS+= -I../wat/inc
LDFLAGS= -L../wat/lib
LIBS  += -lwatt
endif

ifeq ($(USE_OGG),yes)
CFLAGS+= -DOGG_SUPPORT
CFLAGS+= -I../3rdparty/include
ifneq ($(USE_TREMOR),1)
LIBS  += -L../3rdparty/lib -lvorbisfile -lvorbis -logg
else
CFLAGS+= -DVORBIS_USE_TREMOR
LIBS  += -L../3rdparty/lib -lvorbisidec -logg
endif
endif

ifeq ($(USE_BSP2),yes)
CFLAGS+= -DBSP2_SUPPORT
endif
LIBS  += -lm

# subdirectory for objects
O=dos

# objects:
DOSTCP :=
ifeq ($(USE_BWTCP),yes)
DOSTCP += $(O)/net_bw.o
endif
ifeq ($(USE_MPATH),yes)
DOSTCP += $(O)/net_mp.o $(O)/mplpc.o
endif
ifeq ($(USE_WATT32),yes)
DOSTCP += $(O)/net_udp.o
else
# get inet_addr() and inet_ntoa() either from Watt-32
# or from our local implementation 
DOSTCP += $(O)/dos_inet.o $(O)/inetaddr.o
endif
SYSOBJ_NET := $(DOSTCP)

# not too sophisticated dependency
OBJS= \
$(O)/cd_audio.o \
$(O)/d_edge.o   \
$(O)/host.o     \
$(O)/r_aclip.o  \
$(O)/dstring.o   \
$(O)/r_vars.o   \
$(O)/chase.o    \
$(O)/d_fill.o   \
$(O)/host_cmd.o \
$(O)/r_alias.o  \
$(O)/sbar.o     \
$(O)/cl_demo.o  \
$(O)/d_init.o   \
$(O)/keys.o     \
$(O)/r_bsp.o    \
$(O)/screen.o   \
$(O)/cl_input.o \
$(O)/d_modech.o \
$(O)/mathlib.o  \
$(O)/r_draw.o   \
$(O)/r_drawa.o  \
$(O)/sv_main.o  \
$(O)/cl_main.o  \
$(O)/menu.o     \
$(O)/r_edge.o   \
$(O)/sv_move.o  \
$(O)/cl_parse.o \
$(O)/d_polyse.o \
$(O)/model.o    \
$(O)/r_efrag.o  \
$(O)/sv_phys.o  \
$(O)/cl_tent.o  \
$(O)/d_scan.o   \
$(O)/r_light.o  \
$(O)/sv_user.o  \
$(O)/cmd.o      \
$(O)/d_sky.o    \
$(O)/r_main.o   \
$(O)/view.o     \
$(O)/common.o   \
$(O)/d_sprite.o \
$(O)/r_misc.o   \
$(O)/wad.o      \
$(O)/d_surf.o   \
$(O)/r_part.o   \
$(O)/world.o    \
$(O)/console.o  \
$(O)/d_vars.o   \
$(O)/pr_cmds.o  \
$(O)/r_sky.o    \
$(O)/zone.o     \
$(O)/crc.o      \
$(O)/d_zpoint.o \
$(O)/pr_edict.o \
$(O)/r_sprite.o \
$(O)/cvar.o     \
$(O)/draw.o     \
$(O)/pr_exec.o  \
$(O)/r_surf.o   \
$(O)/net_main.o \
$(O)/net_dgrm.o \
$(O)/net_loop.o \
$(O)/net_dos.o  \
$(O)/net_ser.o  \
$(O)/net_ipx.o  \
$(DOSTCP)       \
$(O)/snd_dos.o  \
$(O)/snd_dma.o  \
$(O)/snd_gus.o  \
$(O)/snd_pci.o  \
$(O)/snd_sb.o  \
$(O)/snd_mix.o  \
$(O)/snd_mem.o  \
$(O)/sys_find.o  \
$(O)/snd_strm.o  \
$(O)/snd_wavstream.o  \
$(O)/in_dos.o   \
$(O)/vid_vga.o  \
$(O)/vid_ext.o  \
$(O)/vregset.o  \
$(O)/d_copy.o   \
$(O)/sys_dosa.o \
$(O)/vid_dos.o  \
$(O)/d_draw.o   \
$(O)/d_draw16.o \
$(O)/d_part.o   \
$(O)/d_parta.o  \
$(O)/d_polysa.o \
$(O)/d_scana.o  \
$(O)/d_spr8.o   \
$(O)/d_varsa.o  \
$(O)/math.o     \
$(O)/r_aclipa.o \
$(O)/r_aliasa.o \
$(O)/r_edgea.o  \
$(O)/r_varsa.o  \
$(O)/snd_mixa.o \
$(O)/surf16.o   \
$(O)/surf8.o    \
$(O)/worlda.o   \
$(O)/cfgfile.o   \
$(O)/dos_v2.o   \
$(O)/Goa/CEngine/darray.o \
$(O)/Goa/CEngine/gserver.o \
$(O)/Goa/CEngine/gserverlist.o \
$(O)/Goa/SDK/goautil.o \
$(O)/Goa/CEngine/hashtable.o \
$(O)/Goa/nonport.o \
$(O)/sys_dos.o
#$(O)/dosasm.o

all: qdos.exe

clean:
	rm -f $(O)/*.o
	rm -f $(O)/Goa/*.o
	rm -f $(O)/Goa/CEngine/*.o
	rm -f $(O)/Goa/SDK/*.o

qdos.exe: $(OBJS)
	$(CC) $(OBJS) $(LDFLAGS) $(LIBS) -o dquake.exe
	rm -f qdos.exe
	cp -p dquake.exe qdos.exe
	$(STRIP) qdos.exe

DO_AS=$(CC) -x assembler-with-cpp

$(O)/%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@
$(O)/%.o: %.S
	$(DO_AS) $(CFLAGS) -c $< -o $@
$(O)/%.o: %.s
	$(DO_AS) $(CFLAGS) -c $< -o $@
