##################################################################
#
#   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 bend
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     \
        tcpip    \
        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 tcpip"
	@cd tcpip; 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/MLogPlugin.h  base/MLogPlugin.h
	@ln -sf ../../Mars/mbase/MLogPlugin.cc base/MLogPlugin.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/MTime.h  base/MTime.h
	@ln -sf ../../Mars/mbase/MTime.cc base/MTime.cc
	@ln -sf ../../Mars/mbase/MParContainer.h  base/MParContainer.h
	@ln -sf ../../Mars/mbase/MParContainer.cc base/MParContainer.cc
	@ln -sf ../../Mars/mbase/MString.h  base/MString.h
	@ln -sf ../../Mars/mbase/MString.cc base/MString.cc
	@ln -sf ../../Mars/mbase/MGMap.h  base/MGMap.h
	@ln -sf ../../Mars/mbase/MGMap.cc base/MGMap.cc
	@ln -sf ../../Mars/mbase/MGMenu.h  base/MGMenu.h
	@ln -sf ../../Mars/mbase/MGMenu.cc base/MGMenu.cc
	@ln -sf ../../Mars/mbase/MString.h  base/MString.h
	@ln -sf ../../Mars/mbase/MString.cc base/MString.cc
	@ln -sf ../../Mars/mgeom/MGeomMirror.h  base/MGeomMirror.h
	@ln -sf ../../Mars/mgeom/MGeomMirror.cc  base/MGeomMirror.cc
	@ln -sf ../../Mars/mgeom/MGeomCam.h  base/MGeomCam.h
	@ln -sf ../../Mars/mgeom/MGeomCam.cc base/MGeomCam.cc
	@ln -sf ../../Mars/mgeom/MGeomPix.h  base/MGeomPix.h
	@ln -sf ../../Mars/mgeom/MGeomPix.cc base/MGeomPix.cc
	@ln -sf ../../Mars/mgeom/MGeomCamMagic.h  base/MGeomCamMagic.h
	@ln -sf ../../Mars/mgeom/MGeomCamMagic.cc base/MGeomCamMagic.cc
	@ln -sf ../../Mars/mastro/MAstro.h  base/MAstro.h
	@ln -sf ../../Mars/mastro/MAstro.cc base/MAstro.cc
	@ln -sf ../../Mars/mastro/MAstroCatalog.h  base/MAstroCatalog.h
	@ln -sf ../../Mars/mastro/MAstroCatalog.cc base/MAstroCatalog.cc
	@ln -sf ../../Mars/mastro/MAstroCamera.h  base/MAstroCamera.h
	@ln -sf ../../Mars/mastro/MAstroCamera.cc base/MAstroCamera.cc
	@ln -sf ../../Mars/mastro/MAstroSky2Local.h  base/MAstroSky2Local.h
	@ln -sf ../../Mars/mastro/MAstroSky2Local.cc base/MAstroSky2Local.cc
	@ln -sf ../../Mars/mastro/MObservatory.h  base/MObservatory.h
	@ln -sf ../../Mars/mastro/MObservatory.cc base/MObservatory.cc
	@ln -sf ../../Mars/mhist/MHCamera.h base/MHCamera.h
	@ln -sf ../../Mars/mhist/MHCamera.cc base/MHCamera.cc
	@ln -sf ../../Mars/mhbase/MH.h base/MH.h
	@ln -sf ../../Mars/mhbase/MH.cc base/MH.cc
	@ln -sf ../../Mars/mhbase/MBinning.h base/MBinning.h
	@ln -sf ../../Mars/mhbase/MBinning.cc base/MBinning.cc
	@ln -sf ../../Mars/mbase/MParList.h base/MParList.h
	@ln -sf ../../Mars/mbase/MParList.cc base/MParList.cc
	@ln -sf ../../Mars/mgui/MHexagon.h base/MHexagon.h
	@ln -sf ../../Mars/mgui/MHexagon.cc base/MHexagon.cc
	@ln -sf ../../Mars/mgui/MCamEvent.h base/MCamEvent.h
	@ln -sf ../../Mars/mgui/MCamEvent.cc base/MCamEvent.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

install:
	@echo Installing cosy to ~stesy/Cosy
	@echo Copy 'cosy'
	@chmod u+w /home/stesy/Cosy/cosy
	@cp cosy /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/cosy
	@echo Copy cosy resources '.cosyrc'
	@chmod u+w /home/stesy/Cosy/.cosyrc
	@cp .cosyrc /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/.cosyrc
	@echo Copy bending model 'bending.txt'
	@chmod u+w /home/stesy/Cosy/bending.txt
	@cp bending.txt /home/stesy/Cosy
	@chmod a-w /home/stesy/Cosy/bending.txt
	@echo Copy LED offsets 'leds.txt'
	@chmod u+w /home/stesy/Cosy/leds.txt
	@cp leds.txt /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/leds.txt
	@echo Copy predefined positions 'prepos.txt'
	@chmod u+w /home/stesy/Cosy/prepos.txt
	@cp prepos.txt /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/prepos.txt
	@echo Copy star catalog 'stars.txt'
	@chmod u+w /home/stesy/Cosy/stars.txt
	@cp stars.txt /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/stars.txt
	@echo Copy PPM catalog
	@chmod u+w /home/stesy/Cosy/ppm9.bin
	@cp ppm9.bin /home/stesy/Cosy/
	@chmod a-w /home/stesy/Cosy/ppm9.bin
	@echo finished...

# @endcode
