##################################################################
#
#   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 -Imars
LIBS     = -lpng -lz -L/usr/X11R6/lib -lpthread libmars.so

#
#  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 /home/caos/March06/Mars/*/*.h mars
	@ln -sf /home/caos/March06/Mars/libmars.so

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
