#!<%= ENV["SHELL"] || "/bin/bash" %>


<%- if !append? -%>
  # Clear rbenv variables before starting tmux
  unset RBENV_VERSION
  unset RBENV_DIR

  <%= tmux %> start-server;
<%- end -%>

cd <%= root || "." %>

# Run on_project_start command.
<%= hook_on_project_start %>

<%- if append? || !tmux_has_session?(name) -%>

  # Run pre command.
  <%= pre %>

  # Run on_project_first_start command.
  <%= hook_on_project_first_start %>

  <%= tmux_new_session_command %>

  <% if Tmuxinator::Config.version < 1.7 %>
  # Set the default path for versions prior to 1.7
    <% if root? %>
  <%= tmux %> set-option -t <%= name %> <%= Tmuxinator::Config.default_path_option %> <%= root -%> 1>/dev/null
    <% end %>
  <% end %>

  <% if enable_pane_titles? %>
    <% if Tmuxinator::Config.version < 2.6 %>
  <%= pane_titles_not_supported_warning %>
    <% end %>
    <% if pane_title_position? && !pane_title_position_valid? %>
  <%= pane_title_position_not_valid_warning %>
    <% end %>
  <% end %>

  # Create windows.
  <% windows.each do |window| %>
  <%= window.tmux_new_window_command %>
  <% end %>

  <% windows.each do |window| %>

  # Window "<%= window.name %>"
    <% if window.synchronize_before? %>
  <%= window.tmux_synchronize_panes %>
    <% end %>

    <% if enable_pane_titles? && Tmuxinator::Config.version >= 2.6 %>
  <%= tmux_set_pane_title_position(window.tmux_window_target) %>
  <%= tmux_set_pane_title_format(window.tmux_window_target) %>
    <% end %>

    <% unless window.panes? %>
      <% if window.project.pre_window %>
  <%= window.tmux_pre_window_command %>
      <% end %>
      <% window.commands.each do |command| %>
  <%= command %>
      <% end %>
    <% else %>
      <% window.panes.each do |pane| %>
        <% unless Tmuxinator::Config.version < 2.6 %>
  <%= pane.tmux_set_title %>
        <% end %>
        <% if pane.project.pre_window %>
  <%= pane.tmux_pre_window_command %>
        <% end %>
        <% if pane.tab.pre %>
  <%= pane.tmux_pre_command %>
        <% end %>
        <% pane.commands.each do |command| %>
  <%= pane.tmux_main_command(command) %>
        <% end %>

        <% unless pane.last? %>
  <%= pane.tmux_split_command %>
        <% end %>
  <%= window.tmux_tiled_layout_command %>
      <% end %>

  <%= window.tmux_layout_command %>
  <%= window.tmux_focus_pane_command %>
    <% end %>

    <% if window.synchronize_after? %>
  <%= window.tmux_synchronize_panes %>
    <% end %>
  <% end %>

  <%= tmux %> select-window -t <%= startup_window %>
  <%= tmux_startup_pane_command %>
<%- else -%>
  # Run on_project_restart command.
  <%= hook_on_project_restart %>
<%- end -%>

<%- if attach? && !append? -%>
  if [ -z "$TMUX" ]; then
    <%= tmux %> -u attach-session -t <%= name %>
  else
    <%= tmux %> -u switch-client -t <%= name %>
  fi
<%- end -%>

<%= post %>

# Run on_project_exit command.
<%= hook_on_project_exit %>
