#
# v1.1
#
# Space Makefile
#
# Cameron K. Smith, September 1997
# 
# Dependancy code added by Mark Cooke
#
# This program is free software and may be freely redistributed as 
# specified in the GNU General Public License.  Please see the file
# 'COPYING' for details.
#

# User configurable portion

###############################################################################
#                You should not have to edit below this point.                #
###############################################################################

# Setup the C and C++ compilers as necessary. Note CPP is only used for
# compiling platform.c when using MUX2 currently.
CC  = gcc
CXX = g++ -D__CPP 

# Uncomment the next line for debugging.
#CEXTRAS = -Wpointer-arith -Wwrite-strings -Wshadow -Wcast-qual

CFLAGS = $(INCDIR) $(MUDCFLAGS) -O3 -g -Wall $(CEXTRAS)

# Used for testing. 
#CFLAGS = -Wall -Wpointer-arith -Wcast-qual -Wconversion -Wstrict-prototypes \
#	-Wmissing-prototypes -Wmissing-declarations $(INCDIR) $(MUDCFLAGS)

BASE_OBJS =	commands.o events.o btree.o object.o sensors.o damage.o \
		eng.o tactical.o nav.o shields.o comm.o smain.o smisc.o \
		platform.o scmlib.o scm.o class.o flag.o rxllib.o rxl.o \
		output.o

BASE_SRC =	commands.c events.c btree.c object.c sensors.c damage.c \
		eng.c tactical.c nav.c shields.c comm.c smain.c smisc.c \
		platform.c scm.c class.c flag.c rxl.c output.c

OBJS = $(BASE_OBJS)

# Build the list of modules in the scm directory dynamically. That way custom
# extras should be linked in automatically.

SCM_C    = $(shell ls scm/scm_*.c )
SCM_O    = $(shell ls scm/scm_*.c | sed -e"s/\\.c/\\.o/g" )
SCM_BASE = $(shell ls scm/scm_*.c | sed -e"s/\\.c//g" -e"s/^scm\///g" )

# Build the list of modules in the range directory dynamically. That way custom
# extras should be linked in automatically.

RXL_C    = $(shell ls rxl/rxl_*.c )
RXL_O    = $(shell ls rxl/rxl_*.c | sed -e"s/\\.c/\\.o/g" )
RXL_BASE = $(shell ls rxl/rxl_*.c | sed -e"s/\\.c//g" -e"s/^rxl\///g" )

all:
	@echo The engine must be compiled along with the MU* server. The \
	Makefile in the
	@echo server directory should automatically do this.

engine:	do-it-all

space.o: $(OBJS)
	$(LD) -r -o space.o $(OBJS)

# Yes it's ugly. Yes it could probably be done better.
# It's in the 'works for me' category. USECPP is defined
# in the makefile for servers that need to use C++ for
# making platform.c
platform.o: platform.c pseint.h
	-if test "${USECPP}X" = "1X" ; then \
		$(CXX) $(CFLAGS) -c platform.c -o platform.o ; \
	else \
		$(CC)  $(CFLAGS) -c platform.c -o platform.o; \
	fi

clean:
	-rm -f *.o scm/*.o scmlib.h scm.def unsplit

unsplit: unsplit.c

depend: $(BASE_SRC) $(SCM_C) unsplit scmlib.h scm.def rxl.def rxllib.h
	for i in $(BASE_SRC) $(SCM_C) ; do $(CC) -E $(CFLAGS) -M $$i; done | sed -e 's:/usr[^ ]* ::g' | ./unsplit > .depend~
	mv .depend~ .depend

distclean: clean
	-rm -f .depend

scmlib.h: $(SCM_C) scm/scm.h
	@echo "/* Interface structures for the plugins */" > scmlib.h
	@for i in $(SCM_BASE); do echo "extern SCM_INTERFACE" $$i";" >> scmlib.h; done

scm.def: $(SCM_C) scm/scm.h
	@echo "static SCM_INTERFACE* scm_plugins[]= {" > scm.def
	@for i in $(SCM_BASE); do echo "&"$$i"," >> scm.def ; done
	@echo "NULL };" >> scm.def

scmlib.o: $(SCM_O) scm/scm.h
	$(LD) -r -o scmlib.o $(SCM_O)

scm.o: scm.def scmlib.h scm.c scmlib.o scm/scm.h

rxllib.h: $(RXL_C) rxl/rxl.h
	@echo "/* Interface structures for the range xlators */" > rxllib.h
	@for i in $(RXL_BASE); do echo "extern RXL_INTERFACE" $$i";" >> rxllib.h; done

rxl.def: $(RXL_C) rxl/rxl.h
	@echo "static RXL_INTERFACE* rxl_plugins[]= {" > rxl.def
	@for i in $(RXL_BASE); do echo "&"$$i"," >> rxl.def ; done
	@echo "NULL };" >> rxl.def

rxllib.o: $(RXL_O) rxl/rxl.h
	$(LD) -r -o rxllib.o $(RXL_O)

rxl.o: rxl.def rxllib.h rxl.c rxllib.o rxl/rxl.h

ifeq (.depend,$(wildcard .depend))
include .depend
do-it-all:      space.o
else
do-it-all:      depend space.o
endif
