#!/bin/bash

NICOSROOT=/afs/psi.ch/project/sinqdev/src/mknicos/nicos-core

if test $# -lt 1; then
tmux ls
exit 0
fi


if [ $1 = "start" ]; then
tmux new-session -d -s nicos-cache /bin/bash
tmux send-keys -t nicos-cache "cd ${NICOSROOT}" "Enter"
tmux send-keys -t nicos-cache "source nicosenv/bin/activate" "Enter"
tmux send-keys -t nicos-cache "bin/nicos-cache" "Enter"

tmux new-session -d -s nicos-poller /bin/bash
tmux send-keys -t nicos-poller "cd ${NICOSROOT}" "Enter"
tmux send-keys -t nicos-poller "source nicosenv/bin/activate" "Enter"
tmux send-keys -t nicos-poller "bin/nicos-poller" "Enter"

tmux new-session -d -s nicos-daemon /bin/bash
tmux send-keys -t nicos-daemon "cd ${NICOSROOT}" "Enter"
tmux send-keys -t nicos-daemon "source nicosenv/bin/activate" "Enter"
tmux send-keys -t nicos-daemon "bin/nicos-daemon" "Enter"

echo "NICOS daemons started in tmux sessions"
fi

if [ $1 = "stop" ]; then
tmux kill-session -t nicos-daemon
tmux kill-session -t nicos-poller
tmux kill-session -t nicos-cache
echo 'All NICOS sessions terminated'
fi


