CXX	=@CXX@
CC	=@CC@
CFLAGS	= -Wall -O2 -march=@target_cpu@ -pipe -fomit-frame-pointer
X_CFLAGS=@X_CFLAGS@
X_LIBS  =@X_LIBS@
LIBS	= -lpthread -lrt -lqt-mt -lm
QTDIR   =@QTDIR@
QT_CXXFLAGS=@QT_CXXFLAGS@
QT_LIBS =@QT_LIBS@
MOC     =$(QTDIR)/bin/moc
UIC     =$(QTDIR)/bin/uic
RS485DIR=@RS485DIR@
RS485_CFLAGS=@RS485_CFLAGS@
SCIENCE	=science
CFLAGS += -I$(SCIENCE) -I.
QTATTIC	=qtattic
EXTRA_CFLAGS=@EXTRA_CFLAGS@
INSTALL	=@INSTALL@
prefix	=@prefix@
exec_prefix=@exec_prefix@
bindir	=@bindir@
TARGET	=turbina
T_GRAPH =turgraph
T_ATMOS	=atmos
T_CNT2ASC=cnt2asc
T_WEIF	=weif
T_XAUTH =xauth.turbina
T_OBJ	=main.o access.o auxiliary.o bicounter.o cfg.o cfgdialog.o counter.o datathr.o device.o exchange.o graph.o iocount.o light.o messages.o module.o motor.o mywidget.o outcontrol.o power.o procounter.o readmass.o rserrno.o rsignal.o scanmotor.o scenariothr.o star.o summary.o svexchange.o tools.o tracker.o trberror.o trbexport.o trbscenario.o trbsocket.o trbsv.o trbthread.o trbtime.o tree.o unroll.o viewstar.o widgets.o @EXTRA_T_OBJ@
T_MOCOBJ=moc_cfgdialog.o moc_device.o moc_graph.o moc_mywidget.o moc_outcontrol.o moc_star.o moc_trbtime.o moc_tree.o moc_viewstar.o moc_widgets.o @EXTRA_T_MOCOBJ@
T_MOCCPP=moc_cfgdialog.cpp moc_device.cpp moc_graph.cpp moc_mywidget.cpp moc_outcontrol.cpp moc_star.cpp moc_trbtime.cpp moc_tree.cpp moc_viewstar.cpp moc_widgets.cpp @EXTRA_T_MOCCPP@
T_LIBS	=$(SCIENCE)/libscience.a

all: all-recursive $(TARGET) $(T_XAUTH)

all-recursive:
	make -C $(SCIENCE)

utils: all-recursive $(T_ATMOS) $(T_CNT2ASC) $(T_WEIF) $(T_GRAPH)

$(T_GRAPH): turgraph.o $(T_LIBS)
	$(CXX) $(X_LIBS) $(QT_LIBS) $(LIBS) turgraph.o $(T_LIBS) -o $(T_GRAPH)

turgraph.o: turgraph.cpp turgraph.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $< -o $@

$(T_ATMOS): $(SCIENCE)/atmos.cpp readmass.o $(T_LIBS)
	$(CXX) $(CFLAGS) -DATMOSTEST $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $^ -o $@

$(T_WEIF): $(SCIENCE)/weif.cpp $(T_LIBS)
	$(CXX) $(CFLAGS) -DWEIFTEST $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $^ -o $@

$(T_CNT2ASC): cnt2asc.cpp iocount.o $(T_LIBS)
	$(CXX) $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $^ -o $@

$(T_XAUTH): xauth.c
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $^ -o $@

clean: clean-recursive
	rm -f moc_*.cpp
	rm -f moc_*.o
	rm -f *.o

clean-recursive:
	make -C $(SCIENCE) clean

install:
	$(INSTALL) -D -m 6755 $(TARGET) $(DESTDIR)$(bindir)/$(TARGET)
	$(INSTALL) -D -m 6755 $(T_XAUTH) $(DESTDIR)$(bindir)/$(T_XAUTH)
	cp -r ../etc $(DESTDIR)$(prefix)
	cp -r ../doc $(DESTDIR)$(prefix)
	mkdir -p $(DESTDIR)$(prefix)/share
	mkdir -p $(DESTDIR)$(prefix)/data/log $(DESTDIR)$(prefix)/data/out $(DESTDIR)$(prefix)/data/weight $(DESTDIR)$(prefix)/data/working
	cp -r ../data/lut $(DESTDIR)$(prefix)/data/lut
	cp -r ../data/spectra $(DESTDIR)$(prefix)/data/spectra
	cp -r ../data/data $(DESTDIR)$(prefix)/data/data

install-utils:
	$(INSTALL) -D -m 755 $(T_ATMOS) $(DESTDIR)$(bindir)/$(T_ATMOS)
	$(INSTALL) -D -m 755 $(T_CNT2ASC) $(DESTDIR)$(bindir)/$(T_CNT2ASC)
	$(INSTALL) -D -m 755 $(T_WEIF) $(DESTDIR)$(bindir)/$(T_WEIF)
	$(INSTALL) -D -m 755 $(T_GRAPH) $(DESTDIR)$(bindir)/$(T_GRAPH)
	mkdir -p $(DESTDIR)$(prefix)/share
	cp -r ../rescripts $(DESTDIR)$(prefix)/share/rescripts

install-permissions:
	/usr/sbin/groupadd -r mass || /bin/true
	/usr/sbin/useradd -r -g mass -s /bin/false -c "MASS account" -d $(prefix)/data mass || /bin/true
	/usr/sbin/usermod mass -A uucp || /bin/true
	chown -R mass.mass $(DESTDIR)/$(prefix)
	find $(DESTDIR)/$(prefix)/data -type d -exec chmod 0775 {} \;
	chmod 4755 $(DESTDIR)$(bindir)/$(TARGET)

uninstall:
	rm -f $(bindir)/$(TARGET)
	rm -f $(bindir)/$(T_XAUTH)

uninstall-utils:
	rm -f $(bindir)/$(T_ATMOS)
	rm -f $(bindir)/$(T_CNT2ASC)
	rm -f $(bindir)/$(T_WEIF)
	rm -f $(bindir)/$(T_GRAPH)

$(TARGET): $(T_LIBS) $(T_MOCCPP) $(T_MOCOBJ) $(T_OBJ)
	$(CXX) $(X_LIBS) $(QT_LIBS) $(T_OBJ) $(T_MOCOBJ) $(LIBS)  $(T_LIBS) -o $(TARGET)

moc_%.cpp: $(QTATTIC)/%.h
	$(MOC) $< -o $@

%.o: $(QTATTIC)/%.cpp
	$(CXX) -c $(CFLAGS)  $(QT_CXXFLAGS) $< -o $@

moc_%.cpp: %.h
	$(MOC) $< -o $@

$(T_LIBS):
	make -C $(SCIENCE)

moc_%.o: moc_%.cpp
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

access.o: access.cpp access.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

auxiliary.o: auxiliary.cpp cfg.h exchange.h auxiliary.h messages.h names.h tools.h trberror.h module.h light.h power.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

bicounter.o: bicounter.cpp cfg.h bicounter.h exchange.h iocount.h messages.h names.h tools.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

cfg.o: cfg.cpp cfg.h color.h messages.h names.h readmass.h tools.h trbconst.h trberror.h trbtime.h tree.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

cfgdialog.o: cfgdialog.cpp cfgdialog.h color.h device.h names.h outcontrol.h tools.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

counter.o: counter.cpp cfg.h counter.h exchange.h iocount.h messages.h names.h tools.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

datathr.o: datathr.cpp bicounter.h exchange.h datathr.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

device.o: device.cpp access.h $(SCIENCE)/atmos.hxx cfg.h bicounter.h device.h exchange.h auxiliary.h messages.h motor.h scanmotor.h names.h $(SCIENCE)/nrutil.h outcontrol.h readmass.h $(SCIENCE)/scan.hxx $(SCIENCE)/scind.hxx star.h summary.h tools.h tracker.h trbconst.h trberror.h trbexport.h trbtime.h unroll.h module.h $(SCIENCE)/weif.hxx channel.h light.h power.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

exchange.o: exchange.cpp exchange.h trbconst.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

graph.o: graph.cpp $(SCIENCE)/atmos.hxx cfg.h graph.h messages.h names.h $(SCIENCE)/nrutil.h outcontrol.h summary.h tools.h tree.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

iocount.o: iocount.cpp iocount.h $(SCIENCE)/nrutil.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

light.o: light.cpp light.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

main.o: main.cpp access.h cfg.h device.h exchange.h graph.h mywidget.h names.h outcontrol.h star.h trbexport.h trbtime.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

messages.o: messages.cpp messages.h trberror.h trbexport.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

module.o: module.cpp cfg.h exchange.h module.h messages.h names.h tools.h trberror.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

motor.o: motor.cpp cfg.h exchange.h messages.h motor.h names.h tools.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

mywidget.o: mywidget.cpp buttons.xpm cfg.h cfgdialog.h device.h diaphragm.xpm messages.h mywidget.h names.h star.h tools.h trbexport.h trbtime.h tree.h widgets.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

outcontrol.o: outcontrol.cpp cfg.h messages.h names.h $(SCIENCE)/nrutil.h outcontrol.h summary.h tools.h trberror.h trbexport.h tree.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

power.o: power.cpp power.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

procounter.o: procounter.cpp cfg.h procounter.h exchange.h iocount.h messages.h names.h tools.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

qtmultilineedit.o: $(QTATTIC)/qtmultilineedit.cpp $(QTATTIC)/qtmultilineedit.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

qttableview.o: $(QTATTIC)/qttableview.cpp $(QTATTIC)/qttableview.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

readmass.o: readmass.cpp readmass.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

rserrno.o: rserrno.c
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

rsignal.o : rsignal.c
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

scanmotor.o: scanmotor.cpp cfg.h exchange.h messages.h scanmotor.h names.h tools.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

scenariothr.o: scenariothr.cpp scenariothr.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

star.o: star.cpp $(SCIENCE)/atmos.hxx cfg.h messages.h names.h star.h tools.h trbconst.h trberror.h trbtime.h $(SCIENCE)/weif.hxx
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

summary.o: summary.cpp $(SCIENCE)/atmos.hxx $(SCIENCE)/nr.h $(SCIENCE)/scind.hxx summary.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

svexchange.o: svexchange.cpp svexchange.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

tools.o: tools.cpp tools.h trbconst.h trbtime.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

tracker.o: tracker.cpp cfg.h exchange.h messages.h names.h tools.h tracker.h trberror.h module.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trberror.o: trberror.cpp trberror.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbexport.o: trbexport.cpp names.h trbexport.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbscenario.o: trbscenario.cpp trbscenario.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbsocket.o: trbsocket.cpp trbsocket.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbsv.o: trbsv.cpp trbsv.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbthread.o: trbthread.cpp trbthread.h trbscenario.h scenariothr.h trbsv.h datathr.h trbsocket.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

trbtime.o: trbtime.cpp cfg.h messages.h tools.h trbconst.h trberror.h trbtime.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

tree.o: tree.cpp access.h tools.h tree.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

unroll.o: unroll.cpp $(SCIENCE)/nrutil.h unroll.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

viewstar.o: viewstar.cpp star.h viewstar.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@

widgets.o: widgets.cpp cfg.h device.h exchange.h messages.h names.h star.h tools.h trbexport.h viewstar.h widgets.h
	$(CXX) -c $(CFLAGS) $(EXTRA_CFLAGS) $(QT_CXXFLAGS) $(RS485_CFLAGS) $< -o $@
