#
# Whatever you put in for $(CC) must be able to grok ANSI C.
#
CC=gcc
OPTIM= -O -g -pipe -Wall -W -Wno-parentheses -Wno-unused
# 
# To log failed commands (HUH's) to stderr, include -DLOG_FAILED_COMMANDS
# To restricted object-creating commands to users with the BUILDER bit,
#   include -DRESTRICTED_BUILDING
# To log all commands, include -DLOG_COMMANDS
#
DEFS= -DLOG_FAILED_COMMANDS
CFLAGS= $(OPTIM) $(DEFS)

# Everything except interface.c --- allows for multiple interfaces
CFILES= create.c game.c help.c look.c match.c move.c player.c predicates.c \
	rob.c set.c speech.c stringutil.c utils.c wiz.c db.c 

# .o versions of above
OFILES= $(CFILES:.c=.o)

# Files in the standard distribution
DISTFILES= $(CFILES) config.h db.h externs.h interface.h match.h \
	interface.c dump.c sanity-check.c extract.c paths.c \
	help.txt small.db minimal.db restart-cmu README small.db.README \
	Makefile Makefile.bor Makefile.dgm Makefile.vc6 Makefile.lcc \
	copyright.h MANIFEST NOTES CHANGELOG
PDIST= $(patsubst %,tinymud-1.4.1/%,$(DISTFILES))

RELEASE=dist

OUTFILES= netmud dump paths sanity-check extract TAGS

all: extract sanity-check dump paths netmud # TAGS

TAGS: *.c *.h
	etags *.c *.h

netmud: interface.o $(OFILES)
	$(CC) $(CFLAGS) -o netmud interface.o $(OFILES)

dump: dump.o utils.o db.o
	$(CC) $(CFLAGS) -o dump dump.o utils.o db.o

sanity-check: sanity-check.o utils.o db.o
	$(CC) $(CFLAGS) -o sanity-check sanity-check.o utils.o db.o

extract: extract.o utils.o db.o
	$(CC) $(CFLAGS) -o extract extract.o utils.o db.o

paths: paths.o db.o
	$(CC) $(CFLAGS) -o paths paths.o db.o

clean:
	-rm -f *.o *.d a.out core gmon.out $(OUTFILES)

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

# pull in dependency info for *existing* .o files
OBJDEPENDS := $(OFILES) interface.o dump.o sanity-check.o extract.o paths.o
-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