#
# Whatever you put in for $(CC) must be able to grok ANSI C.
#
CC = cl
LD = cl
YACC = yacc
YFLAGS= -v
WINZIP = C:\apps\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 /FD /GZ /D "DEBUG" 
DEBUG_LFLAGS = /DEBUG /PDBTYPE:SEPT 
LIBS= libcmtd.lib kernel32.lib ws2_32.lib 
!else
DEBUG_CFLAGS = /MT /Ot /Oy /Ob1 /Gs /Gy /D "NDEBUG" 
DEBUG_LFLAGS = 
LIBS= libcmt.lib kernel32.lib ws2_32.lib 
!endif

DEFS= /D "NOISY_GC" /D "LOG_COMMANDS" /D "DUMP_CORE" 
DEFS2= /D "WIN32" /D "WIN32_LEAN_AND_MEAN" /D "_CONSOLE" /D "_MBCS" /D "TRACE"
OPTIM= /nologo /GX /W1 $(DEBUG_CFLAGS) /G$(PROCESSOR_LEVEL) 
CFLAGS= $(OPTIM) $(DEFS) $(DEFS2)
LFLAGS = /link /NOLOGO /NODEFAULTLIB /SUBSYSTEM:CONSOLE $(DEBUG_LFLAGS) 

HDR = alist.h  bytecode.h  config.h  db.h  externs.h  globals.h  interface.h \
    os.h set.h

XTRA = language.pdf language.tex language.txt reserved.txt caveats.txt \
    INSTALL_NOTES COPYING TODO load.sh load.cmd init.db loadup.txt tiny.m4 \
    Makefile makefile.bor makefile.vc 

# Files needed to run the database 
DBSRC = db.c alist.c set.c utils.c vars.c predicates.c os.c
DBOBJ = $(DBSRC:.c=.obj)

# Files needed to run the interpreter
INTERSRC = move.c tell.c create.c syscall.c encrypt.c bytecode.c \
        compiler.y match.c delay.c 
INTEROBJ = move.obj tell.obj create.obj syscall.obj encrypt.obj \
        bytecode.obj compiler.obj match.obj delay.obj 
        
# Files needed to run the parser
PARSESRC = command.c
PARSEOBJ = $(PARSESRC:.c=.obj)

# Files needed to run the game (everything except the driver)
GAMESRC = game.c
GAMEOBJ = $(GAMESRC:.c=.obj)

# Files needed to run the network version
NETSRC = netdriver.c 
NETOBJ = $(NETSRC:.c=.obj)

# Files needed to run the tty version
TTYSRC = ttydriver.c 
TTYOBJ = $(TTYSRC:.c=.obj)

# Files needed for the loader utility
LDRSRC = loader.c 
LDROBJ = $(LDRSRC:.c=.obj)

# Files needed for the massager utility
MSRC = massager.c 
MOBJ = $(MSRC:.c=.obj)

# Files needed for the runtest program
RTSRC = runtest.c 
RTOBJ = $(RTSRC:.c=.obj)

# Files needed for the comptest program
CTSRC = comptest.c 
CTOBJ = $(CTSRC:.c=.obj)

OUTFILES= y.tab.h test.db y.output compiler.c

# Files in the standard distribution
DISTFILES = $(HDR) $(XTRA) $(DBSRC) $(INTERSRC) $(PARSESRC) $(GAMESRC) \
         $(NETSRC) $(TTYSRC) $(LDRSRC) $(MSRC) $(RTSRC) $(CTSRC)
PDIST = $(DISTFILES:%=smug\%)
!ifndef RELEASE
RELEASE = dist
!endif

# Dependency information
OBJDEPENDS = $(DBOBJ) $(INTEROBJ) $(PARSEOBJ) $(GAMEOBJ) $(NETOBJ) \
         $(TTYOBJ) $(LDROBJ) $(MOBJ) $(RTOBJ) $(CTOBJ)

TARGETS = netmud$(EXE) ttymud$(EXE) loader$(EXE) test.db \
       massager$(EXE) runtest$(EXE) comptest$(EXE)

all: $(TARGETS)

netmud$(EXE) : $(NETOBJ) $(GAMEOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

ttymud$(EXE) : $(TTYOBJ) $(GAMEOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

loader$(EXE) : $(LDROBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ)
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

# builds the test database
test.db: init.db loadup.txt tiny.m4 loader$(EXE)
	load.cmd init.db test.db < loadup.txt

# build parser
compiler.c y.tab.h : compiler.y 
	$(YACC) $(YFLAGS) compiler.y
	move y.tab.c compiler.c

# utilities and test programs
massager$(EXE) : $(MOBJ)
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

runtest$(EXE) : $(RTOBJ) $(DBOBJ) $(INTEROBJ) $(PARSEOBJ) 
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

comptest$(EXE) : $(CTOBJ) compiler.obj $(DBOBJ)
	$(LD) $** $(LIBS) /Fe$@ $(LFLAGS)

clean :
	@echo "Cleaning..."
	@-del *.pdb *.ilk *.idb $(OUTFILES) $(TARGETS) $(OBJDEPENDS) 2>NUL
	@echo "Done."

dist : $(DISTFILES)
	@echo "Building distribution..."
	@md smug
	@!copy /y $** smug 1>NUL
	@$(WINZIP) -Pr smug-$(RELEASE).zip smug 1>NUL 2>NUL
	@rd /s /q smug
	@echo "Done."

.c.obj:
	$(CC) $(CFLAGS) /c /Tc$(<:\=/)