SHELL := /bin/bash
STANDARD_TAGS := "IO_EVT, IO_SUM, IO_TSL, IO_TSL0, IO_TSL1, IO_TSL2, IO_TSSN, MSG, IO_MONIT"
LIGIER := $(shell command -v JLigier 2> /dev/null)
ifndef LIGIER
    LIGIER := "SJ\ 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 --pid gunicorn.pid -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 send-keys -t ${SESSION_NAME}:trigger.2  \
	    "python scripts/live_triggermap.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux select-layout even-vertical

	@# ZT-Plots
	@#
	@tmux new-window -n ztplots -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:ztplots
	@tmux send-keys -t ${SESSION_NAME}:ztplots.1  \
	    "python scripts/ztplot.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux send-keys -t ${SESSION_NAME}:ztplots.2  \
	    "echo narf"
	@tmux select-layout even-vertical

	@# AHRS/RTTC
	@#
	@tmux new-window -n ahrs-rttc -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:ahrs-rttc
	@tmux send-keys -t ${SESSION_NAME}:ahrs-rttc.1  \
	    "python scripts/ahrs_calibration.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux send-keys -t ${SESSION_NAME}:ahrs-rttc.2  \
	    "python scripts/rttc.py -d ${DETECTOR_ID} -l ${DETECTOR_MANAGER_IP}" Enter
	@tmux select-layout even-vertical

	@# K40
	@#
	@tmux new-window -n k40 -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:k40
	@tmux send-keys -t ${SESSION_NAME}:k40.1  \
	    "python scripts/k40_calibration.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
	@tmux select-layout even-vertical

	@# Logs
	@#
	@tmux new-window -n log -t ${SESSION_NAME}
	@tmux split-window -v -t ${SESSION_NAME}:log
	@tmux send-keys -t ${SESSION_NAME}:log.1  \
	    "python scripts/msg_dumper.py -l 127.0.0.1 -p ${MONITORING_LIGIER_PORT} -f logs/MSG.log" Enter
	@tmux send-keys -t ${SESSION_NAME}:log.2  \
	    "touch logs/MSG.log && frontail logs/*.log --ui-highlight --ui-highlight-preset frontail.json --theme dark -l 10000 -n 200 -p 8082" Enter
	@tmux select-layout even-vertical

stop:
	@echo Stopping monitoring session...
	tmux kill-session -t ${SESSION_NAME}
	kill -9 $(shell cat gunicorn.pid)
	@sleep 5

clean:
	rm Makefile

.PHONY: build start stop clean
