##################################################################
#
#   makefile
# 
#   for the MARS software
#
##################################################################
# @maintitle

# @code

#
#  please change all system depend values in the 
#  config.mk.${OSTYPE} file 
#
#
include Makefile.conf.$(OSTYPE)
include Makefile.conf.general

#

#PROGRAMS = readraw merpp mars test mona status
PROGRAMS = readraw merpp star mars
SOLIB    = libmars.so
CINT     = M

#
#  connect the include files defined in the config.mk file
#
#    WARNING: the result (whether the linkage works or not) depends on the
#             order of the libraries. It seems, that the most base library
#             must be the last one
#

#
#  ----->>>   mars libraries
#
SUBDIRS = mbase \
	  mmain \
          mfilter \
          mdata \
          mhist \
          manalysis \
          mfileio \
          mreflector \
          mgeom \
          mimage \
          mmontecarlo \
          mmc \
          mraw \
          mgui \
          mranforest \
          mhistmc \
          mtools

#          monline \

#LIBRARIES = $(SUBDIRS:%=lib/lib%.a)
LIBRARIES = $(SUBDIRS:=.a)
MRPROPERS = $(SUBDIRS:=.mrproper)
CLEANERS  = $(SUBDIRS:=.clean)

#------------------------------------------------------------------------------

.SUFFIXES: .c .cc .h .o 

SRCFILES =

SRCS    = $(SRCFILES)
HEADERS = $(SRCFILES:.cc=.h)
OBJS    = $(SRCFILES:.cc=.o) 

############################################################

all: rmlib $(PROGRAMS) $(SOLIB)
	@echo " Done. "
	@echo " "

include Makefile.rules
include Makefile.depend

# Use $(CXX) -v ... for a more verbose output
#
#
#$(MARS_LIB)
$(SOLIB): $(LIBRARIES) $(OBJS) $(HEADERS) MCint.o
	@echo " Linking $(SOLIB) ..."
	$(CXX) $(DYNLIB) $(CXXFLAGS) $(SUBDIRS:=/*.o) $(ROOTGLIBS) -o $@

$(PROGRAMS): $(SOLIB) $(PROGRAMS:=.o)
	@echo " Linking $@ ..." 
	$(CXX) $(CXXFLAGS) $(ROOTGLIBS) $(SOLIB) $@.o $(MARS_LIB) -o $@

#
# We could link mars.so instead of all libraries. This would need
# some MBs less space on the HD. But this means, that the Shared
# Library Path in your system must be set properly to be able to start
# 'mars'
#
#$(PROGRAMS): $(LIBRARIES) $(OBJS) $(HEADERS) $(SOLIB) MCint.o $(PROGRAMS:=.o) 
#	@echo " Linking $@ ..." 
#	$(CXX) $(CXXFLAGS) $@.o $(OBJS) lib/$(SOLIB)  MCint.o $(ROOTGLIBS) -o $@

#
# For later use
#
#$(PROGRAMS): $(LIBRARIES) $(OBJS) $(HEADERS) MCint.o $(PROGRAMS:=.o) 
#	@echo " Linking $@ ..." 
#	$(CXX) $(CXXFLAGS) $@.o $(OBJS) $(MARS_LIB) -L/usr/X11R6/lib -lX11 -lXpm MCint.o $(ROOTGLIBS) -o $@
#
#$(SOLIB): $(LIBRARIES) $(OBJS) $(HEADERS) MCint.o
#	@echo " Linking $(SOLIB) ..."
#	$(CXX) -shared $(CXXFLAGS) $(SUBDIRS:=/*.o) $(ROOTGLIBS) -o $@
#	mv mars.so lib

$(LIBRARIES):
	@echo " Creating lib$@:"
	(cd $*; make; cd ..; mv $*/$@ lib/lib$@)
#	(cd $(@:lib/lib%.a=%); make; cd ..; mv $(@:lib/lib%.a=%)/$(@:lib/lib%=%) $@)

$(MRPROPERS):
	@echo " Doing Mr.Proper in $(@:.mrproper=)"
	(cd $(@:.mrproper=); (make mrproper > /dev/null); cd ..;) 

$(CLEANERS):
	@echo "Cleaning $(@:.clean=):"
	(cd $(@:.clean=); make clean; cd ..;) 

dox: $(SOLIB)
	@echo " Creating html documentation:"
	root -b -q dohtml.C

clean:	rmmake rmcint rmobjs rmcore rmlib

mrproper:	$(MRPROPERS) rmmake rmbin rmbak rmbakmac rmhtml clean
	@echo " Done."
	@echo " "

tar:	mrproper
	@echo "Making tar-file"
	@root -b -q -l -n tar.C
#	@tar cvf ../mars.tar --exclude=Root .rootrc *
#	@gzip -9 ../mars.tar

Makefile.depend:
	(! find ./ Makefile.depend -maxdepth 1 -empty 2> /dev/null && \
	echo " Generating dependancies into Makefile.depend" && \
	makedepend -- $(INCLUDES) -- $(PROGRAMS:=.cc) $(SRCS) $(SUBDIRS:=/*.cc) -w1024 -f- 2> /dev/null | grep -v Cint | grep -v "/usr/" > Makefile.depend && \
	echo " ") || find -maxdepth 0 -true > /dev/null

depend:	Makefile.depend	

# @endcode
