#!/bin/bash
#tmux start-server
#tmux new-session -t 0

tmux has-session -t imsCore                        # determine if this is a reload
OUT=$?
if [ $OUT -eq 0 ]; # true = 0
then
    tmux detach-client -s imsCore
#   tmux kill-pane -a -t imsCore:core.4            # reload - kill all existing except pane 4
#   tmux kill-window -t imsCore:core
    tmux kill-session -t imsCore
    tmux new-session -d -s imsCore -x 160 -y 48    # fresh launch :)
#   tmux rename-window -t imsCore:core garbage     # prepare the old core for destruction
    tmux new-window -n core -k
else
    tmux start-server
    tmux new-session -d -s imsCore -x 160 -y 48    # fresh launch :)
    tmux new-window -n core -k
fi

# Launch all the Core Daemons
tmux splitw -h -t imsCore:core.0 "cd /opt/OpenIMSCore; sleep 1;bash -c ./pcscf.sh"  
tmux splitw -h -t imsCore:core.1 "cd /opt/OpenIMSCore; sleep 1; bash -c ./icscf.sh"
tmux splitw -h -t imsCore:core.2 "cd /opt/OpenIMSCore; sleep 1; bash -c ./scscf.sh"
tmux splitw -h -t imsCore:core.3 "cd /opt/OpenIMSCore/FHoSS/deploy; sleep 1; bash -c ./startup.sh"
# tmux splitw -h -t imsCore:core.4 "cd; bash"

# Cleanup all the mess we made of the panes/windows
tmux kill-pane -t imsCore:core.0
tmux select-window -t imsCore:core
tmux select-layout -t imsCore:core main-horizontal      #tiled
tmux swap-window -s imsCore:core -t imsCore:0
tmux rename-window -t imsCore:1 mgmt
tmux attach
# tmux kill-window -t garbage

