SHELL := /bin/bash
STANDARD_TAGS := "IO_EVT, IO_SUM, IO_TSL, IO_TSL0, IO_TSL1, IO_TSL2, IO_TSSN, MSG, IO_MONIT"
ifeq (, $(shell which SJ))
LIGIER := "SJ\ JLigier"
else
LIGIER := "JLigier"
endif

default: build

build:
	pip install -Ur requirements.txt

start: 
	@echo Creating tmux session...

	@tmux new-session -d -s ${SESSION_NAME}  \
	    || (echo Run \"make stop\" to close the current session.; exit 1)

	@tmux rename-window -t ${SESSION_NAME}:1 main
	@tmux split-window -v -t ${SESSION_NAME}:main
	@tmux split-window -v -t ${SESSION_NAME}:main

	@echo Launching our own ligier
	@#
	@tmux send-keys -t ${SESSION_NAME}:main.1  \
	    "${LIGIER} -d2 -P ${MONITORING_LIGIER_PORT}" Enter

	@sleep 1  # wait a second for JLigier

	@echo Setting up the ligier mirror
	@#
	@tmux send-keys -t ${SESSION_NAME}:main.2  \
	    "ligiermirror -m \"${STANDARD_TAGS}\""  \
	                " -q ${MONITORING_LIGIER_PORT}"  \
		        " -p ${DAQ_LIGIER_PORT} ${DAQ_LIGIER_IP}"  \
		        Enter
	@echo Starting the web server on 0.0.0.0:${WEBSERVER_PORT}
	@#
	@tmux send-keys -t ${SESSION_NAME}:main.3  \
	    "gunicorn -w 4 -b 0.0.0.0:${WEBSERVER_PORT} km3mon:app" Enter
	@tmux select-layout even-vertical

	@echo Starting the monitoring scripts
	@sleep 3

	@# DOM activity and DOM rates
	@#
	@tmux new-window -n doms -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:doms
	@tmux send-keys -t ${SESSION_NAME}:doms.1  \
	    "python scripts/dom_activity.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux send-keys -t ${SESSION_NAME}:doms.2  \
	    "python scripts/dom_rates.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux select-layout even-vertical

	@# PMT rates and HRV
	@#
	@tmux new-window -n pmts -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:pmts
	@tmux send-keys -t ${SESSION_NAME}:pmts.1  \
	    "python scripts/pmt_rates.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT} -i 20 -u 1" Enter
	@tmux send-keys -t ${SESSION_NAME}:pmts.2  \
	    "python scripts/pmt_hrv.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT} -i 20 -u 1" Enter
	@tmux select-layout even-vertical

	@# Trigger rates
	@#
	@tmux new-window -n trigger -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:trigger
	@tmux send-keys -t ${SESSION_NAME}:trigger.1  \
	    "python scripts/trigger_rates.py -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux select-layout even-vertical


stop:
	tmux kill-session -t ${SESSION_NAME}

clean:
	rm Makefile

.PHONY: build start stop clean
