# set if you want to use libggi, type also make depend
#USE_GGI=yes

#BinInstallPath=$(ARCH)/
#LibInstallPath=images/
BinInstallPath=/usr/X11R6/bin/
LibInstallPath=/usr/lib/Wormz/

#AllDepths=16
AllDepths=8 16 24

#BinInstallPath=/tmp/Wormz-ggi-alpha-2/bin/
#LibInstallPath=/tmp/Wormz-ggi-alpha-2/lib/

first: all

ifndef TYPEOS
TYPEOS=$(shell uname)
endif

ifeq ($(TYPEOS),Solaris)
ARCH=Solaris
EXTRALIBS=-lsocket -lnsl -lm -lstdc++ -lucb -lposix4
else
ifeq ($(TYPEOS),IRIX)
ARCH=Irix
EXTRALIBS=-lm
else
ifeq ($(TYPEOS),Linux)
ARCH=Linux
EXTRALIBS=-lm
#!!!
#EXTRALIBS=-lefence
else

endif
endif
endif

ifdef USE_GGI
EXTRAGGILIBS=-lggi -lt1
endif

ifdef ARCH

INSTALL=install
INSTALLBIN=install -s
INSTALLDIR=install -d

CC=gcc
CDEFS=-D$(ARCH) -DARCHDIR=\"$(ARCHDIR)\" -DColorDepth=$(Depth)

OPTFLAGS=-O2
#-frerun-loop-opt -freduce-all-givs -fstrength-reduce
#-fomit-frame-pointer -frerun-loop-opt -freduce-all-givs
#-fargument-noalias-global -malign-double
#DEBUGFLAGS=-g3
WARNFLAGS=-Wall -Wcast-qual -Wcast-align -Wconversion -Woverloaded-virtual
#-Winline
#-lefence
CFLAGS=-pipe $(INCLUDE) $(OPTFLAGS) $(DEBUGFLAGS) $(WARNFLAGS) $(CDEFS)
#-Wshadow
X11INCLUDE=-I/usr/openwin/share/include/
STDINCLUDE=-I/usr/local/lib/g++-include/ -I/usr/include/g++/
INCLUDE=-I./include -I. $(X11INCLUDE) $(STDINCLUDE)



ARCHDIR=$(ARCH)/

ifdef USE_GGI
DepthDepenceDirs=Xlib XVision Game Client PaintInterface GGI
DEF_USE_GGI=USE_GGI
else
DepthDepenceDirs=Xlib XVision Game Client PaintInterface
DEF_USE_GGI=DONT_USE_GGI
endif

NotDepthDepenceDirs=Common Network Server Runner
ClientDDD=$(DepthDepenceDirs)
ClientDND=Common Network
AllDirs=$(DepthDepenceDirs) $(NotDepthDepenceDirs)

DirsServer=Common Server Network

DirsRunner=Runner

#TargetNameClient is defined in Runner/Runner.cc too!!!
TargetNameClient=Wormz-client$(Depth)
TargetNameRunner=Wormz
#TargetNameServer is defined in Client/WormApp.cc too!!!
TargetNameServer=Wormz-server
OBJDIR=$(ARCHDIR)

TargetClient=$(OBJDIR)$(TargetNameClient)
TargetServer=$(OBJDIR)$(TargetNameServer)
TargetRunner=$(OBJDIR)$(TargetNameRunner)

SrcsClientDD=$(foreach dir,$(ClientDDD),$(wildcard $(dir)/*.cc))
SrcsClientDDNotDir=$(notdir $(SrcsClientDD))
ObjsClientDDNotDir=$(SrcsClientDDNotDir:.cc=$(Depth).o)
ObjsClientDD=$(addprefix $(OBJDIR),$(ObjsClientDDNotDir))

SrcsClientND=$(foreach dir,$(ClientDND),$(wildcard $(dir)/*.cc))
SrcsClientNDNotDir=$(notdir $(SrcsClientND))
ObjsClientNDNotDir=$(SrcsClientNDNotDir:.cc=.o)
ObjsClientND=$(addprefix $(OBJDIR),$(ObjsClientNDNotDir))

SrcsClient=$(SrcsClientND) $(SrcsClientDD)
ObjsClient=$(ObjsClientND) $(ObjsClientDD)
LibsClient=-L/usr/X11R6/lib/ -L/usr/local/lib/ -lX11 -lXext $(EXTRALIBS) $(EXTRAGGILIBS)

SrcsServer=$(foreach dir,$(DirsServer),$(wildcard $(dir)/*.cc))
SrcsServerNotDir=$(notdir $(SrcsServer))
ObjsServerNotDir=$(SrcsServerNotDir:.cc=.o)
ObjsServer=$(addprefix $(OBJDIR),$(ObjsServerNotDir))
LibsServer=$(EXTRALIBS)

SrcsRunner=$(foreach dir,$(DirsRunner),$(wildcard $(dir)/*.cc))
SrcsRunnerNotDir=$(notdir $(SrcsRunner))
ObjsRunnerNotDir=$(SrcsRunnerNotDir:.cc=.o)
ObjsRunner=$(addprefix $(OBJDIR),$(ObjsRunnerNotDir))
LibsRunner=-L/usr/X11R6/lib/ -L/usr/local/lib/ -lX11

AllDSrcs=$(foreach dir,$(DepthDepenceDirs),$(wildcard $(dir)/*.cc))
AllNSrcs=$(foreach dir,$(NotDepthDepenceDirs),$(wildcard $(dir)/*.cc))

vpath %.cc $(AllDirs)
vpath %.o $(OBJDIR)


$(OBJDIR)%$(Depth).o: %.cc
	$(CC) $(CFLAGS) -c $< -o $@

$(OBJDIR)%.o: %.cc
	$(CC) $(CFLAGS) -c $< -o $@

DEPEND=$(OBJDIR).depend

ifdef Depth
all: $(TargetClient)
else
all: $(TargetRunner) $(TargetServer) Client-depth
endif

depend: Create-PathConfig
	Depth=xxx $(MAKE) real-depend
	
real-depend:
	$(CC) $(CFLAGS) -MM $(AllNSrcs) | \
	awk '/^[^ ]/ { print "$(OBJDIR)" $$0 } /^ / { print $$0 }' > $(DEPEND)
	$(CC) $(CFLAGS) -MM $(AllDSrcs) | \
	awk '/^[^ ]/ { print "$(OBJDIR)" $$0 } /^ / { print $$0 }' > .deptmp
	for i in $(AllDepths); do \
	sed s/\\\.o/$$i\\\.o/g < .deptmp >> $(DEPEND) ; done
	rm .deptmp

clean:
	rm -f $(OBJDIR)*.o $(OBJDIR)Worm*
	rm -f `find . -name \*~`
	
$(TargetClient): $(ObjsClient)
	$(CC) $(CFLAGS) -o $(TargetClient) $(LibsClient) $(ObjsClient)

$(TargetRunner): $(ObjsRunner)
	$(CC) $(CFLAGS) -o $(TargetRunner) $(LibsRunner) $(ObjsRunner)
	
$(TargetServer): $(ObjsServer)
	$(CC) $(CFLAGS) -o $(TargetServer) $(LibsServer) $(ObjsServer)

Client-target: $(TargetClient)

Client-depth:
	for i in $(AllDepths) ; do Depth=$$i $(MAKE) Client-target ; done

PathConfig=include/PathConfig.hh

Create-PathConfig:
	echo '#ifndef _PathConfig_hh_' > $(PathConfig)
	echo '#define _PathConfig_hh_' >> $(PathConfig)
	echo '#define ClientBinInstallPath "$(LibInstallPath)$(TargetNameClient)"' >> $(PathConfig)
	echo '#define ServerBinInstallPath "$(BinInstallPath)$(TargetNameServer)"' >> $(PathConfig)
	echo '#define LibInstallPath "$(LibInstallPath)"' >> $(PathConfig)
	echo '#define '$(DEF_USE_GGI) >> $(PathConfig)
	echo '#endif' >> $(PathConfig)

install-client2:
	$(INSTALLBIN) $(TargetClient) $(LibInstallPath)$(TargetNameClient)
	
install: all
	$(INSTALLDIR) $(BinInstallPath)
	$(INSTALLDIR) $(LibInstallPath)	
	$(INSTALLBIN) $(TargetRunner) $(BinInstallPath)$(TargetNameRunner)
	$(INSTALLBIN) $(TargetServer) $(BinInstallPath)$(TargetNameServer)
	for i in $(AllDepths); do Depth=$$i $(MAKE) install-client2 ; done
	$(INSTALL) images/* $(LibInstallPath)
	
include $(DEPEND)

else
# not def ARCH

all:
	@echo "Sorry, probably unsupported architecture"
	@echo "(enviroment variable TYPEOS not set)"
	@echo "If you have Linux, type: TYPEOS=Linux make <args>"
	@echo "If you have Solaris, type: TYPEOS=Solaris make <args>"
	@echo "If you have Irix, type: TYPEOS=Irix make <args>"
	@echo "Or edit this Makefile"

endif
