##################################################################
#
#   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
SOLIB    = mars.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 = mmain       \
          mmc         \
          mgui        \
	  manalysis   \
          mbase       \
          meventdisp  \
          mdatacheck  \
          mmontecarlo \
          mhist       \
          mdata       \
          mfilter     \
          mraw

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 " "

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

$(PROGRAMS): $(LIBRARIES) $(OBJS) $(HEADERS) MCint.o $(PROGRAMS:=.o) 
	@echo " Linking $@ ..." 
	$(CXX) $(CXXFLAGS) $@.o $(OBJS) $(MARS_LIB) MCint.o $(ROOTGLIBS) -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 $@

$(LIBRARIES):
	@echo " Creating lib$@:"
	(cd $*; make; cd ..; mv $*/$@ 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

include Makefile.rules

clean:	rmcint rmobjs rmcore rmlib

mrproper:	$(MRPROPERS) 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

# @endcode
