#
# Makefile for CoolMud
#
CC=gcc
LD=gcc
AR = ar
CPP = gcc -E -P -x c
#YACC = bison -y
YACC = byacc 

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

DEFS = -DMETHOD_DEBUG # -DCACHE_DEBUG -DDBMCHUNK_DEBUG
OPTIM = -O2 -fno-strict-aliasing 
WARN = -Wall -Wextra -Wno-parentheses -Wno-unused 
PROF = -g

# source and bin directories
S = src/
B = bin/

CFLAGS= $(OPTIM) $(WARN) $(DEFS) $(PROF)
LFLAGS = $(OPTIM) $(PROF)

INCS = -I$(S)ndbm
LIBS = -lcrypt -lndbm 

NDBM_SRC = $(S)ndbm/hash.c $(S)ndbm/hash_bigkey.c $(S)ndbm/hash_buf.c \
	$(S)ndbm/hash_func.c $(S)ndbm/hash_log2.c $(S)ndbm/hash_page.c \
	$(S)ndbm/ndbm.c 
NDBM_OBJ = $(NDBM_SRC:.c=.o)
NDBM_LIB = $(S)ndbm/libndbm.a
NDBM_HDRS = $(S)ndbm/db.h $(S)ndbm/ndbm.h $(S)ndbm/hash.h \
	$(S)ndbm/page.h $(S)ndbm/extern.h 
NDBM_DOC = $(S)ndbm/ndbm.html $(S)ndbm/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.o $(CFILES:.c=.o) 

HDRS =  $(S)buf.h $(S)code.h $(S)cool.h $(S)db_config.h $(S)db_setup.h \
	$(S)execute.h $(S)netio.h $(S)netio_private.h $(S)proto.h \
	$(S)servers.h $(S)servers_private.h $(S)string.h \
	$(S)os.h

BINFILES = $(B)README  $(B)TODO $(B)restart $(B)mud_in $(B)shutdownmud \
	$(B)online/help/intro.txt $(B)online/news/intro.txt \
	$(B)west.cl $(B)west.cfg $(B)west.welcome $(B)west.precompiled \
	$(B)east.cl $(B)east.cfg $(B)east.welcome $(B)east.precompiled \
	$(B)boot.clh $(B)common.clh $(B)cm.core $(B)atscript \
	$(B)checkpoint $(B)help_in 

DOC = doc/README doc/cool-prog-man-2.2.0-prelim.pdf doc/cooldocs.texi doc/distrib \
	doc/messages doc/verbs doc/YO doc/cool-prog-man-2.2.0-prelim.ps \
	doc/cooldocs.txt doc/exceptions doc/methods doc/assumptions \
	doc/cool-prog-man-2.2.0-prelim.tex doc/coolmud.l doc/functions \
	doc/permissions doc/compile.l doc/cooldocs.ps doc/datatypes \
	doc/locking doc/variables doc/getfuncs doc/listfuncs doc/cool_docs \
	doc/fixlist doc/cooldocs.pdf doc/makefunclist doc/ps          

# Files in the standard distribution
DISTFILES= $(CFILES) $(HDRS) $(BINFILES) $(DOC) \
 	README CHANGES CHANGELOG COPYING COPYRIGHT CREDITS INSTALL_NOTES \
	Makefile Makefile.bor Makefile.vc Makefile.dgm $(S)cool.y \
	$(NDBM_SRC) $(NDBM_HDRS) $(NDBM_DOC)

OBJDEPENDS := $(OFILES) $(NDBM_OBJ)

PDIST= $(patsubst %,coolmud/%,$(DISTFILES))
ifndef RELEASE
RELEASE=dist
endif

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

all: $(OUTFILES)

$(NDBM_LIB): $(NDBM_OBJ)
	$(AR) rsc $@ $^

$(B)cm$(EXE): $(NDBM_LIB) $(OFILES) 
	$(LD) $(LFLAGS) -o$@ $^ -L$(S)ndbm $(LIBS)

$(B)west.db: $(B)cm.core 
	rm -f west.tmp
	for file in $?; do cat $$file >> west.tmp; done
	cp $(B)west.cl $(B)west.cl.c
	$(CPP) $(B)west.cl.c | grep -v '^#' >>west.tmp
	rm $(B)west.cl.c
	$(B)cm$(EXE) $(B)west -i -f west.tmp

$(B)east.db: $(B)cm.core 
	rm -f east.tmp
	for file in $?; do cat $$file >> east.tmp; done
	cp $(B)east.cl $(B)east.cl.c
	$(CPP) $(B)east.cl.c | grep -v '^#' >>east.tmp
	rm $(B)east.cl.c
	$(B)cm$(EXE) $(B)east -i -f east.tmp

$(S)cool.c $(S)y.tab.h : $(S)cool.y 
	$(YACC) -l -d $^
	mv y.tab.c $(S)cool.c
	mv y.tab.h $(S)y.tab.h

clean:
	rm -f $(OBJDEPENDS) $(OBJDEPENDS:.o=.d) $(S)y.tab.h $(S)cool.c gmon.out \
	$(B)core $(OUTFILES) $(B)west.db $(B)east.db $(B)west.chunk $(B)east.chunk $(B)*.log

dist:
	ln -s ./ coolmud
	tar czvf coolmud-2.3-$(RELEASE).tar.gz $(PDIST)
	rm coolmud

# pull in dependency info for *existing* .o files
-include $(OBJDEPENDS:.o=.d)

# compile and generate dependency info;
# more complicated dependency computation, so all prereqs listed
# will also become command-less, prereq-less targets
#   sed:    append directory to object target. (gcc bug?)
#   sed:    strip the target (everything before colon)
#   sed:    remove any continuation backslashes
#   fmt -1: list words one per line
#   sed:    strip leading spaces
#   sed:    add trailing colons
%.o: %.c
	$(CC) -c $(CFLAGS) $(INCS) $*.c -o$*.o
	@$(CC) -MM $(CFLAGS) $(INCS) $*.c > $*.d
	@mv -f $*.d $*.d.tmp
	@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
	@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
	  sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
	@rm -f $*.d.tmp