#!/bin/bash -f

UBUNTU_CODENAME=$(lsb_release -sc)
UBUNTU_RELEASE=$(lsb_release -sr)
FASTER_SHARE_DIR=/usr/share/faster/
DIRECTORY=$(pwd)
IF_NAME=$(netstat -ie | grep -B1 "192.168.0.1" | head -n1 | awk '{print $1}')
IF_NAME="${IF_NAME//:}"
KILLFDS="$(killall fds)"
CONFIGURATION_NAME="$(echo $1)"
export IF_NAME
export CONFIGURATION_NAME

new_default_tmux_session () {
   tmux -f $FASTER_SHARE_DIR/.tmux.conf new-session -d -s DAQ -n faster_daq_shell
   tmux split -v       -t DAQ:0.0
   tmux resize-pane    -t DAQ:0.1 -U 2
   tmux splitw -h      -t DAQ:0.0
   tmux resize-pane    -t DAQ:0.0 -R 10
   tmux splitw -v      -t DAQ:0.1
   tmux resize-pane    -t DAQ:0.2 -U 2
   tmux splitw -h      -t DAQ:0.1
   tmux resize-pane    -t DAQ:0.2 -L 5
   tmux clock          -t DAQ:0.2
   tmux send-keys      -t DAQ:0.0 "echo \"\n\nctrl-x d to detach from the tmux session \nctrl-x t to diplay time \n\"" C-m
   tmux send-keys      -t DAQ:0.1 "htop -u nobody" C-m
   tmux send-keys      -t DAQ:0.3 "speedometer -r $IF_NAME" C-m
   tmux send-keys      -t DAQ:0.4 "fds $CONFIGURATION_NAME" C-m
   tmux select-pane    -t DAQ:0.4
   tmux attach-session -t DAQ
}

new_xenial_tmux_session () {
   tmux -f $FASTER_SHARE_DIR/.tmux.conf new-session -d -s DAQ -n faster_daq_shell
   tmux split -v       -t DAQ:0.0
   tmux resize-pane    -t DAQ:0.1 -U 8
   tmux splitw -h      -t DAQ:0.0
   tmux splitw -v      -t DAQ:0.2
   tmux splitw -h      -t DAQ:0.2
   tmux clock          -t DAQ:0.4
   tmux send-keys      -t DAQ:0.0 "echo \"Shell tool utilities. For example use it with faster_setup_gui\"" C-m
   tmux send-keys      -t DAQ:0.2 "htop -u nobody" C-m
   tmux send-keys      -t DAQ:0.3 "speedometer -r $IF_NAME" C-m
   tmux send-keys      -t DAQ:0.1 "fds $CONFIGURATION_NAME" C-m
   tmux select-pane    -t DAQ:0.1
   tmux attach-session -t DAQ
}

if [ ! -f $HOME/.tmux.conf ]
then
   echo $FASTER_SHARE_DIR
   cp $FASTER_SHARE_DIR/.tmux.conf $HOME/
fi


# check that tmux is installed
if command -v tmux > /dev/null; then
  # check that we're not already in screen or tmux; only via SSH connection (remote)
  if [[ ! $TERM =~ screen ]] && [ -z $TMUX ] ; then
     # if a session called admin exist, attach to it; else create it
     if tmux ls | grep -q DAQ; then
        echo "A tmux FASTER DAQ session is already running"
        echo "Do you want to restart an new one or attach it ?"
        echo "   1 : Restart a new one"
        echo "   2 : Attach to the previous one (default)"
              read -p "Make your choice : " choice
        if [[ $choice == 1 ]]; then
	        tmux kill-session -t DAQ
          if [[ $UBUNTU_CODENAME == "xenial" ]] ; then
             new_xenial_tmux_session
           else
	           new_default_tmux_session
          fi
        else
          if [[ $UBUNTU_CODENAME == "xenial" ]] ; then
            tmux send-keys   -t DAQ:0.1 "cd $DIRECTORY" C-m
            tmux send-keys   -t DAQ:0.1 "fds $CONFIGURATION_NAME" C-m
            exec tmux attach -t DAQ
          else
            tmux send-keys   -t DAQ:0.4 "cd $DIRECTORY" C-m
            #$KILLFDS
            tmux send-keys   -t DAQ:0.4 "fds $CONFIGURATION_NAME" C-m
            exec tmux attach -t DAQ
          fi
        fi
     else
        if [[ $UBUNTU_CODENAME == "xenial" ]] ; then
           new_xenial_tmux_session
         else
	         new_default_tmux_session
        fi
     fi
  else
     if [[ $UBUNTU_CODENAME == "xenial" ]] ; then
        tmux send-keys      -t DAQ:0.1 "fds $CONFIGURATION_NAME" C-m
        tmux select-pane    -t DAQ:0.1
      else
        tmux send-keys      -t DAQ:0.4 "fds $CONFIGURATION_NAME" C-m
        tmux select-pane    -t DAQ:0.4
     fi
  fi
fi

