# Makefile for Tunnel Fighter
#
# (C) 2000 W.C.A. Wijngaards
# 
# Mostly it will work as is, if you set the CRYSTAL environment variable.
# on windows you will have to edit CSDIR, OUTDIR, and DLLEXT
# on some systems (e.g. NeXT) edit LIBS as well.

# determine directory where Crystal Space is installed
# if CRYSTAL is undefined use /usr/local/crystal
ifeq ($(strip $(CRYSTAL)),)
  CSDIR=/usr/local/crystal
else
  CSDIR=$(CRYSTAL)
endif

# Fill in a manual crystalspace location here, if need be
#CSDIR=../CS

# subdirectory where CS stores the libraries
OUTDIR1=lib/
OUTDIR2=$(firstword $(strip $(patsubst $(CSDIR)/%, %, $(sort $(dir \
  $(wildcard $(CSDIR)/out/*/*/*/*.a))))))

#manual library dir for crystalspace
#OUTDIR2=out/LINUX/INTEL/debug

# file extension of plug-in modules (often .so for Unix, .dll for Windows)
DLLEXT=.so
# does your system have a strdup? Most systems have, otherwise uncomment.
# TFDEFS= -DMISSING_STRDUP

# png, jpeg and zlib locations and include. Note $(CS)/libs/libjpeg,
# $(CS)/libs/libpng and $(CS)/lib/zlib can be used, but your system may
# have the libraries installed differently.
PNG.LIB= -L/usr/lib -lpng
PNG.INCLUDE= -I/usr/include
JPEG.LIB= -L/usr/lib -ljpeg
JPEG.INCLUDE= -I/usr/include
ZLIB.LIB= -L/usr/lib -lz
ZLIB.INCLUDE= -I/usr/include

#
# Unix/Linux Defs 
#
CSDEFS=
CSLIBS = `./cs-config --libs csgeom csgfx cssys cstool csutil`
LIBS= $(CSLIBS) -lm
# For Apple/NeXT, comment out above line and uncomment next line.
# $(PNG.LIB) $(JPEG.LIB) $(ZLIB.LIB) -lNeXT_s -lsys_s

# uncomment this on unixlike systems (linux, solaris)
#LIBS+=-ldl

LIBDIRS=-L/usr/lib -L/usr/local/lib -L$(CSDIR) -L$(CSDIR)/$(OUTDIR1) \
   -L$(CSDIR)/$(OUTDIR2)

INCLUDE=-Isrc -I. -I$(CSDIR)/. -I$(CSDIR)/apps -I$(CSDIR)/include \
  -I$(CSDIR)/libs -I$(CSDIR)/support  $(PNG.INCLUDE) $(JPEG.INCLUDE) \
  $(ZLIB.INCLUDE) 

# C flags: general, pentium, and debug flags
CPPFLAGS=-Wall -Wunused -W \
  $(INCLUDE) $(CSDEFS) $(TFDEFS)
CPPFLAGS+=-g
#CPPFLAGS+=-mpentium -march=i586
CPPFLAGS+=-fno-exceptions -fno-rtti 

# uncomment this line for mingw32 compilation
CPPFLAGS+=-fvtable-thunks

#CPPFLAGS+=-fomit-frame-pointer
#CPPFLAGS+=-O6
#CPPFLAGS+=-pg

# compiler to use
CCC=g++
#how to make depend
#HOWTODEPEND=unixdepend
HOWTODEPEND=windepend
MAKEDEPEND=makedepend
#WINMAKEDEPEND=gcc -MM
WINMAKEDEPEND=$(CSDIR)/makedep -f-
#the copy command for copyfiles.
COPY=cp

#old kludge to get 'missing symbols' linked in. These .o files are also linked.
LINKOBJS= 

#########################################################################
# You probably will not need to modify anything below

#what comprises the prog
OBJSRC=$(wildcard src/*.cpp src/*/*.cpp)
OBJH=$(wildcard src/*.h src/*/*.h)
OBJS=$(addsuffix .o, $(basename $(OBJSRC)))

# name of final prog.
EXECUTABLE=tunnel
#
# targets
#
.SUFFIXES: .cpp
.cpp.o: 
	$(CCC) $(CPPFLAGS) -o $@ -c $<

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJS)
	$(CCC) -o $(EXECUTABLE) $(CPPFLAGS) $(LIBDIRS) $(OBJS) $(LINKOBJS) $(LIBS)

run: all
	./$(EXECUTABLE)

debug: all
	gdb --directory $(CSDIR) $(EXECUTABLE)

clean:
	$(RM) src/*.o

cleancopyfiles:
	$(RM) *$(DLLEXT)
	$(RM) `ls *.cfg data/config/*.cfg | sed -e '/tunnel/d'`

distclean: clean cleancopyfiles
	$(RM) $(EXECUTABLE)

# the sed command put $(CSDIR) in the depenency file, and
# removes all other files; from /usr etc.; first .h then .cpp and then
# empty lines. (Note that weird CSDIR's will confuse sed)
depend: $(HOWTODEPEND)

unixdepend:
	touch temp.dep
	$(MAKEDEPEND) $(INCLUDE) -ftemp.dep -- src/*.cpp 
	sed -e 's?'$(CSDIR)'?$$(CSDIR)?g' temp.dep | \
	sed -e 's? [^s$$].*.h??g' | sed -e 's? [^s$$].*.cpp??g' | \
	sed -e '/:$$/d' > makefile.dep
	$(RM) temp.dep temp.dep.bak

windepend:
	$(WINMAKEDEPEND) $(INCLUDE) src/*.cpp > makefile.dep

copyfiles: cleancopyfiles
	$(COPY) $(CSDIR)/*$(DLLEXT) .
	$(COPY) $(CSDIR)/*.cfg .
	$(COPY) $(CSDIR)/data/config/*.cfg data/config/
	echo Note - copy png.dll z.dll jpeg.dll

bak:
	cd ..; tar czvf tunnel.tgz tunnel
backup:
	tar czvf ../tfbak.tgz README.txt LICENSE CHANGES \
	"Tunnel Fighter.dsp" Makefile makefile.dep data/config/tunnel.cfg \
	data/tflib* src/*.cpp src/*.h

# include dependencies
-include makefile.dep
