##################################################################
#
#   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 = testse starg cosy
SOLIB    = cosy.so
CINT     = M
INCLUDES = -I. -Imain  -Ibase -Icandrv -Iincl -Igui -Ivideodev -Icatalog -Idevdrv
LIBS     = -lpng -lz -L/usr/X11R6/lib -lpthread

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

LIBRARIES = $(SUBDIRS:=.a)

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

.SUFFIXES: .c .cc .h .o 

SRCFILES =

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

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

all: rmlib $(PROGRAMS) $(SOLIB)

# Use $(CXX) -v ... for a more verbose output
#
# 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) $(PROGRAMS:=.o) 
	@echo " Linking $@ ..." 
	$(CXX) $(CXXFLAGS) $(LIBS) $(OBJS) $@.o  $(MARS_LIB) $(ROOTGLIBS) -o $@

$(SOLIB): $(LIBRARIES) $(OBJS) $(HEADERS) 
	@echo " Linking $(SOLIB) ..."
	$(CXX) -shared $(CXXFLAGS) $(SUBDIRS:=/*.o) $(ROOTGLIBS) -o $@
	mv cosy.so lib

$(LIBRARIES):
	@echo " "  
	@echo " Creating lib$@:"
	(cd $*; make -f Makefile; cd ..; mv $*/$@ lib/lib$@) 

dox:
	@echo " Creating html documentation:"
	root -b -q dohtml.C

include Makefile.rules

clean:	rmlib rmcint rmobjs rmcore rmbin

mrproper:	clean rmbak
	@rm -f macros/*~
	@rm -rf htmldoc
	@echo "cd base"
	@cd base; make mrproper; cd ..
	@echo "cd .."
	@echo "cd caos"
	@cd caos; make mrproper; cd ..
	@echo "cd .."
	@echo "cd candrv"
	@cd candrv; make mrproper; cd ..
	@echo "cd .."
	@echo "cd devdrv"
	@cd devdrv; make mrproper; cd ..
	@echo "cd .."
	@echo "cd gui"
	@cd gui; make mrproper; cd ..
	@echo "cd .."
	@echo "cd videodev"
	@cd videodev; make mrproper; cd ..
	@echo "cd .."
	@echo "cd catalog"
	@cd catalog; make mrproper; cd ..
	@echo "cd .."
	@echo "cd main"
	@cd main; make mrproper; cd ..
	@echo "cd .."

links:
	@ln -sf ../../Mars/mbase/MAGIC.h base/MAGIC.h
	@ln -sf ../../Mars/mbase/MLog.h base/MLog.h
	@ln -sf ../../Mars/mbase/MLog.cc base/MLog.cc
	@ln -sf ../../Mars/mbase/MLogManip.h base/MLogManip.h
	@ln -sf ../../Mars/mbase/MLogManip.cc base/MLogManip.cc
	@ln -sf ../../Mars/mbase/MGList.h base/MGList.h
	@ln -sf ../../Mars/mbase/MGList.cc base/MGList.cc
	@ln -sf ../../Mars/mbase/MParContainer.h base/MParContainer.h
	@ln -sf ../../Mars/mbase/MParContainer.cc base/MParContainer.cc
	@ln -sf ../../Mars/mtemp/MObservatory.h base/MObservatory.h
	@ln -sf ../../Mars/mtemp/MObservatory.cc base/MObservatory.cc

magic:
	@ln -sf bending_magic.txt bending.txt
	@ln -sf prepos_magic.txt prepos.txt
	@ln -sf .cosyrc_magic .cosyrc

model:
	@ln -sf bending_model.txt bending.txt
	@ln -sf prepos_model.txt prepos.txt
	@ln -sf .cosyrc_model .cosyrc

tar:	mrproper
	@echo "Making tar-file"
	@tar -cvf ../cosy.tar *
	@gzip -9 ../cosy.tar

# @endcode
