#! /bin/bash

for file in {$PWD,$HOME}/.tmuxify.layout; do
  test -f $file && layout=$file && break
done

if [[ ! -f $layout ]]; then
  echo "Aborting. No $PWD/.tmuxify.layout nor $HOME/.tmuxify.layout found."
  exit 1
fi

session=${1:-$(basename $PWD | tr . -)}

if [[ -z $(tmux ls -F "#{session_name}" 2>/dev/null | grep "^$session$") ]]; then
  tmux $TMUX_OPTS new-session -s $session -d
else
  tmux $TMUX_OPTS attach -t $session
  exit 0
fi

while read window pane command; do

  [ -z "$window" -o -z "$pane" -o -z "$command" ] && continue

  pane_number=${pane//[^0-9]*}
  active=${pane//[^*]}

  if [[ -n $active ]]; then
    active_window=$window
    active_pane=$pane_number
  fi

  if [[ -z $(tmux list-windows -t $session  -F "#{window_name}" | grep "^${window}$") ]]; then
    tmux new-window -a -t $session -n $window -c $PWD
  fi

  if [[ -z $(tmux list-panes -t $session:$window  -F "#{pane_index}" | grep "^${pane_number}$") ]]; then
    tmux split-window -t $session:$window
  fi

  tmux select-layout -t $session:$window tiled > /dev/null

  tmux send-keys -t $session:$window.$pane_number "$command" C-m
done < $layout

# The first window is never used. The specified window names in the layout create new windows.
# Closing the first window will not renumber the existing ones. To fix that, add the following option in ~/.tmux.conf
#
#   set-option -g renumber-windows on
#

tmux kill-window -t $session:$( tmux list-windows -t $session -F "#{window_index}" | head -n 1 )

tmux select-window -t $session:$active_window

tmux select-pane -t $session:$active_window.$active_pane

tmux $TMUX_OPTS attach-session -t $session
