#
# Makefile for CoolMud
#
CC=gcc
LD=gcc
CPP = gcc -E -x c
YACC = bison -y
OPTIM= -g -O2 -pipe -Wall -W -Wno-parentheses -Wno-unused

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

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

CFLAGS= $(OPTIM)
LDFLAGS = -g 

# You may need to change this.  Sh
LIBS = -lcrypt -lgdbm_compat

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

DBPARTS = $(B)root_obj.cl $(B)sys_obj.cl $(B)located_obj.cl $(B)described_obj.cl \
	$(B)thing.cl $(B)container.cl $(B)room.cl $(B)exit.cl $(B)player.cl \
	$(B)builder.cl $(B)programmer.cl $(B)wizard.cl

BINFILES = $(DBPARTS)	$(B)README  $(B)restart  \
	$(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

DOC = doc/README doc/cool-prog-man-2.2.0-prelim.dvi 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

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

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

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

all: $(OUTFILES)

$(B)cm$(EXE): $(OFILES)
	$(LD) $(LDFLAGS) -o $@ $^ $(LIBS)

$(B)west.db: $(DBPARTS) $(B)west.cl
	rm -f west.tmp
	for file in $?; do $(CPP) $$file >> west.tmp; done
	$(B)cm$(EXE) $(B)west -i -f west.tmp
	rm -f west.tmp

$(B)east.db: $(DBPARTS) $(B)east.cl
	rm -f east.tmp
	for file in $?; do $(CPP) $$file >> east.tmp; done
	$(B)cm$(EXE) $(B)east -i -f east.tmp
	rm -f east.tmp

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

clean:
	rm -f $(S)*.d $(S)y.tab.h $(S)cool.c gmon.out $(B)/core $(OUTFILES) $(OFILES) \
    $(B)*.db $(B)*.dir $(B)*.pag $(B)*.log

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

# pull in dependency info for *existing* .o files
OBJDEPENDS := $(OFILES) 
-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) $*.c -o $*.o
	@$(CC) -MM $(CFLAGS) $*.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
