#!/usr/bin/env bash

pkg_path="/home/mepland/chance_of_showers"

# Start a tmux daq session, if it does not exist
# https://unix.stackexchange.com/questions/443569/start-tmux-and-execute-a-set-of-commands-on-boot
if ! tmux has-session -t daq > /dev/null 2>&1; then
	echo "No tmux daq session running, starting..."
	tmux new-session -d -s daq
	# Wait 1 second for the shell started by tmux to begin, avoids printing later commands twice
	# https://stackoverflow.com/a/70203248
	sleep 1
else
	echo "Found tmux daq session running, doing nothing."
fi

# Now start windows, if they do not exist
if ! tmux has-session -t daq:daq > /dev/null 2>&1; then
	echo "No tmux daq:daq session:window running, starting window..."
	tmux new-window -d -n daq
	# move daq window to index 0, swapping current 0 to end of list if needed
	tmux swap-window -d -s daq -t 0
	sleep 1
else
	echo "Found tmux daq:daq session:window running, doing nothing."
fi

if ! tmux has-session -t daq:fan > /dev/null 2>&1; then
	echo "No tmux daq:fan session:window running, starting window..."
	tmux new-window -d -n fan
	# Move fan window to index 1, swapping current 1 to end of list if needed
	tmux swap-window -d -s fan -t 1
	sleep 1
else
	echo "Found tmux daq:fan session:window running, doing nothing."
fi

# Start daq.py, if it is not running
if ! pgrep -fc "python -u daq.py" > /dev/null 2>&1; then
	echo "No python -u daq.py running, starting..."
	# Start daq.py in daq:daq
	tmux send-keys -t daq:daq "cd $pkg_path/daq && poetry run python -u daq.py" Enter
else
	echo "Found python -u daq.py running, doing nothing"
fi

# Start fan_control.py, if it is not running
if ! pgrep -fc "python -u fan_control.py" > /dev/null 2>&1; then
	echo "No python -u fan_control.py running, starting..."
	# Start fan_control.py in daq:fan
	tmux send-keys -t daq:fan "cd $pkg_path/fan_control && poetry run python -u fan_control.py" Enter
else
	echo "Found python -u fan_control.py running, doing nothing"
fi
