#!/bin/sh
#
# Attach to a tmux session, or create it if it doesnt exist

if [ -e $XDG_CONFIG_HOME/tmux/tmux.conf ]; then
	start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
else
	start=tmux
fi

path_name="$(basename "$PWD" | tr . -)"
curr_dir=${1-$path_name}
session_name=${1:-$curr_dir}

_not_in_tmux() {
  [ -z "$TMUX" ]
}

_session_exists() {
  $start has-session -t "$session_name"
}

_create_detached_session() {
  (TMUX='' $start new-session -Ad -s "$session_name")
}

_get_session_name() {
  #${1:-$session_name}
  return "blubb"
}

_attach_or_create() {
  if _not_in_tmux; then
    $start new-session -As "$session_name"
  else
    if ! _session_exists; then
      _create_detached_session
    fi
    $start switch-client -t "$session_name"
  fi
}

_attach_or_create