##################################################################
#
#   makefile
# 
#   for the COSY software
#
##################################################################
# @maintitle

# @code

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

#
PROGRAMS = cosy telesto
SOLIB    = libcosy.so
CINT     = M
MARS_LIB = libmars.so

INCLUDES = -I. -Imain -Imars -Ibase -Icandrv -Iincl -Ivideodev \
 	   -Igui -Itcpip -Itpoint

#
#  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 = base \
	  candrv \
	  caos \
          catalog \
          devdrv \
          gui \
          main \
          slalib \
          tcpip \
          videodev \
          tpoint

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

#------------------------------------------------------------------------------
.SUFFIXES: .c .cc .h .o .cxx .hxx .gch

SRCFILES = 

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

static: LIBS=$(SUBDIRS:=/*.o) $(OBJS)
#static: rmlib $(LIBRARIES) $(PROGRAMS)
static: $(LIBRARIES) $(PROGRAMS)
	@echo " Done. "
	@echo " "

include Makefile.rules

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

$(PROGRAMS): $(PROGRAMS:=.o) $(SOLIB) $(MARS_LIB)
	@echo " Linking $@ ..." 
	$(CXX) $(CXXFLAGS) $(ROOTGLIBS) $(SOLIB) $(MARS_LIB) $@.o -o $@
#	$(CXX) $(CXXFLAGS) $(ROOTGLIBS) $(SUBDIRS:=/*.o) ../Mars/*/*.o $@.o -o $@

rmcondor:
	rm -f .makecondor.log

condor:	rmcondor $(CONDOR)
	echo Waiting for jobs to finish...
	condor_wait .makecondor.log
	rm .makecondor.log
	make -j2 $(PROGRAMS)
#	find -name "*/.makecondor.*"
#	find -maxdepth 2 -name ".makecondor.*" -exec echo {}\\\; cat {}\\\; echo {}\\\; \;

dox: $(SOLIB)
	@echo
	@echo " Creating html documentation and logfile dohtml.log..."
	rm -f dohtml.log
	root -b -q dohtml.C 2>&1 >> dohtml.log | tee -a dohtml.log
	@echo " done."
	@echo

links:
	mkdir -p mars
	cd mars; ln -sf ../../Mars/*/*.h .
	ln -sf ../Mars/libmars.so .

magic1:
	ln -sf .cosyrc_magic1 .cosyrc
	ln -sf prepos_magic1.txt prepos.txt
	ln -sf leds_magic1.txt leds.txt
	ln -sf stargleds_magic1.txt stargleds.txt
	ln -sf bending_magic1.txt bending.txt

magic2:
	ln -sf .cosyrc_magic2 .cosyrc
	ln -sf prepos_magic2.txt prepos.txt
	ln -sf leds_magic2.txt leds.txt
	ln -sf stargleds_magic2.txt stargleds.txt
	ln -sf bending_magic2.txt bending.txt


#clean:	rmcint rmobjs rmdep rmcore rmlib

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