#
# Whatever you put in for $(CC) must be able to grok ANSI C.
#
CC = cl
CPP = cl 
AR = link
LD = cl
YACC = yacc
WINZIP = C:\Applications\Winzip\wzzip

# The suffix appended to executables.  
# This should be set for Cygwin and Windows.
EXE = .exe
#EXE =

# Compiler directives for debugging
!ifdef DEBUG
DEBUG_CFLAGS = /MTd /ZI /Od /EHsc /DDEBUG 
DEBUG_LFLAGS = /DEBUG /PDBTYPE:SEPT 
LIBS= libcmtd.lib kernel32.lib ws2_32.lib
!else
DEBUG_CFLAGS = /MT /Ot /Oy /Ob2 /Gs /GF /Gy /EHsc /DNDEBUG   
DEBUG_LFLAGS = 
LIBS= libcmt.lib kernel32.lib ws2_32.lib
!endif

DEFS= -DWIN32 -DWIN32_LEAN_AND_MEAN -D_CONSOLE -D_MBCS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USE_32BIT_TIME_T # -DMETHOD_DEBUG -DCACHE_DEBUG -DDBMCHUNK_DEBUG
OPTIM= /nologo /W1 $(DEBUG_CFLAGS) 

CFLAGS= $(OPTIM) $(DEFS)
LFLAGS = /link /NOLOGO /NODEFAULTLIB /SUBSYSTEM:CONSOLE $(DEBUG_LFLAGS) 

# source and bin directories
S = src
N = src\ndbm
B = bin

INCS = -I$(N)

NDBM_SRC = $(N)\hash.c $(N)\hash_bigkey.c $(N)\hash_buf.c $(N)\hash_func.c \
	$(N)\hash_log2.c $(N)\hash_page.c $(N)\ndbm.c 
NDBM_OBJ = $(NDBM_SRC:.c=.obj)
NDBM_LIB = $(N)\ndbm.lib
NDBM_HDRS = $(N)\db.h $(N)\ndbm.h $(N)\hash.h $(N)\page.h $(N)\extern.h 
NDBM_DOC = $(N)\ndbm.html $(N)\README_Impt_License_Change.txt

CFILES = $(S)\perms.c $(S)\error.c $(S)\code.c $(S)\symbol.c \
	$(S)\utils.c $(S)\decode.c $(S)\list.c $(S)\map.c $(S)\var.c $(S)\verb.c \
	$(S)\method.c $(S)\compile.c $(S)\execute.c $(S)\opcode.c $(S)\opcodes1.c \
	$(S)\opcodes2.c $(S)\opcodes3.c $(S)\builtins.c $(S)\servers.c \
	$(S)\dispatch.c $(S)\message.c $(S)\storage.c $(S)\dbsize.c $(S)\dbpack.c \
	$(S)\dbunpack.c $(S)\hash.c $(S)\string.c $(S)\buf.c $(S)\netio.c \
	$(S)\playerio.c $(S)\serverio.c $(S)\cache.c $(S)\dbmchunk.c $(S)\os.c

OFILES= $(S)\cool.obj $(CFILES:.c=.obj) 

# Files in the standard distribution
DISTFILES= README CHANGES CHANGELOG COPYING COPYRIGHT CREDITS INSTALL_NOTES \
	Makefile Makefile.bor Makefile.vc Makefile.dgm 

!ifndef RELEASE
RELEASE=dist
!endif

TARGETS = $(NDBM_LIB) $(B)\cm$(EXE) $(B)\west.db $(B)\east.db 

all: $(TARGETS)

$(NDBM_LIB): $(NDBM_OBJ)
	$(AR) /LIB -OUT:$@ $**

$(B)\cm$(EXE): $(NDBM_LIB) $(OFILES) 
	$(LD) $** $(LIBS)  /Fe$@ $(LFLAGS)

$(B)\west.db: $(B)\cm.core 
	-@del west.tmp 2>NUL
	for %i in ($**) do type %i >> west.tmp
	copy $(B)\west.cl $(B)\west.cl.c
	$(CPP) /nologo /EP $(B)\west.cl.c >>west.tmp
	-@del $(B)west.cl.c 2>NUL
	$(B)\cm$(EXE) $(B)\west -i -f west.tmp

$(B)\east.db: $(B)\cm.core 
	-@del east.tmp 2>NUL
	for %i in ($**) do type %i >> east.tmp
	copy $(B)\east.cl $(B)\east.cl.c
	$(CPP) /nologo /EP $(B)\east.cl.c >>east.tmp
	-@del $(B)\east.cl.c 2>NUL
	$(B)\cm$(EXE) $(B)\east -i -f east.tmp

$(S)\cool.c $(S)\y.tab.h: $(S)\cool.y
	$(YACC) -l -d $**
	-move y.tab.c $(S)\cool.c
	-move y.tab.h $(S)\y.tab.h

clean:
	-@del $(B)\*.pdb $(B)\*.idb $(B)\*.ilk $(S)\y.tab.h $(S)\cool.c $(TARGETS) \
	$(OFILES) $(NDBM_OBJ) $(B)\west.db $(B)\east.db $(B)\west.chunk $(B)\east.chunk $(B)\*.log 2>NUL

dist : $(DISTFILES)
	@echo "Building distribution..."
	@-md coolmud
	@-copy /y $** coolmud 1>NUL
	@echo .svn > ex.tmp
	@xcopy src coolmud\src /I /E /Y /Q /EXCLUDE:ex.tmp 
	@xcopy bin coolmud\bin /I /E /Y /Q /EXCLUDE:ex.tmp 
	@xcopy doc coolmud\doc /I /E /Y /Q /EXCLUDE:ex.tmp 
	@del ex.tmp
	@$(WINZIP) -Pr coolmud-2.3-$(RELEASE).zip coolmud 1>NUL 2>NUL
	@rd /s /q coolmud
	@echo "Done."

.c.obj:
	$(CC) $(CFLAGS) $(INCS) /c /Tc$< /Fo$@